class Seeder

Constants

VERSION

Attributes

data[R]
keys[R]
model[R]

Public Class Methods

create(data, keys, model) click to toggle source
# File lib/seeder.rb, line 4
def self.create(data, keys, model)
  new(data, keys, model).create
end
new(data, keys, model) click to toggle source
# File lib/seeder.rb, line 8
def initialize(data, keys, model)
  @keys = keys.map(&:to_sym)
  @data = data.map(&:symbolize_keys)
  @model = model
end

Public Instance Methods

create() click to toggle source
# File lib/seeder.rb, line 14
def create
  model.transaction do
    delete_outdated_records
    update_existing_records
    create_new_records
  end
end
create_new_records() click to toggle source
# File lib/seeder.rb, line 37
def create_new_records
  new_keys = data_keys_hash.keys - existing_records_keys_hash.keys

  data_keys_hash.values_at(*new_keys).each do |attributes|
    model.create!(attributes)
  end
end
delete_outdated_records() click to toggle source
# File lib/seeder.rb, line 22
def delete_outdated_records
  return unless records_to_delete.present?
  model.where(id: records_to_delete).delete_all
end
update_existing_records() click to toggle source
# File lib/seeder.rb, line 27
def update_existing_records
  existing_records_keys_hash.each do |record_keys, record|
    attributes = data_keys_hash[record_keys]
    next unless attributes

    record.attributes = attributes
    record.save! if record.changed?
  end
end

Private Instance Methods

data_keys_hash() click to toggle source
# File lib/seeder.rb, line 47
def data_keys_hash
  @data_keys_hash ||= data.inject({}) do |hash, attributes|
    hash.merge!(attributes.values_at(*keys) => attributes)
  end
end
existing_records_keys_hash() click to toggle source
# File lib/seeder.rb, line 53
def existing_records_keys_hash
  @existing_records_keys_hash ||= model.all.inject({}) do |hash, record|
    record_keys = keys.map { |key| record.public_send(key) }
    hash.merge!(record_keys => record)
  end
end
records_to_delete() click to toggle source
# File lib/seeder.rb, line 60
def records_to_delete
  @records_to_delete ||= begin
    keys_to_delete = existing_records_keys_hash.keys - data_keys_hash.keys
    existing_records_keys_hash.values_at(*keys_to_delete)
  end
end