Module: ActiveRecord::Remote::Helpers::AssociationHelper
- Included in:
- Base
- Defined in:
- lib/active_record/remote/helpers/association_helper.rb
Instance Method Summary collapse
- #association_klass(name) ⇒ Object
- #has_many(association, options = {}) ⇒ Object
- #parse_association_name(association, options = {}) ⇒ Object
- #set_inflection(association, options) ⇒ Object
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, = {}) association_name = parse_association_name(association, ) set_inflection(association, ) self.attribute association, Array[association_klass(association_name)], 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, = {}) if [:collection].present? [: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, ) return if [: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 [:strict] assoc_name = association.to_s collection = [:collection].to_s else assoc_name = association.to_s.upcase collection = [:collection].to_s.upcase end ActiveSupport::Inflector.inflections do |inflect| inflect.singular assoc_name, collection end end |