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

def call
  create_content_block!

  3.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)
    Decidim::Initiative.skip_callback(:create, :after, :notify_creation, 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



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

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



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

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



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

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



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

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



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

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



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

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