module BuiltInData::ClassMethods

Public Instance Methods

built_in_object_id(key) click to toggle source

cached database id for fixture files

# File lib/built_in_data.rb, line 28
def built_in_object_id(key)
  built_in_object_ids[key]
end
delete_all() click to toggle source
Calls superclass method
# File lib/built_in_data.rb, line 32
def delete_all
  @built_in_object_ids = nil
  super
end
load_built_in_data!(hash = nil) click to toggle source
# File lib/built_in_data.rb, line 12
def load_built_in_data!(hash = nil)
  objects_hash = prepare_objects_hash(hash)
  Array.new.tap do |updated_objects|

    objects_hash.each do |key, attributes|
      updated_objects << create_or_update!(key, attributes)
    end

    # destroy any built_in objects that have been removed from built_in_data_attributes
    self.built_in.each do |object|
      object.destroy unless objects_hash.has_key?(object.built_in_key)
    end
  end
end

Private Instance Methods

built_in_object_ids() click to toggle source

memoized hash of built in object ids

# File lib/built_in_data.rb, line 60
def built_in_object_ids
  @built_in_object_ids ||= Hash.new do |hash, key|
    hash[key] = where(built_in_key: key).pluck(:id).first
  end
end
create_or_update!(key, attributes) click to toggle source
# File lib/built_in_data.rb, line 52
def create_or_update!(key, attributes)
  find_or_initialize_by(built_in_key: key).tap do |object|
    object.attributes = attributes
    object.save!
  end
end
load_yaml_data() click to toggle source
# File lib/built_in_data.rb, line 43
def load_yaml_data
  # allow a standard key to be used for doing defaults in YAML
  YAML.load(read_and_erb_process_yaml_file).except('DEFAULTS')
end
prepare_objects_hash(hash) click to toggle source
# File lib/built_in_data.rb, line 39
def prepare_objects_hash(hash)
  return hash.nil? ? load_yaml_data : hash.with_indifferent_access
end
read_and_erb_process_yaml_file() click to toggle source
# File lib/built_in_data.rb, line 48
def read_and_erb_process_yaml_file
  ERB.new(File.read(Rails.root.join('db', 'built_in_data', "#{table_name}.yml"))).result
end