Sha256: 6feabbdb4b94fde79eddbd457bd560071d2ebe15c8e12640f218a39ff308ba2b

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

# frozen_string_literal: true

require 'discourse_dev'
require 'rails'
require 'faker'

module DiscourseDev
  class Record
    DEFAULT_COUNT = 30.freeze

    attr_reader :model, :type

    def initialize(model, count = DEFAULT_COUNT)
      Faker::Discourse.unique.clear
      @model = model
      @type = model.to_s
      @count = count
      @index = nil
    end

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

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

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

      puts
    end

    def index
      @index
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
discourse_dev-0.0.4 lib/discourse_dev/record.rb