Class: DiscourseDev::Category

Inherits:
Record
  • Object
show all
Defined in:
lib/discourse_dev/category.rb

Constant Summary

Constants inherited from Record

Record::AUTO_POPULATED, Record::DEFAULT_COUNT

Instance Attribute Summary

Attributes inherited from Record

#model, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#current_count, #populate!, populate!

Constructor Details

#initializeCategory

Returns a new instance of Category.



9
10
11
12
# File 'lib/discourse_dev/category.rb', line 9

def initialize
  super(::Category, DiscourseDev.config.category[:count])
  @parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
end

Class Method Details

.randomObject



55
56
57
# File 'lib/discourse_dev/category.rb', line 55

def self.random
  super(::Category)
end

Instance Method Details

#create!Object



46
47
48
49
50
51
52
53
# File 'lib/discourse_dev/category.rb', line 46

def create!
  super do |category|
    category.set_permissions(permissions)
    category.save!

    @parent_category_ids << category.id if category.parent_category_id.blank?
  end
end

#dataObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/discourse_dev/category.rb', line 14

def data
  name = Faker::Discourse.unique.category
  parent_category_id = nil

  if Faker::Boolean.boolean(true_ratio: 0.6)
    offset = Faker::Number.between(from: 0, to: @parent_category_ids.count - 1)
    parent_category_id = @parent_category_ids[offset]
    @permissions = ::Category.find(parent_category_id).permissions_params.presence
  else
    @permissions = nil
  end

  {
    name: name,
    description: Faker::Lorem.paragraph,
    user_id: ::Discourse::SYSTEM_USER_ID,
    color: Faker::Color.hex_color.last(6),
    parent_category_id: parent_category_id,
  }
end

#permissionsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/discourse_dev/category.rb', line 35

def permissions
  return @permissions if @permissions.present?
  return { everyone: :full } if Faker::Boolean.boolean(true_ratio: 0.75)

  permission = {}
  group = Group.random
  permission[group.id] = Faker::Number.between(from: 1, to: 3)

  permission
end