/
update_voting.rb
83 lines (71 loc) · 2.25 KB
/
update_voting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true
module Decidim
module Votings
module Admin
# A command with all the business logic when updating an existing
# voting in the system.
class UpdateVoting < Rectify::Command
# Public: Initializes the 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
# Executes the command. Broadcasts these events:
#
# - :ok when everything is valid.
# - :invalid if the form wasn't valid and we couldn't proceed.
#
# Returns nothing.
def call
return broadcast(:invalid) if form.invalid?
update_voting!
if voting.valid?
broadcast(:ok, voting)
else
image_fields.each do |field|
form.errors.add(field, voting.errors[field]) if voting.errors.include? field
end
broadcast(:invalid)
end
end
private
attr_reader :form, :voting
def image_fields
[:banner_image, :introductory_image]
end
def update_voting!
voting.assign_attributes(attributes)
return unless voting.valid?
voting.save!
Decidim.traceability.perform_action!(:update, voting, form.current_user) do
voting
end
end
def attributes
{
title: form.title,
description: form.description,
slug: form.slug,
start_time: form.start_time,
end_time: form.end_time,
scope: form.scope
}.merge(uploader_attributes)
end
def uploader_attributes
{
banner_image: form.banner_image,
remove_banner_image: form.remove_banner_image,
introductory_image: form.introductory_image,
remove_introductory_image: form.remove_introductory_image
}.delete_if { |_k, val| val.is_a?(Decidim::ApplicationUploader) }
end
end
end
end
end