Skip to content

Commit

Permalink
Refactor the update voting form
Browse files Browse the repository at this point in the history
  • Loading branch information
alecslupu committed Jan 26, 2021
1 parent 3e1b6a7 commit 35b19a3
Showing 1 changed file with 10 additions and 2 deletions.
Expand Up @@ -11,6 +11,9 @@ class UpdateVoting < Rectify::Command
# voting - the Voting to update
# form - A form object with the params.
def initialize(voting, form)
image_fields.each do |field|
form.send("#{field}=".to_sym, voting.send(field)) if form.send(field).blank?
end
@voting = voting
@form = form
end
Expand All @@ -29,8 +32,9 @@ def call
if voting.valid?
broadcast(:ok, voting)
else
form.errors.add(:banner_image, voting.errors[:banner_image]) if voting.errors.include? :banner_image
form.errors.add(:introductory_image, voting.errors[:introductory_image]) if voting.errors.include? :introductory_image
image_fields.each do |field|
form.errors.add(field, voting.errors[field]) if organization.errors.include? field
end
broadcast(:invalid)
end
end
Expand All @@ -39,6 +43,10 @@ def call

attr_reader :form, :voting

def image_fields
[:banner_image, :introductory_image]
end

def update_voting!
voting.assign_attributes(attributes)
return unless voting.valid?
Expand Down

0 comments on commit 35b19a3

Please sign in to comment.