module ActiveScaffold::ReverseAssociation::CommonMethods

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

autodetect_inverse(klass = nil) click to toggle source
# File lib/active_scaffold/extensions/reverse_associations.rb, line 25
def autodetect_inverse(klass = nil)
  return nil if klass.nil? && options[: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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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