There is a form object which allows creating an object with associations. This main problem is associations update. I really don't know how to extend this without tons of each and conditions. What is the most convenient way to extend this class for updating object?
module Company
class JobForm
include ActiveModel::Model
attr_accessor :title, :body, :city, :start_date, :attachments,
:requirements, :company
attr_reader :object
validates :title, :body, :start_date, :city, presence: true
def save
persist! if valid?
end
private
def persist!
ActiveRecord::Base.transaction do
save_job
save_attachments
save_requirements
end
object
end
def save_job
@object = company.jobs.create(
# attributes.. ..)
end
def save_attachments
return unless attachments&.present?
attachments.each do |attacment_attributes|
object.attachments.create(attacment_attributes)
end
end
def save_requirements
return unless requirements&.present?
object.requirements.destroy_all
requirements.each do |requirement_attributes|
object.requirements.create(requirement_attributes)
end
end
end
end
Aucun commentaire:
Enregistrer un commentaire