-
Notifications
You must be signed in to change notification settings - Fork 4
/
component.rb
212 lines (183 loc) · 9.42 KB
/
component.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# frozen_string_literal: true
require "decidim/components/namer"
Decidim.register_component(:budgets) do |component|
component.engine = Decidim::Budgets::Engine
component.admin_engine = Decidim::Budgets::AdminEngine
component.icon = "media/images/decidim_budgets.svg"
component.stylesheet = "decidim/budgets/budgets"
component.permissions_class_name = "Decidim::Budgets::Permissions"
component.component_form_class_name = "Decidim::Budgets::Admin::ComponentForm"
component.data_portable_entities = ["Decidim::Budgets::Order"]
component.newsletter_participant_entities = ["Decidim::Budgets::Order"]
component.query_type = "Decidim::Budgets::BudgetsType"
component.actions = %w(vote comment)
component.on(:before_destroy) do |instance|
raise StandardError, "Can't remove this component" if Decidim::Budgets::Budget.where(component: instance).any?
end
component.register_resource(:budget) do |resource|
resource.model_class_name = "Decidim::Budgets::Budget"
resource.card = "decidim/budgets/budget"
resource.searchable = true
end
component.register_resource(:project) do |resource|
resource.model_class_name = "Decidim::Budgets::Project"
resource.template = "decidim/budgets/projects/linked_projects"
resource.card = "decidim/budgets/project"
resource.actions = %w(vote comment)
resource.searchable = true
end
component.register_stat :budgets_count, primary: true, priority: Decidim::StatsRegistry::LOW_PRIORITY do |components|
Decidim::Budgets::Budget.where(component: components).count
end
component.register_stat :projects_count, priority: Decidim::StatsRegistry::LOW_PRIORITY do |components, start_at, end_at|
Decidim::Budgets::FilteredProjects.for(components, start_at, end_at).count
end
component.register_stat :orders_count do |components, start_at, end_at|
budgets = Decidim::Budgets::Budget.where(component: components)
orders = Decidim::Budgets::Order.where(component: budgets)
orders = orders.where("created_at >= ?", start_at) if start_at.present?
orders = orders.where("created_at <= ?", end_at) if end_at.present?
orders.count
end
component.register_stat :comments_count, tag: :comments do |components, start_at, end_at|
projects = Decidim::Budgets::FilteredProjects.for(components, start_at, end_at)
projects.sum(:comments_count)
end
component.register_stat :followers_count, tag: :followers, priority: Decidim::StatsRegistry::LOW_PRIORITY do |components, start_at, end_at|
projects_ids = Decidim::Budgets::FilteredProjects.for(components, start_at, end_at).pluck(:id)
Decidim::Follow.where(decidim_followable_type: "Decidim::Budgets::Project", decidim_followable_id: projects_ids).count
end
component.exports :projects do |exports|
exports.collection do |component_instance, _user, resource_id|
budgets = resource_id ? Decidim::Budgets::Budget.find(resource_id) : Decidim::Budgets::Budget.where(decidim_component_id: component_instance)
Decidim::Budgets::Project
.where(decidim_budgets_budget_id: budgets)
.includes(:category, :component)
end
exports.include_in_open_data = true
exports.serializer Decidim::Budgets::ProjectSerializer
end
component.imports :paper_ballot_result do |imports|
imports.messages do |msg|
msg.set(:resource_name) { |count: 1| I18n.t("decidim.budgets.admin.imports.resources.paper_ballot_results", count: count) }
msg.set(:title) { I18n.t("decidim.budgets.admin.imports.title.paper_ballot_results") }
msg.set(:label) { I18n.t("decidim.budgets.admin.imports.label.paper_ballot_results") }
msg.set(:help) { I18n.t("decidim.budgets.admin.imports.help.paper_ballot_results") }
end
imports.creator Decidim::Budgets::Import::PaperBallotResultCreator
end
component.settings(:global) do |settings|
settings.attribute :scopes_enabled, type: :boolean, default: false
settings.attribute :scope_id, type: :scope
settings.attribute :workflow, type: :enum, default: "one", choices: -> { Decidim::Budgets.workflows.keys.map(&:to_s) }
settings.attribute :projects_per_page, type: :integer, default: 12
settings.attribute :vote_rule_threshold_percent_enabled, type: :boolean, default: true
settings.attribute :vote_threshold_percent, type: :integer, default: 70
settings.attribute :vote_threshold_percent, type: :integer, default: 70
settings.attribute :vote_rule_minimum_budget_projects_enabled, type: :boolean, default: false
settings.attribute :vote_minimum_budget_projects_number, type: :integer, default: 1
settings.attribute :vote_rule_selected_projects_enabled, type: :boolean, default: false
settings.attribute :vote_selected_projects_minimum, type: :integer, default: 0
settings.attribute :vote_selected_projects_maximum, type: :integer, default: 1
settings.attribute :comments_enabled, type: :boolean, default: true
settings.attribute :comments_max_length, type: :integer, required: false
settings.attribute :resources_permissions_enabled, type: :boolean, default: true
settings.attribute :announcement, type: :text, translated: true, editor: true
settings.attribute :landing_page_content, type: :text, translated: true, editor: true
settings.attribute :more_information_modal, type: :text, translated: true
end
component.settings(:step) do |settings|
settings.attribute :comments_blocked, type: :boolean, default: false
settings.attribute :votes, type: :enum, default: "enabled", choices: %w(disabled enabled finished)
settings.attribute :show_votes, type: :boolean, default: false
settings.attribute :announcement, type: :text, translated: true, editor: true
settings.attribute :landing_page_content, type: :text, translated: true, editor: true
settings.attribute :more_information_modal, type: :text, translated: true
settings.attribute :announcement, type: :text, translated: true, editor: true
end
component.seeds do |participatory_space|
landing_page_content = Decidim::Faker::Localized.localized do
"<h2>#{::Faker::Lorem.sentence}</h2>" \
"<p>#{::Faker::Lorem.paragraph}</p>" \
"<p>#{::Faker::Lorem.paragraph}</p>"
end
component = Decidim::Component.create!(
name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :budgets).i18n_name,
manifest_name: :budgets,
published_at: Time.current,
participatory_space: participatory_space,
settings: {
landing_page_content: landing_page_content,
more_information_modal: Decidim::Faker::Localized.paragraph(sentence_count: 4),
workflow: %w(one random all).sample
}
)
rand(1...3).times do
Decidim::Budgets::Budget.create!(
component: component,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end,
total_budget: Faker::Number.number(digits: 8)
)
end
Decidim::Budgets::Budget.where(component: component).each do |budget|
rand(2...4).times do
project = Decidim::Budgets::Project.create!(
budget: budget,
scope: participatory_space.organization.scopes.sample,
category: participatory_space.categories.sample,
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end,
budget_amount: Faker::Number.number(digits: 8)
)
attachment_collection = Decidim::AttachmentCollection.create!(
name: Decidim::Faker::Localized.word,
description: Decidim::Faker::Localized.sentence(word_count: 5),
collection_for: project
)
Decidim::Attachment.create!(
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.sentence(word_count: 5),
attachment_collection: attachment_collection,
attached_to: project,
content_type: "application/pdf",
file: ActiveStorage::Blob.create_after_upload!(
io: File.open(File.join(__dir__, "seeds", "Exampledocument.pdf")),
filename: "Exampledocument.pdf",
content_type: "application/pdf",
metadata: nil
)
)
Decidim::Attachment.create!(
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.sentence(word_count: 5),
attached_to: project,
content_type: "image/jpeg",
file: ActiveStorage::Blob.create_after_upload!(
io: File.open(File.join(__dir__, "seeds", "city.jpeg")),
filename: "city.jpeg",
content_type: "image/jpeg",
metadata: nil
)
)
Decidim::Attachment.create!(
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.sentence(word_count: 5),
attached_to: project,
content_type: "application/pdf",
file: ActiveStorage::Blob.create_after_upload!(
io: File.open(File.join(__dir__, "seeds", "Exampledocument.pdf")),
filename: "Exampledocument.pdf",
content_type: "application/pdf",
metadata: nil
)
)
Decidim::Comments::Seed.comments_for(project)
end
end
end
end