Module: ActiveScaffold::ReverseAssociation::CommonMethods
- Defined in:
- lib/active_scaffold/extensions/reverse_associations.rb
Class Method Summary collapse
Instance Method Summary collapse
- #autodetect_inverse(klass = nil) ⇒ Object
- #inverse_for?(klass) ⇒ Boolean
- #inverse_of_with_autodetect ⇒ Object
- #reverse(klass = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 |
# File 'lib/active_scaffold/extensions/reverse_associations.rb', line 4 def self.included(base) base.class_eval { attr_writer :reverse } base.alias_method_chain :inverse_of, :autodetect end |
Instance Method Details
#autodetect_inverse(klass = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_scaffold/extensions/reverse_associations.rb', line 25 def autodetect_inverse(klass = nil) return nil if klass.nil? && [:polymorphic] klass ||= self.klass # name-based matching (association name vs self.active_record.to_s) matches = reverse_matches(klass) if matches.length > 1 matches.find_all do |assoc| active_record.to_s.underscore.include? assoc.name.to_s.pluralize.singularize end end matches.first end |
#inverse_for?(klass) ⇒ Boolean
13 14 15 16 |
# File 'lib/active_scaffold/extensions/reverse_associations.rb', line 13 def inverse_for?(klass) inverse_class = inverse_of.try(:active_record) inverse_class.present? && (inverse_class == klass || klass < inverse_class) end |
#inverse_of_with_autodetect ⇒ Object
9 10 11 |
# File 'lib/active_scaffold/extensions/reverse_associations.rb', line 9 def inverse_of_with_autodetect inverse_of_without_autodetect || autodetect_inverse end |
#reverse(klass = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/active_scaffold/extensions/reverse_associations.rb', line 18 def reverse(klass = nil) unless defined? @reverse # rubocop:disable Style/IfUnlessModifier @reverse ||= inverse_of.try(:name) end @reverse || (autodetect_inverse(klass).try(:name) unless klass.nil?) end |