Class: Decidim::Initiatives::Seeds

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
  organization = Decidim::Organization.first

  Decidim::ContentBlock.create(
    organization:,
    weight: 33,
    scope_name: :homepage,
    manifest_name: :highlighted_initiatives,
    published_at: Time.current
  )

  3.times do |n|
    type = Decidim::InitiativesType.create!(
      title: Decidim::Faker::Localized.sentence(word_count: 5),
      description: Decidim::Faker::Localized.sentence(word_count: 25),
      organization:,
      banner_image: ::Faker::Boolean.boolean(true_ratio: 0.5) ? banner_image : nil # Keep after organization
    )

    organization.top_scopes.each do |scope|
      Decidim::InitiativesTypeScope.create(
        type:,
        scope:,
        supports_required: (n + 1) * 1000
      )
    end
  end

  Decidim::Initiative.states.keys.each do |state|
    Decidim::Initiative.skip_callback(:save, :after, :notify_state_change, raise: false)
    Decidim::Initiative.skip_callback(:create, :after, :notify_creation, raise: false)

    params = {
      title: Decidim::Faker::Localized.sentence(word_count: 3),
      description: Decidim::Faker::Localized.sentence(word_count: 25),
      scoped_type: Decidim::InitiativesTypeScope.all.sample,
      state:,
      signature_type: "online",
      signature_start_date: Date.current - 7.days,
      signature_end_date: Date.current + 7.days,
      published_at: 7.days.ago,
      author: Decidim::User.all.sample,
      organization:
    }

    initiative = Decidim.traceability.perform_action!(
      "publish",
      Decidim::Initiative,
      organization.users.first,
      visibility: "all"
    ) do
      Decidim::Initiative.create!(params)
    end
    initiative.add_to_index_as_search_resource

    if %w(published rejected accepted).include? state
      users = []
      rand(50).times do
        author = (Decidim::User.all - users).sample
        initiative.votes.create!(author:, scope: initiative.scope, hash_id: SecureRandom.hex)
        users << author
      end
    end

    Decidim::Comments::Seed.comments_for(initiative)

    create_attachment(attached_to: initiative, filename: "city.jpeg")

    Decidim::Initiatives.default_components.each do |component_name|
      component = Decidim::Component.create!(
        name: Decidim::Components::Namer.new(initiative.organization.available_locales, component_name).i18n_name,
        manifest_name: component_name,
        published_at: Time.current,
        participatory_space: initiative
      )

      next unless component_name.in? ["pages", :pages]

      Decidim::Pages::CreatePage.call(component) do
        on(:invalid) { raise "Cannot create page" }
      end
    end
  end
end