Class: DiscourseDev::Category

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

Constant Summary

Constants inherited from Record

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, #index, #populate!, populate!

Constructor Details

#initialize(count = DEFAULT_COUNT) ⇒ Category

Returns a new instance of Category.



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

def initialize(count = DEFAULT_COUNT)
  super(::Category, count)
  @parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
end

Class Method Details

.randomObject



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

def self.random
  super(::Category)
end

Instance Method Details

#create!Object



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

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



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

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



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

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