Module: Easy::ReferenceData

Defined in:
lib/easy/reference_data/railtie.rb,
lib/easy/reference_data/refresh.rb,
lib/easy/reference_data/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/easy/reference_data/refresh.rb', line 3

def self.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes)
  record = clazz.where(unique_attribute_symbol => unique_attribute_value).first

  if record.nil?
    record = clazz.new
    record.send "#{unique_attribute_symbol}=", unique_attribute_value
  end

  attributes.each_pair do |key, value|
    record.send "#{key}=", value
  end

  if record.new_record?
    puts "..creating #{clazz}(#{unique_attribute_value})"
  elsif record.changed?
    puts "..updating #{clazz}(#{unique_attribute_value})"
  end

  begin
    record.save!
  rescue
    puts "Save failed for #{record.class}[#{unique_attribute_symbol}: #{unique_attribute_value}] with attributes #{attributes.inspect}"
    raise
  end

  record
end