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

def call
  create_content_block!

  number_of_records.times do |_n|
    type = create_initiative_type!

    organization.top_scopes.each do |scope|
      create_initiative_type_scope!(scope:, type:)
    end
  end

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

    initiative = create_initiative!(state:)

    create_initiative_votes!(initiative:) if %w(published rejected accepted).include? state

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

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

    Decidim::Initiatives.default_components.each do |component_name|
      create_component!(initiative:, component_name:)
    end
  end
end

#create_component!(initiative:, component_name:) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/decidim/initiatives/seeds.rb', line 103

def create_component!(initiative:, 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
  )

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

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

#create_content_block!Object



37
38
39
40
41
42
43
44
45
# File 'lib/decidim/initiatives/seeds.rb', line 37

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

#create_initiative!(state:) ⇒ Object



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 65

def create_initiative!(state:)
  published_at = %w(published rejected accepted).include?(state) ? 7.days.ago : nil

  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:,
    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

  initiative
end

#create_initiative_type!Object



47
48
49
50
51
52
53
54
# File 'lib/decidim/initiatives/seeds.rb', line 47

def create_initiative_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
  )
end

#create_initiative_type_scope!(scope:, type:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/decidim/initiatives/seeds.rb', line 56

def create_initiative_type_scope!(scope:, type:)
  n = rand(3)
  Decidim::InitiativesTypeScope.create(
    type:,
    scope:,
    supports_required: (n + 1) * 1000
  )
end

#create_initiative_votes!(initiative:) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/decidim/initiatives/seeds.rb', line 94

def create_initiative_votes!(initiative:)
  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