module SeedBox

Constants

VERSION

Public Instance Methods

seed(model, find_or_create_by, update_with = {}) click to toggle source
# File lib/seed_box.rb, line 8
def seed(model, find_or_create_by, update_with = {})
  record = model.where(find_or_create_by).first_or_initialize

  record.update(update_with)

  handle_errors(record, find_or_create_by, update_with)
end
seed_file(name) click to toggle source
# File lib/seed_box.rb, line 22
def seed_file(name)
  File.new(Rails.root.join('db/seeds').join(name))
end
seed_once(model, find_or_create_by, create_with = {}) click to toggle source
# File lib/seed_box.rb, line 16
def seed_once(model, find_or_create_by, create_with = {})
  return if model.where(find_or_create_by).exists?

  seed(model, find_or_create_by, create_with)
end

Private Instance Methods

handle_errors(record, find_or_create_by, update_with) click to toggle source
# File lib/seed_box.rb, line 28
def handle_errors(record, find_or_create_by, update_with)
  return record if record.valid?

  raise "Couldn't save #{record.class} " +
    "(#{record.errors.full_messages.join(', ')}) with provided data: " +
    "#{find_or_create_by.inspect}, #{update_with.inspect}"
end