Sha256: 69a1bd77cfbd6a1bca02131eaa8472c2095952c87f363aba413b97538badd47d
Contents?: true
Size: 939 Bytes
Versions: 2
Compression:
Stored size: 939 Bytes
Contents
require 'rails' require 'active_record' require 'seed_box/version' require 'seed_box/railtie' module SeedBox def seed(model, find_or_create_by, update_with = {}) record = model.where(find_or_create_by).first_or_initialize record.update_attributes(update_with) handle_errors(record, find_or_create_by, update_with) end def seed_once(model, find_or_create_by, create_with = {}) record = model.where(find_or_create_by).first_or_initialize(create_with) record.save handle_errors(record, find_or_create_by, create_with) end def seed_file(name) File.new(Rails.root.join('db/seeds').join(name)) end private 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 end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
seed_box-0.2.0 | lib/seed_box.rb |
seed_box-0.1.0 | lib/seed_box.rb |