Class: Decidim::Comments::Seed

Inherits:
Object
  • Object
show all
Defined in:
decidim-comments/app/models/decidim/comments/seed.rb

Overview

A comment can belong to many Commentable models. This class is responsible to Seed those models in order to be able to use them in the development app.

Class Method Summary collapse

Class Method Details

.comments_for(resource) ⇒ Object

Public: adds a random amount of comments for a given resource.

resource - the resource to add the coments to.

Returns nothing.



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
# File 'decidim-comments/app/models/decidim/comments/seed.rb', line 14

def self.comments_for(resource)
  return unless resource.accepts_new_comments?

  Decidim::Comments::Comment.reset_column_information

  organization = resource.organization

  2.times do
    author = Decidim::User.where(organization:).all.sample
    user_group = [true, false].sample ? Decidim::UserGroups::ManageableUserGroups.for(author).verified.sample : nil

    params = {
      commentable: resource,
      root_commentable: resource,
      body: { en: ::Faker::Lorem.sentence(word_count: 50) },
      author:,
      user_group:
    }

    Decidim.traceability.create!(
      Decidim::Comments::Comment,
      author,
      params,
      visibility: "public-only"
    )
  end
end