Class: Sow::ActiveRecord

Inherits:
Adapter show all
Defined in:
lib/seed_drill/active_record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

adapters, get

Constructor Details

#initialize(relation) ⇒ ActiveRecord


9
10
11
12
# File 'lib/seed_drill/active_record.rb', line 9

def initialize(relation)
  @rel = relation
  @klass = relation.klass
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


3
4
5
# File 'lib/seed_drill/active_record.rb', line 3

def model
  @model
end

Class Method Details

.target_nameObject


5
6
7
# File 'lib/seed_drill/active_record.rb', line 5

def self.target_name
  'ActiveRecord::Base'
end

Instance Method Details

#first_or_create(fields) ⇒ Object


14
15
16
# File 'lib/seed_drill/active_record.rb', line 14

def first_or_create(fields)
  @rel.where(fields).first || @rel.create(fields)
end

#update_associations(model, fields) ⇒ Object


22
23
24
25
26
27
28
29
# File 'lib/seed_drill/active_record.rb', line 22

def update_associations(model, fields)
  fields.each do |field, block|
    fail 'unknown association' unless associations.keys.map(&:to_sym).include?(field)
    klass = association_class(field)
    child_model = Sower.new(klass, &block).model
    model.send("#{field}=", child_model)
  end
end

#update_attributes(model, fields) ⇒ Object


18
19
20
# File 'lib/seed_drill/active_record.rb', line 18

def update_attributes(model, fields)
  model.update_attributes(fields)
end