Module: Ardm::ActiveRecord::Associations

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ardm/active_record/associations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_options(klass, options, *keep) ⇒ Object

Convert options from DM style to AR style.

Keep any unknown keys to use as conditions.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ardm/active_record/associations.rb', line 24

def self.convert_options(klass, options, *keep)
  keep += [:class_name, :foreign_key]

  ar = options.dup
  ar[:class_name]  = ar.delete(:model)      if ar[:model]
  ar[:foreign_key] = ar.delete(:child_key)  if ar[:child_key]
  ar[:foreign_key] = ar[:foreign_key].first if ar[:foreign_key].respond_to?(:to_ary)

  if ar[:foreign_key] && property = klass.properties[ar[:foreign_key]]
    ar[:foreign_key] = property.field
  end

  if (conditions = ar.slice!(*keep)).any?
    ar[:conditions] = conditions
  end
  ar
end

Instance Method Details

#assign_attributes(attrs, *a) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ardm/active_record/associations.rb', line 8

def assign_attributes(attrs, *a)
  new_attrs = attrs.inject({}) do |memo,(k,v)|
    if assoc = self.class.reflect_on_association(k)
      memo[assoc.foreign_key] = v && v.send(v.class.primary_key)
    else
      memo[k] = v
    end
    memo
  end
  super new_attrs, *a
end