Class: Decidim::Debates::Seeds

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



10
11
12
# File 'lib/decidim/debates/seeds.rb', line 10

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



8
9
10
# File 'lib/decidim/debates/seeds.rb', line 8

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



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
93
94
95
96
97
98
99
100
101
# File 'lib/decidim/debates/seeds.rb', line 14

def call
  admin_user = Decidim::User.find_by(
    organization: participatory_space.organization,
    email: "admin@example.org"
  )

  user = Decidim::User.find_by(
    organization: participatory_space.organization,
    email: "user@example.org"
  )

  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :debates).i18n_name,
    manifest_name: :debates,
    published_at: Time.current,
    participatory_space:
  }

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

  5.times do |x|
    finite = x != 2
    if finite
      start_time = [rand(1..20).weeks.from_now, rand(1..20).weeks.ago].sample
      end_time = start_time + [rand(1..4).hours, rand(1..20).days].sample
    else
      start_time = nil
      end_time = nil
    end
    params = {
      component:,
      category: participatory_space.categories.sample,
      title: Decidim::Faker::Localized.sentence(word_count: 2),
      description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
        Decidim::Faker::Localized.paragraph(sentence_count: 3)
      end,
      instructions: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
        Decidim::Faker::Localized.paragraph(sentence_count: 3)
      end,
      start_time:,
      end_time:,
      author: component.organization
    }

    debate = Decidim.traceability.create!(
      Decidim::Debates::Debate,
      admin_user,
      params,
      visibility: "all"
    )

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

  closed_debate = Decidim::Debates::Debate.last
  closed_debate.conclusions = Decidim::Faker::Localized.wrapped("<p>", "</p>") do
    Decidim::Faker::Localized.paragraph(sentence_count: 3)
  end
  closed_debate.closed_at = Time.current
  closed_debate.save!

  params = {
    component:,
    category: participatory_space.categories.sample,
    title: Decidim::Faker::Localized.sentence(word_count: 2),
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 3)
    end,
    instructions: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.paragraph(sentence_count: 3)
    end,
    author: user
  }

  Decidim.traceability.create!(
    Decidim::Debates::Debate,
    user,
    params,
    visibility: "all"
  )
end