Class: Decidim::CollaborativeTexts::Seeds

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



11
12
13
# File 'lib/decidim/collaborative_texts/seeds.rb', line 11

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



9
10
11
# File 'lib/decidim/collaborative_texts/seeds.rb', line 9

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/decidim/collaborative_texts/seeds.rb', line 15

def call
  component = create_component!

  number_of_records.times do
    create_document!(component:)
  end

  number_of_records.times do
    create_document!(component:, published_at: nil)
  end
end

#create_body_blocksObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/decidim/collaborative_texts/seeds.rb', line 101

def create_body_blocks
  blocks = []
  rand(3..5).times do
    blocks << "<h2>#{::Faker::Lorem.word.capitalize}</h2>"
    level = 3
    rand(1..4).times do
      blocks << "<h#{level}>#{::Faker::Lorem.word.capitalize}</h#{level}>"
      blocks << ::Faker::HTML.paragraph(sentence_count: rand(3..5))
      blocks << ::Faker::HTML.random(exclude: [:heading, :script, :table])
      level += 1
    end
  end
  blocks
end

#create_component!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/decidim/collaborative_texts/seeds.rb', line 27

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

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

#create_document!(component:, published_at: Time.current) ⇒ Object



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

def create_document!(component:, published_at: Time.current)
  body_blocks = create_body_blocks
  params = {
    component:,
    title: ::Faker::Lorem.paragraph,
    body: body_blocks.join("\n"),
    published_at:,
    accepting_suggestions: [true, false].sample,
    coauthorships: [Decidim::Coauthorship.new(author: organization)]
  }

  document = Decidim.traceability.create!(
    Decidim::CollaborativeTexts::Document,
    admin_user,
    params,
    visibility: "all"
  )

  # Create some versions
  number_of_records.times do |num|
    params = {
      document:,
      body: create_body_blocks.join("\n"),
      created_at: num.seconds.from_now
    }
    Decidim.traceability.create!(
      Decidim::CollaborativeTexts::Version,
      admin_user,
      params,
      visibility: "all"
    )
  end

  # Create some suggestions
  random_positions = (0...body_blocks.size).to_a.sample(5)
  random_positions.each do |position|
    changeset = {
      firstNode: position.to_s,
      lastNode: (position + rand(1..3)).to_s,
      replace: rand(1..4).times.map { ::Faker::HTML.paragraph(sentence_count: rand(1..3)) }
    }
    create_suggestion!(document_version: document.current_version, changeset:)
  end

  document
end

#create_suggestion!(document_version:, changeset:) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/decidim/collaborative_texts/seeds.rb', line 92

def create_suggestion!(document_version:, changeset:)
  params = {
    document_version:,
    changeset:,
    author: document_version.organization.users.sample
  }
  Decidim::CollaborativeTexts::Suggestion.create!(params)
end