Module: ActiveRecord::Remote::Helpers::AssociationHelper

Included in:
Base
Defined in:
lib/active_record/remote/helpers/association_helper.rb

Instance Method Summary collapse

Instance Method Details

#association_klass(name) ⇒ Object



48
49
50
51
52
# File 'lib/active_record/remote/helpers/association_helper.rb', line 48

def association_klass(name)
  singular = name.to_s.singularize
  parent_module = to_s.split('::')[0..-2].join('::').constantize
  parent_module.const_get(singular.classify)
end

#has_many(association, options = {}) ⇒ Object



5
6
7
8
9
# File 'lib/active_record/remote/helpers/association_helper.rb', line 5

def has_many(association, options = {})
  association_name = parse_association_name(association, options)
  set_inflection(association, options)
  self.attribute association, Array[association_klass(association_name)], options
end

#parse_association_name(association, options = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/active_record/remote/helpers/association_helper.rb', line 11

def parse_association_name(association, options = {})
  if options[:collection].present?
    options[:collection]
  else
    association
  end
end

#set_inflection(association, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record/remote/helpers/association_helper.rb', line 19

def set_inflection(association, options)
  return if options[:collection].nil?
  # since RLM has an irregular API we have to adjust the inflections
  # so we can have children collections that do not match the parents
  # i.e.
  # <DETAILS>
  #   <LINE>
  # instead of default behavior
  # <DETAILS>
  #   <DETAIL>
  #
  # the code below dynamically adjusts active_support inflections
  # here is a basic example
  #
  # ActiveSupport::Inflector.inflections do |inflect|
  #   inflect.singular 'DETAILS', 'LINE'
  # end
  if options[:strict]
    assoc_name = association.to_s
    collection = options[:collection].to_s
  else
    assoc_name = association.to_s.upcase
    collection = options[:collection].to_s.upcase
  end
  ActiveSupport::Inflector.inflections do |inflect|
    inflect.singular assoc_name, collection
  end
end