Class: DiscourseDev::Record
- Inherits:
-
Object
- Object
- DiscourseDev::Record
show all
- Defined in:
- lib/discourse_dev/record.rb
Constant Summary
collapse
- DEFAULT_COUNT =
30.freeze
- AUTO_POPULATED =
"auto_populated"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, count = DEFAULT_COUNT) ⇒ Record
Returns a new instance of Record.
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/discourse_dev/record.rb', line 14
def initialize(model, count = DEFAULT_COUNT)
@@initialized ||=
begin
Faker::Discourse.unique.clear
RateLimiter.disable
true
end
@model = model
@type = model.to_s.downcase.to_sym
@count = count
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
12
13
14
|
# File 'lib/discourse_dev/record.rb', line 12
def model
@model
end
|
#type ⇒ Object
Returns the value of attribute type.
12
13
14
|
# File 'lib/discourse_dev/record.rb', line 12
def type
@type
end
|
Class Method Details
.populate!(**args) ⇒ Object
71
72
73
|
# File 'lib/discourse_dev/record.rb', line 71
def self.populate!(**args)
self.new(**args).populate!
end
|
.random(model, use_existing_records: true) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/discourse_dev/record.rb', line 75
def self.random(model, use_existing_records: true)
if !use_existing_records && model.new.respond_to?(:custom_fields)
model.joins(:_custom_fields).where(
"#{model.to_s.underscore}_custom_fields.name = '#{AUTO_POPULATED}'",
)
end
count = model.count
raise "#{model} records are not yet populated" if count == 0
offset = Faker::Number.between(from: 0, to: count - 1)
model.offset(offset).first
end
|
Instance Method Details
#create! {|record| ... } ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/discourse_dev/record.rb', line 27
def create!
record = model.create!(data)
record.custom_fields[AUTO_POPULATED] = true if record.respond_to?(:custom_fields)
yield(record) if block_given?
DiscourseEvent.trigger(:after_create_dev_record, record, type)
record
end
|
#current_count ⇒ Object
67
68
69
|
# File 'lib/discourse_dev/record.rb', line 67
def current_count
model.count
end
|
#populate!(ignore_current_count: false) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/discourse_dev/record.rb', line 35
def populate!(ignore_current_count: false)
unless Discourse.allow_dev_populate?
raise 'To run this rake task in a production site, set the value of `ALLOW_DEV_POPULATE` environment variable to "1"'
end
unless ignore_current_count || @ignore_current_count
if current_count >= @count
puts "Already have #{current_count} #{type} records"
Rake.application.top_level_tasks.each { |task_name| Rake::Task[task_name].reenable }
Rake::Task["dev:repopulate"].invoke
return
elsif current_count > 0
@count -= current_count
puts "There are #{current_count} #{type} records. Creating #{@count} more."
else
puts "Creating #{@count} sample #{type} records"
end
end
records = []
@count.times do
records << create!
putc "." unless type == :post
end
puts unless type == :post
DiscourseEvent.trigger(:after_populate_dev_records, records, type)
records
end
|