/
update_result.rb
77 lines (65 loc) · 2.06 KB
/
update_result.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
# frozen_string_literal: true
module Decidim
module Accountability
module Admin
# This command is executed when the user changes a Result from the admin
# panel.
class UpdateResult < Rectify::Command
# Initializes an UpdateResult Command.
#
# form - The form from which to get the data.
# result - The current instance of the result to be updated.
def initialize(form, result)
@form = form
@result = result
end
# Updates the result if valid.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
return broadcast(:invalid) if form.invalid?
transaction do
update_result
link_proposals
link_meetings
end
broadcast(:ok)
end
private
attr_reader :result, :form
def update_result
Decidim.traceability.update!(
result,
form.current_user,
scope: @form.scope,
category: @form.category,
parent_id: @form.parent_id,
title: @form.title,
description: @form.description,
start_date: @form.start_date,
end_date: @form.end_date,
progress: @form.progress,
decidim_accountability_status_id: @form.decidim_accountability_status_id
)
end
def proposals
@proposals ||= result.sibling_scope(:proposals).where(id: form.proposal_ids)
end
def meeting_ids
@meeting_ids ||= proposals.flat_map do |proposal|
proposal.linked_resources(:meetings, "proposals_from_meeting").pluck(:id)
end.uniq
end
def meetings
@meetings ||= result.sibling_scope(:meetings).where(id: meeting_ids)
end
def link_proposals
result.link_resources(proposals, "included_proposals")
end
def link_meetings
result.link_resources(meetings, "meetings_through_proposals")
end
end
end
end
end