Class: Decidim::Proposals::Seeds
- Inherits:
-
Seeds
- Object
- Seeds
- Decidim::Proposals::Seeds
- Defined in:
- lib/decidim/proposals/seeds.rb
Instance Attribute Summary collapse
-
#participatory_space ⇒ Object
readonly
Returns the value of attribute participatory_space.
Instance Method Summary collapse
- #call ⇒ Object
- #create_collaborative_draft!(component:) ⇒ Object
- #create_component! ⇒ Object
- #create_emendation!(proposal:) ⇒ Object
- #create_proposal!(component:) ⇒ Object
- #create_proposal_notes!(proposal:) ⇒ Object
- #create_proposal_votes!(proposal:, emendation: nil) ⇒ Object
-
#initialize(participatory_space:) ⇒ Seeds
constructor
A new instance of Seeds.
- #organization ⇒ Object
- #random_coauthor ⇒ Object
- #random_email(suffix:) ⇒ Object
- #random_nickname ⇒ Object
- #random_state_answer ⇒ Object
- #update_traceability!(component:) ⇒ Object
Constructor Details
#initialize(participatory_space:) ⇒ Seeds
Returns a new instance of Seeds.
11 12 13 |
# File 'lib/decidim/proposals/seeds.rb', line 11 def initialize(participatory_space:) @participatory_space = participatory_space end |
Instance Attribute Details
#participatory_space ⇒ Object (readonly)
Returns the value of attribute participatory_space.
9 10 11 |
# File 'lib/decidim/proposals/seeds.rb', line 9 def participatory_space @participatory_space end |
Instance Method Details
#call ⇒ Object
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 |
# File 'lib/decidim/proposals/seeds.rb', line 15 def call component = create_component! Decidim::Proposals.create_default_states!(component, admin_user) 5.times do |n| proposal = create_proposal!(component:) emendation = create_emendation!(proposal:) if proposal.state.nil? (n % 3).times do |_m| create_proposal_votes!(proposal:, emendation:) end (n % 3).times do create_proposal_notes!(proposal:) end Decidim::Comments::Seed.comments_for(proposal) create_collaborative_draft!(component:) end update_traceability!(component:) create_report!(reportable: Decidim::Proposals::Proposal.take, current_user: Decidim::User.take) hide_report!(reportable: Decidim::Proposals::Proposal.take) end |
#create_collaborative_draft!(component:) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/decidim/proposals/seeds.rb', line 238 def create_collaborative_draft!(component:) n = rand(5) state = if n > 3 "published" elsif n > 2 "withdrawn" else "open" end = Decidim::User.where(organization:).all.sample draft = Decidim.traceability.perform_action!("create", Decidim::Proposals::CollaborativeDraft, ) do draft = Decidim::Proposals::CollaborativeDraft.new( component:, category: participatory_space.categories.sample, scope: random_scope(participatory_space:), title: ::Faker::Lorem.sentence(word_count: 2), body: ::Faker::Lorem.paragraphs(number: 2).join("\n"), state:, published_at: Time.current ) draft..build(author: participatory_space.organization) draft.save! draft end case n when 2 = Decidim::User.where(organization:).all.sample(5) .each do || Decidim::Coauthorship.create(coauthorable: draft, author: ) end when 3 = Decidim::User.where(organization:).all.sample Decidim::Coauthorship.create(coauthorable: draft, author: ) end Decidim::Comments::Seed.comments_for(draft) end |
#create_component! ⇒ Object
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 |
# File 'lib/decidim/proposals/seeds.rb', line 48 def create_component! step_settings = if participatory_space.allows_steps? { participatory_space.active_step.id => { votes_enabled: true, votes_blocked: false, creation_enabled: true } } else {} end params = { name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :proposals).i18n_name, manifest_name: :proposals, published_at: Time.current, participatory_space:, settings: { vote_limit: 0, attachments_allowed: [true, false].sample, collaborative_drafts_enabled: true }, step_settings: } Decidim.traceability.perform_action!( "publish", Decidim::Component, admin_user, visibility: "all" ) do Decidim::Component.create!(params) end end |
#create_emendation!(proposal:) ⇒ Object
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 213 214 215 216 217 218 219 |
# File 'lib/decidim/proposals/seeds.rb', line 165 def create_emendation!(proposal:) = find_or_initialize_user_by(email: random_email(suffix: "amendment")) group = Decidim::UserGroup.create!( name: ::Faker::Name.name, nickname: random_nickname, email: ::Faker::Internet.email, extended_data: { document_number: ::Faker::Code.isbn, phone: ::Faker::PhoneNumber.phone_number, verified_at: Time.current }, organization:, confirmed_at: Time.current ) Decidim::UserGroupMembership.create!( user: , role: "creator", user_group: group ) params = { component: proposal.component, category: participatory_space.categories.sample, scope: random_scope(participatory_space:), title: { en: "#{proposal.title["en"]} #{::Faker::Lorem.sentence(word_count: 1)}" }, body: { en: "#{proposal.body["en"]} #{::Faker::Lorem.sentence(word_count: 3)}" }, proposal_state: Decidim::Proposals::ProposalState.where(component: proposal.component, token: :evaluating).first, answer: nil, answered_at: Time.current, published_at: Time.current } emendation = Decidim.traceability.perform_action!( "create", Decidim::Proposals::Proposal, , visibility: "public-only" ) do emendation = Decidim::Proposals::Proposal.new(params) emendation.(, user_group: .user_groups.first) emendation.save! emendation end Decidim::Amendment.create!( amender: , amendable: proposal, emendation:, state: "evaluating" ) emendation end |
#create_proposal!(component:) ⇒ Object
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 |
# File 'lib/decidim/proposals/seeds.rb', line 78 def create_proposal!(component:) proposal_state, answer, state_published_at = random_state_answer proposal_state = Decidim::Proposals::ProposalState.where(component:, token: proposal_state).first params = { component:, category: participatory_space.categories.sample, scope: random_scope(participatory_space:), title: { en: ::Faker::Lorem.sentence(word_count: 2) }, body: { en: ::Faker::Lorem.paragraphs(number: 2).join("\n") }, proposal_state:, answer:, answered_at: proposal_state.present? ? Time.current : nil, state_published_at:, published_at: Time.current } proposal = Decidim.traceability.perform_action!( "publish", Decidim::Proposals::Proposal, admin_user, visibility: "all" ) do proposal = Decidim::Proposals::Proposal.new(params) = proposal.() proposal.save! Decidim::EventsManager.publish( event: "decidim.events.proposals.proposal_published_for_space", event_class: Decidim::Proposals::PublishProposalEvent, resource: proposal, followers: proposal.participatory_space.followers ) proposal end (attached_to: proposal, filename: "city.jpeg") if component.settings. proposal end |
#create_proposal_notes!(proposal:) ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/decidim/proposals/seeds.rb', line 228 def create_proposal_notes!(proposal:) = Decidim::User.where(organization:, admin: true).all.sample Decidim::Proposals::ProposalNote.create!( proposal:, author: , body: ::Faker::Lorem.paragraphs(number: 2).join("\n") ) end |
#create_proposal_votes!(proposal:, emendation: nil) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/decidim/proposals/seeds.rb', line 221 def create_proposal_votes!(proposal:, emendation: nil) = find_or_initialize_user_by(email: random_email(suffix: "vote")) Decidim::Proposals::ProposalVote.create!(proposal:, author:) unless proposal.published_state? && proposal.rejected? Decidim::Proposals::ProposalVote.create!(proposal: emendation, author:) if emendation end |
#organization ⇒ Object
44 45 46 |
# File 'lib/decidim/proposals/seeds.rb', line 44 def organization @organization ||= participatory_space.organization end |
#random_coauthor ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/decidim/proposals/seeds.rb', line 137 def n = rand(5) n = 3 if n == 2 && !Decidim.module_installed?(:meetings) case n when 0 Decidim::User.where(organization:).sample when 1 Decidim::UserGroup.where(organization:).sample when 2 meeting_component = participatory_space.components.find_by(manifest_name: "meetings") Decidim::Meetings::Meeting.where(component: meeting_component).sample else organization end end |
#random_email(suffix:) ⇒ Object
159 160 161 162 163 |
# File 'lib/decidim/proposals/seeds.rb', line 159 def random_email(suffix:) r = SecureRandom.hex(4) "#{suffix}-author-#{participatory_space.underscored_name}-#{participatory_space.id}-#{r}@example.org" end |
#random_nickname ⇒ Object
155 156 157 |
# File 'lib/decidim/proposals/seeds.rb', line 155 def random_nickname "#{::Faker::Twitter.unique.screen_name}-#{SecureRandom.hex(4)}"[0, 20] end |
#random_state_answer ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/decidim/proposals/seeds.rb', line 121 def random_state_answer n = rand(5) if n > 3 [:accepted, Decidim::Faker::Localized.sentence(word_count: 10), Time.current] elsif n > 2 [:rejected, nil, Time.current] elsif n > 1 [:evaluating, nil, Time.current] elsif n.positive? [:accepted, Decidim::Faker::Localized.sentence(word_count: 10), nil] else [:not_answered, nil, nil] end end |
#update_traceability!(component:) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/decidim/proposals/seeds.rb', line 278 def update_traceability!(component:) Decidim.traceability.update!( Decidim::Proposals::CollaborativeDraft.all.sample, Decidim::User.where(organization:).all.sample, component:, category: participatory_space.categories.sample, scope: random_scope(participatory_space:), title: ::Faker::Lorem.sentence(word_count: 2), body: ::Faker::Lorem.paragraphs(number: 2).join("\n") ) end |