Class: Decidim::Budgets::Seeds

Inherits:
Seeds
  • Object
show all
Defined in:
lib/decidim/budgets/seeds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



11
12
13
# File 'lib/decidim/budgets/seeds.rb', line 11

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



9
10
11
# File 'lib/decidim/budgets/seeds.rb', line 9

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/decidim/budgets/seeds.rb', line 15

def call
  component = create_component!

  number_of_records.times do
    create_budget!(component:)
  end

  Decidim::Budgets::Budget.where(component:).each do |budget|
    number_of_records.times do
      project = create_project!(budget:)

      create_attachments!(attached_to: project)

      Decidim::Comments::Seed.comments_for(project)
    end
  end
end

#create_budget!(component:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/decidim/budgets/seeds.rb', line 72

def create_budget!(component:)
  Decidim.traceability.perform_action!(
    "create",
    Decidim::Budgets::Budget,
    admin_user
  ) do
    Decidim::Budgets::Budget.create!(
      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: 7)
    )
  end
end

#create_component!Object



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
# File 'lib/decidim/budgets/seeds.rb', line 33

def create_component!
  landing_page_content = Decidim::Faker::Localized.localized do
    "<h2>#{::Faker::Lorem.sentence}</h2>" \
      "<p>#{::Faker::Lorem.paragraph}</p>" \
      "<p>#{::Faker::Lorem.paragraph}</p>"
  end

  step_settings = if participatory_space.allows_steps?
                    { participatory_space.active_step.id => {
                      votes: %w(enabled disabled finished).sample
                    } }
                  else
                    {}
                  end

  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :budgets).i18n_name,
    manifest_name: :budgets,
    published_at: Time.current,
    participatory_space:,
    settings: {
      geocoding_enabled: [true, false].sample,
      landing_page_content:,
      more_information_modal: Decidim::Faker::Localized.paragraph(sentence_count: 4),
      workflow: Decidim::Budgets.workflows.keys.sample
    },
    step_settings:
  }

  Decidim.traceability.perform_action!(
    "publish",
    Decidim::Component,
    admin_user,
    visibility: "all"
  ) do
    Decidim::Component.create!(params)
  end
end

#create_project!(budget:) ⇒ Object



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
# File 'lib/decidim/budgets/seeds.rb', line 89

def create_project!(budget:)
  minimum_amount = Integer(budget.total_budget * 0.1)
  maximum_amount = Integer(budget.total_budget * 0.5)
  params = {
    budget:,
    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.between(from: minimum_amount, to: maximum_amount)
  }

  if budget.component.settings.geocoding_enabled?
    params = params.merge(
      address: "#{::Faker::Address.street_address} #{::Faker::Address.zip} #{::Faker::Address.city}",
      latitude: ::Faker::Address.latitude,
      longitude: ::Faker::Address.longitude
    )
  end

  Decidim.traceability.perform_action!(
    "create",
    Decidim::Budgets::Project,
    admin_user
  ) do
    Decidim::Budgets::Project.create!(params)
  end
end