Class: Decidim::Blogs::Seeds

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/blogs/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/blogs/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/blogs/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
# File 'lib/decidim/blogs/seeds.rb', line 14

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

  step_settings = if participatory_space.allows_steps?
                    { participatory_space.active_step.id => { comments_enabled: true, comments_blocked: false } }
                  else
                    {}
                  end

  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :blogs).i18n_name,
    manifest_name: :blogs,
    published_at: Time.current,
    participatory_space:,
    settings: {
      vote_limit: 0
    },
    step_settings:
  }

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

  6.times do |n|
    author = if n >= 3
               Decidim::User.where(organization: component.organization).sample
             elsif n <= 1
               Decidim::UserGroup.where(organization: component.organization).sample
             else
               component.organization
             end

    params = {
      component:,
      title: Decidim::Faker::Localized.sentence(word_count: 5),
      body: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
        Decidim::Faker::Localized.paragraph(sentence_count: 20)
      end,
      author:
    }

    post = Decidim.traceability.create!(
      Decidim::Blogs::Post,
      author,
      params,
      visibility: "all"
    )

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