Class: DeepFreezer::Defrost

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_freezer/defrost.rb

Class Method Summary collapse

Class Method Details

.hash_to_instance(object) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deep_freezer/defrost.rb', line 16

def self.hash_to_instance(object)
  klass = object.keys.first
  attrs = object[klass]
  instance = klass.constantize.new

  attrs.keys.each do |a|
    instance.send("#{a}=", attrs[a])
  end
  
  instance
end

.load!Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/deep_freezer/defrost.rb', line 3

def self.load!
  path = DeepFreezer::Base.fixture_path.join("**/*.yml")
  Dir.glob(path).each do |file|
    objects = YAML.load(File.read(file))
    puts "Loading #{objects.first.keys.first}"
    objects.each do |object|
      instance = self.hash_to_instance(object)
      sql = self.sql_for(instance)
      ActiveRecord::Base.connection.execute sql
    end
  end
end

.sql_for(record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/deep_freezer/defrost.rb', line 28

def self.sql_for(record)
  values = record.send(:arel_attributes_with_values_for_create, record.attribute_names)

  model = record.class
  scope = model.unscoped

  im = scope.arel.create_insert
  im.into model.arel_table

  substitutes, binds = scope.substitute_values(values)
  im.insert substitutes

  record.class.connection.to_sql(im, binds)
end