Class: DiscourseDev::Record

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

Direct Known Subclasses

Category, Group, Post, Tag, Topic, User

Constant Summary collapse

DEFAULT_COUNT =
30.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, count = DEFAULT_COUNT) ⇒ Record



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/discourse_dev/record.rb', line 13

def initialize(model, count = DEFAULT_COUNT)
  @@initialized ||= begin
    Faker::Discourse.unique.clear
    RateLimiter.disable
    true
  end

  @model = model
  @type = model.to_s
  @count = count
  @index = nil
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.populate!Object



62
63
64
# File 'lib/discourse_dev/record.rb', line 62

def self.populate!
  self.new.populate!
end

.random(model) ⇒ Object



66
67
68
69
# File 'lib/discourse_dev/record.rb', line 66

def self.random(model)
  offset = Faker::Number.between(from: 0, to: model.count - 1)
  model.offset(offset).first
end

Instance Method Details

#create! {|record| ... } ⇒ Object

Yields:

  • (record)


26
27
28
29
# File 'lib/discourse_dev/record.rb', line 26

def create!
  record = model.create!(data)
  yield(record) if block_given?
end

#current_countObject



58
59
60
# File 'lib/discourse_dev/record.rb', line 58

def current_count
  model.count
end

#indexObject



54
55
56
# File 'lib/discourse_dev/record.rb', line 54

def index
  @index
end

#populate!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/discourse_dev/record.rb', line 31

def populate!
  if current_count >= @count
    puts "Already have #{@count}+ #{type.downcase} records."

    Rake.application.top_level_tasks.each do |task_name|
      Rake::Task[task_name].reenable
    end

    Rake::Task['dev:repopulate'].invoke
    return
  end

  puts "Creating #{@count} sample #{type.downcase} records"

  @count.times do |i|
    @index = i
    create!
    putc "."
  end

  puts
end