Module: Ardm::Ar::Associations

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

Defined Under Namespace

Modules: ClassMethods

Class 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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ardm/ar/associations.rb', line 11

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[:source]      = ar.delete(:via)        if ar[:via]
  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 Ardm.rails3?
    if (conditions = ar.slice!(*keep)).any?
      ar[:conditions] = conditions
    end
    [ar]
  else
    order = ar.delete(:order)
    conditions = ar.slice!(*keep)
    # sigh
    block = if conditions.any? && order
              lambda { where(conditions).order(order) }
            elsif conditions.any?
              lambda { where(conditions) }
            elsif order
              lambda { order(order) }
            end
    [block, ar].compact
  end
end