decidim-restfull-proposals 0.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/app/controllers/decidim/api/rest_full/components/proposal_components_controller.rb +91 -0
- data/app/controllers/decidim/api/rest_full/draft_proposals/draft_proposals_controller.rb +116 -0
- data/app/controllers/decidim/api/rest_full/proposals/proposal_extended_data_controller.rb +31 -0
- data/app/controllers/decidim/api/rest_full/proposals/proposals_controller.rb +172 -0
- data/app/controllers/decidim/api/rest_full/vote_proposals/vote_proposals_controller.rb +90 -0
- data/app/jobs/decidim/rest_full/proposals/proposal_webhook_job.rb +67 -0
- data/app/models/decidim/rest_full/proposals/proposal_application_id.rb +14 -0
- data/app/serializers/decidim/api/rest_full/proposals/draft_proposal_serializer.rb +44 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_component_serializer.rb +232 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_serializer.rb +113 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_state_serializer.rb +39 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer.rb +54 -0
- data/app/serializers/decidim/api/rest_full/proposals/vote_proposal_serializer.rb +27 -0
- data/app/services/decidim/rest_full/proposals/draft_proposals_operations.rb +248 -0
- data/app/services/decidim/rest_full/proposals/proposal_vote_eligibility.rb +106 -0
- data/app/services/decidim/rest_full/proposals/proposal_votes_operations.rb +173 -0
- data/app/services/decidim/rest_full/proposals/vote_proposals_operations.rb +114 -0
- data/config/locales/decidim_rest_full_proposals.en.yml +26 -0
- data/lib/decidim/rest_full/http_cache/proposal_show_fingerprint.rb +77 -0
- data/lib/decidim/rest_full/proposals/engine.rb +129 -0
- data/lib/decidim/rest_full/proposals/proposal_client_id_override.rb +18 -0
- data/lib/decidim/rest_full/proposals/proposals_controller_override.rb +28 -0
- data/lib/decidim/rest_full/proposals/ransackers.rb +84 -0
- data/lib/decidim/rest_full/proposals/test_definitions.rb +7 -0
- data/lib/decidim/rest_full/test/definitions/draft_proposal.rb +101 -0
- data/lib/decidim/rest_full/test/definitions/proposal.rb +115 -0
- data/lib/decidim/rest_full/test/definitions/proposal_component_schema.rb +110 -0
- data/lib/decidim/rest_full/test/definitions/proposals.rb +4 -0
- data/lib/decidim/rest_full/test/definitions/tags/draft_proposals_tag.rb +26 -0
- data/lib/decidim/rest_full/test/definitions/tags/proposal_tag.rb +32 -0
- data/lib/decidim/rest_full/test/definitions/vote_proposal.rb +87 -0
- data/lib/decidim-restfull-proposals.rb +17 -0
- data/spec/jobs/decidim/rest_full/proposals/proposal_webhook_job_spec.rb +66 -0
- data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_index_spec.rb +149 -0
- data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_show_spec.rb +140 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_create_spec.rb +116 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_delete_spec.rb +64 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_publish_spec.rb +132 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_show_spec.rb +72 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_update_spec.rb +307 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposal_extended_data_controller_spec.rb +80 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_index_spec.rb +275 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_show_spec.rb +187 -0
- data/spec/requests/decidim/api/rest_full/scenarios/draft_publish_async_increments_count_and_lists_newest_spec.rb +144 -0
- data/spec/requests/decidim/api/rest_full/scenarios/participatory_process_phase_change_webhook_and_component_meta_spec.rb +130 -0
- data/spec/requests/decidim/api/rest_full/scenarios/proposals_published_at_desc_prev_next_chain_spec.rb +72 -0
- data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb +88 -0
- data/spec/requests/decidim/api/rest_full/vote_proposals/vote_proposals_controller_spec.rb +231 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_chatbot_menu_spec.rb +147 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_phases_spec.rb +88 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_votes_spec.rb +103 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_serializer_can_vote_spec.rb +86 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer_spec.rb +63 -0
- data/spec/services/decidim/rest_full/proposals/proposal_vote_eligibility_spec.rb +120 -0
- metadata +132 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Proposals
|
|
7
|
+
class ProposalComponentSerializer < ::Decidim::Api::RestFull::Core::ComponentSerializer
|
|
8
|
+
extend ::Decidim::Api::RestFull::Core::Helpers::ResourceLinksHelper
|
|
9
|
+
|
|
10
|
+
AWESOME_SETTINGS_KEYS = [
|
|
11
|
+
:awesome_voting_manifest,
|
|
12
|
+
:voting_cards_show_abstain,
|
|
13
|
+
:voting_cards_show_modal_help,
|
|
14
|
+
:voting_cards_box_title,
|
|
15
|
+
:voting_cards_instructions
|
|
16
|
+
].freeze
|
|
17
|
+
|
|
18
|
+
AWESOME_TRANSLATED_SETTINGS = [:voting_cards_box_title, :voting_cards_instructions].freeze
|
|
19
|
+
|
|
20
|
+
def self.resources_for(component, act_as)
|
|
21
|
+
resources = ::Decidim::Proposals::Proposal.where(component:)
|
|
22
|
+
if act_as.nil?
|
|
23
|
+
resources.published
|
|
24
|
+
else
|
|
25
|
+
resources.joins(:coauthorships).published.or(
|
|
26
|
+
resources.joins(:coauthorships).where(
|
|
27
|
+
published_at: nil,
|
|
28
|
+
coauthorships: { decidim_author_id: act_as.id }
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.draft_for(component_id, user_id, client_id)
|
|
35
|
+
Decidim::Proposals::Proposal.joins(
|
|
36
|
+
:rest_full_application,
|
|
37
|
+
:coauthorships
|
|
38
|
+
).where(
|
|
39
|
+
rest_full_application: { api_client_id: client_id },
|
|
40
|
+
decidim_component_id: component_id,
|
|
41
|
+
published_at: nil,
|
|
42
|
+
decidim_coauthorships: { decidim_author_id: user_id }
|
|
43
|
+
).limit(1)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.unvoted_voteable_proposals_exist?(component, user)
|
|
47
|
+
Decidim::RestFull::Proposals::ProposalVoteEligibility.unvoted_voteable_proposals_exist?(component, user)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.awesome_weighted_voting?
|
|
51
|
+
Decidim::Toggle.gem_present?("decidim-decidim_awesome") &&
|
|
52
|
+
Decidim::DecidimAwesome.enabled?(:weighted_proposal_voting)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.voting_cards_manifest?(settings_h)
|
|
56
|
+
settings_h[:awesome_voting_manifest].to_s == "voting_cards"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.vote_option(weight, available_locales, label_key)
|
|
60
|
+
{
|
|
61
|
+
weight:,
|
|
62
|
+
label: available_locales.index_with { |locale| I18n.t(label_key, locale:) }
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.vote_options_for(settings_h, available_locales)
|
|
67
|
+
has_abstain = ActiveModel::Type::Boolean.new.cast(settings_h[:voting_cards_show_abstain])
|
|
68
|
+
weights =
|
|
69
|
+
if awesome_weighted_voting? && voting_cards_manifest?(settings_h)
|
|
70
|
+
[1, 2, 3]
|
|
71
|
+
else
|
|
72
|
+
[1]
|
|
73
|
+
end
|
|
74
|
+
weights = [0] + weights if has_abstain
|
|
75
|
+
|
|
76
|
+
weights.map do |weight|
|
|
77
|
+
label_key =
|
|
78
|
+
if weight.zero? || (awesome_weighted_voting? && voting_cards_manifest?(settings_h))
|
|
79
|
+
"decidim.decidim_awesome.voting.voting_cards.weights.weight_#{weight}"
|
|
80
|
+
else
|
|
81
|
+
"decidim.components.proposals.actions.vote"
|
|
82
|
+
end
|
|
83
|
+
vote_option(weight, available_locales, label_key)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.assign_awesome_settings!(metas, settings_h, available_locales)
|
|
88
|
+
return unless awesome_weighted_voting?
|
|
89
|
+
|
|
90
|
+
AWESOME_SETTINGS_KEYS.each do |key|
|
|
91
|
+
next unless settings_h.has_key?(key)
|
|
92
|
+
|
|
93
|
+
value = settings_h[key]
|
|
94
|
+
metas[key] =
|
|
95
|
+
if AWESOME_TRANSLATED_SETTINGS.include?(key)
|
|
96
|
+
translated_field(value, available_locales)
|
|
97
|
+
else
|
|
98
|
+
value
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
meta do |component, params|
|
|
104
|
+
metas = ::Decidim::Api::RestFull::Core::ComponentSerializer.default_meta(component)
|
|
105
|
+
component = Decidim::Component.find(component.id) unless component.is_a?(Decidim::Component)
|
|
106
|
+
available_locales = params[:locales]
|
|
107
|
+
|
|
108
|
+
settings_h = component.settings.to_h
|
|
109
|
+
current_settings_h = component.current_settings.to_h
|
|
110
|
+
# Decidim 0.32 renamed/removed some settings; keep stable API meta keys.
|
|
111
|
+
settings_keys = %w(
|
|
112
|
+
amendments_enabled
|
|
113
|
+
attachments_allowed
|
|
114
|
+
comments_enabled
|
|
115
|
+
comments_max_length
|
|
116
|
+
default_sort_order
|
|
117
|
+
geocoding_enabled
|
|
118
|
+
minimum_votes_per_user
|
|
119
|
+
official_proposals_enabled
|
|
120
|
+
participatory_texts_enabled
|
|
121
|
+
proposal_edit_time
|
|
122
|
+
proposal_limit
|
|
123
|
+
resources_permissions_enabled
|
|
124
|
+
threshold_per_proposal
|
|
125
|
+
vote_limit
|
|
126
|
+
)
|
|
127
|
+
settings_keys.each do |key|
|
|
128
|
+
metas[key.to_sym] = settings_h[key.to_sym]
|
|
129
|
+
end
|
|
130
|
+
metas[:collaborative_drafts_enabled] = false
|
|
131
|
+
metas[:scopes_enabled] = settings_h.fetch(:scopes_enabled, false)
|
|
132
|
+
metas[:proposal_edit_before_minutes] = edit_time_to_minutes(settings_h[:edit_time])
|
|
133
|
+
|
|
134
|
+
step_settings_keys = %w(
|
|
135
|
+
votes_enabled
|
|
136
|
+
creation_enabled
|
|
137
|
+
proposal_answering_enabled
|
|
138
|
+
amendment_creation_enabled
|
|
139
|
+
amendment_reaction_enabled
|
|
140
|
+
amendment_promotion_enabled
|
|
141
|
+
)
|
|
142
|
+
step_settings_keys.each do |key|
|
|
143
|
+
metas[key.to_sym] = current_settings_h[key.to_sym]
|
|
144
|
+
end
|
|
145
|
+
# endorsements → likes in Decidim 0.32; keep endorsements_* for API stability
|
|
146
|
+
metas[:endorsements_enabled] = current_settings_h.fetch(:likes_enabled) do
|
|
147
|
+
current_settings_h[:endorsements_enabled]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
resources = ::Decidim::Proposals::Proposal.where(component:)
|
|
151
|
+
act_as = params[:act_as]
|
|
152
|
+
proposal_limit = metas[:proposal_limit].to_i
|
|
153
|
+
metas[:can_create_proposals] = false
|
|
154
|
+
if metas[:creation_enabled] && act_as.present?
|
|
155
|
+
metas[:can_create_proposals] = proposal_limit.zero? ||
|
|
156
|
+
resources.joins(:coauthorships).published.where(
|
|
157
|
+
coauthorships: { decidim_author_id: act_as.id }
|
|
158
|
+
).count < proposal_limit
|
|
159
|
+
end
|
|
160
|
+
eligibility = Decidim::RestFull::Proposals::ProposalVoteEligibility
|
|
161
|
+
metas[:awesome_votes_enabled_by_status] = eligibility.awesome_votes_enabled_by_status(component)
|
|
162
|
+
metas[:awesome_votes_enabled_states] = eligibility.awesome_votes_enabled_states(component)
|
|
163
|
+
metas[:can_vote] = eligibility.voting_enabled?(component)
|
|
164
|
+
if metas[:can_vote]
|
|
165
|
+
metas[:can_vote] =
|
|
166
|
+
if act_as.present?
|
|
167
|
+
unvoted_voteable_proposals_exist?(component, act_as)
|
|
168
|
+
else
|
|
169
|
+
true
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
metas[:can_endorse] = metas[:endorsements_enabled]
|
|
173
|
+
metas[:can_comment] = metas[:comments_enabled]
|
|
174
|
+
|
|
175
|
+
assign_awesome_settings!(metas, settings_h, available_locales)
|
|
176
|
+
|
|
177
|
+
metas[:votes] =
|
|
178
|
+
if metas[:votes_enabled]
|
|
179
|
+
vote_options_for(settings_h, available_locales)
|
|
180
|
+
else
|
|
181
|
+
[]
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
metas
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def self.edit_time_to_minutes(edit_time)
|
|
188
|
+
return 0 if edit_time.blank?
|
|
189
|
+
|
|
190
|
+
amount, unit = Array(edit_time)
|
|
191
|
+
amount = amount.to_i
|
|
192
|
+
case unit.to_s
|
|
193
|
+
when "hours" then amount * 60
|
|
194
|
+
when "days" then amount * 24 * 60
|
|
195
|
+
else amount
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
private_class_method :edit_time_to_minutes
|
|
199
|
+
private_class_method :awesome_weighted_voting?
|
|
200
|
+
private_class_method :voting_cards_manifest?
|
|
201
|
+
private_class_method :vote_option
|
|
202
|
+
private_class_method :vote_options_for
|
|
203
|
+
private_class_method :assign_awesome_settings!
|
|
204
|
+
|
|
205
|
+
link :draft, if: (proc do |component, params|
|
|
206
|
+
next false unless params[:act_as]
|
|
207
|
+
|
|
208
|
+
draft_for(component.id, params[:act_as].id, params[:client_id]).exists?
|
|
209
|
+
end) do |component, params|
|
|
210
|
+
draft = draft_for(component.id, params[:act_as].id, params[:client_id]).first
|
|
211
|
+
infos = link_infos_from_resource(draft)
|
|
212
|
+
{
|
|
213
|
+
href: link_join(params[:host], "draft_proposals", draft.id),
|
|
214
|
+
title: "Draft Details",
|
|
215
|
+
rel: "resource",
|
|
216
|
+
meta: {
|
|
217
|
+
**infos,
|
|
218
|
+
action_method: "GET"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
has_many :resources, meta: (proc do |component, params|
|
|
224
|
+
{ count: resources_for(component, params[:act_as]).count }
|
|
225
|
+
end) do |component, params|
|
|
226
|
+
resources_for(component, params[:act_as]).limit(50)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Proposals
|
|
7
|
+
# JSON:API serializer for proposals, enriching them with voting
|
|
8
|
+
# metadata, client ids and translated fields for the REST API.
|
|
9
|
+
class ProposalSerializer < ::Decidim::Api::RestFull::Core::ResourceSerializer
|
|
10
|
+
extend ::Decidim::Api::RestFull::Core::Helpers::ResourceLinksHelper
|
|
11
|
+
|
|
12
|
+
def self.default_meta(proposal)
|
|
13
|
+
scope = proposal.component.scope || proposal.participatory_space.scope
|
|
14
|
+
metas = {
|
|
15
|
+
published: proposal.published?
|
|
16
|
+
}
|
|
17
|
+
metas[:scope] = scope.id if scope
|
|
18
|
+
metas
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
meta do |proposal, params|
|
|
22
|
+
metas = default_meta(proposal)
|
|
23
|
+
metas[:can_vote] = Decidim::RestFull::Proposals::ProposalVoteEligibility.proposal_can_vote?(proposal)
|
|
24
|
+
metas[:client_id] = proposal.rest_full_application.api_client_id if proposal.rest_full_application
|
|
25
|
+
if params[:act_as] && proposal.published?
|
|
26
|
+
vote = proposal.votes.where(decidim_author_id: params[:act_as].id).last
|
|
27
|
+
vote_manifest = proposal.component.settings[:awesome_voting_manifest]
|
|
28
|
+
if vote
|
|
29
|
+
metas[:voted] = if vote_manifest && Decidim::Toggle.gem_present?("decidim-decidim_awesome") && Decidim::DecidimAwesome.enabled?(:weighted_proposal_voting)
|
|
30
|
+
match = Decidim::DecidimAwesome::VoteWeight.find_by(proposal_vote_id: vote.id)
|
|
31
|
+
weight_value = match&.weight || 1
|
|
32
|
+
{ weight: weight_value }
|
|
33
|
+
else
|
|
34
|
+
{ weight: 1 }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
metas
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
attribute :title do |comp, params|
|
|
43
|
+
translated_field(comp.title, params[:locales])
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
attribute :body do |comp, params|
|
|
47
|
+
translated_field(comp.body, params[:locales])
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
has_one :state, if: (proc do |proposal|
|
|
51
|
+
proposal.state
|
|
52
|
+
end), meta: (proc do |proposal, _params|
|
|
53
|
+
{ token: proposal.state }
|
|
54
|
+
end) do |proposal, _params|
|
|
55
|
+
proposal.proposal_state
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
has_one :author,
|
|
59
|
+
if: (proc do |proposal|
|
|
60
|
+
proposal.coauthorships.count.positive?
|
|
61
|
+
end),
|
|
62
|
+
serializer: ::Decidim::Api::RestFull::Core::UserSerializer do |proposal, _params|
|
|
63
|
+
proposal.coauthorships.first.author
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
has_many :coauthors,
|
|
67
|
+
meta: (proc do |proposal, _params|
|
|
68
|
+
{ count: proposal.coauthorships.count }
|
|
69
|
+
end),
|
|
70
|
+
serializer: ::Decidim::Api::RestFull::Core::UserSerializer do |proposal, _params|
|
|
71
|
+
proposal.coauthorships.map do |coauthorship, _params|
|
|
72
|
+
coauthorship.author
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
link :next, if: (proc do |_object, params|
|
|
77
|
+
params.has_key?(:next) && params[:next]
|
|
78
|
+
end) do |object, params|
|
|
79
|
+
next_id = params[:next]
|
|
80
|
+
infos = link_infos_from_resource(object)
|
|
81
|
+
{
|
|
82
|
+
href: link_for_resource(params[:host], infos, next_id),
|
|
83
|
+
title: "Next Proposal",
|
|
84
|
+
rel: "resource",
|
|
85
|
+
meta: {
|
|
86
|
+
**infos,
|
|
87
|
+
resource_id: next_id.to_s,
|
|
88
|
+
action_method: "GET"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
link :prev, if: (proc do |_object, params|
|
|
94
|
+
params.has_key?(:prev) && params[:prev]
|
|
95
|
+
end) do |object, params|
|
|
96
|
+
prev_id = params[:prev]
|
|
97
|
+
infos = link_infos_from_resource(object)
|
|
98
|
+
{
|
|
99
|
+
href: link_for_resource(params[:host], infos, prev_id),
|
|
100
|
+
title: "Previous Proposal",
|
|
101
|
+
rel: "resource",
|
|
102
|
+
meta: {
|
|
103
|
+
**infos,
|
|
104
|
+
resource_id: prev_id.to_s,
|
|
105
|
+
action_method: "GET"
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Proposals
|
|
7
|
+
class ProposalStateSerializer < ::Decidim::Api::RestFull::Core::ResourceSerializer
|
|
8
|
+
def self.default_meta(state)
|
|
9
|
+
scope = state.component.scope || state.participatory_space.scope
|
|
10
|
+
metas = {}
|
|
11
|
+
metas[:scope] = scope.id if scope
|
|
12
|
+
metas
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
meta do |state, _params|
|
|
16
|
+
metas = default_meta(state)
|
|
17
|
+
metas
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attributes :token, :bg_color, :text_color
|
|
21
|
+
|
|
22
|
+
attribute :title do |state, params|
|
|
23
|
+
translated_field(state.title, params[:locales])
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
attribute :announcement_title do |state, params|
|
|
27
|
+
translated_field(state.announcement_title, params[:locales])
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
has_many :proposals, meta: (proc do |state, _params|
|
|
31
|
+
{ count: state.proposals.count }
|
|
32
|
+
end) do |state, _params|
|
|
33
|
+
state.proposals
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Proposals
|
|
7
|
+
# Webhook envelope for any proposal lifecycle event (draft or published — same model).
|
|
8
|
+
class ProposalWebhookSerializer < ::Decidim::Api::RestFull::Core::WebhookSerializer
|
|
9
|
+
def self.find_example_resource(organization, event_name:)
|
|
10
|
+
proposals = proposals_for(organization)
|
|
11
|
+
if event_name.to_s.start_with?("draft_")
|
|
12
|
+
proposals.find(&:draft?)
|
|
13
|
+
else
|
|
14
|
+
proposals.reverse_each.find { |proposal| !proposal.draft? }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.proposals_for(organization)
|
|
19
|
+
component_ids = Decidim::Component
|
|
20
|
+
.where(manifest_name: "proposals")
|
|
21
|
+
.filter_map do |component|
|
|
22
|
+
next unless component.organization == organization
|
|
23
|
+
|
|
24
|
+
component.id
|
|
25
|
+
end
|
|
26
|
+
Decidim::Proposals::Proposal.where(decidim_component_id: component_ids).order(:id).to_a
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
protected
|
|
30
|
+
|
|
31
|
+
def resource_serializable_hash
|
|
32
|
+
params = serializer_params(publishable: publishable?)
|
|
33
|
+
if resource.draft?
|
|
34
|
+
DraftProposalSerializer.new(resource, params:).serializable_hash
|
|
35
|
+
else
|
|
36
|
+
ProposalSerializer.new(resource, params:).serializable_hash
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def publishable?
|
|
43
|
+
return false unless resource.draft?
|
|
44
|
+
|
|
45
|
+
Decidim::Proposals::ProposalForm
|
|
46
|
+
.from_model(resource)
|
|
47
|
+
.with_context(current_organization: organization, current_component: resource.component)
|
|
48
|
+
.valid?
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Proposals
|
|
7
|
+
class VoteProposalSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
set_type :vote_proposals
|
|
9
|
+
|
|
10
|
+
attributes :weight
|
|
11
|
+
|
|
12
|
+
attribute :created_at do |vote|
|
|
13
|
+
vote.created_at.iso8601
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attribute :updated_at do |vote|
|
|
17
|
+
vote.updated_at.iso8601
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
belongs_to :proposal, record_type: :proposals, &:proposal
|
|
21
|
+
|
|
22
|
+
belongs_to :author, record_type: :users, &:author
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|