module ActiveScaffold::ReverseAssociation::AssociationReflection

Public Class Methods

included(base) click to toggle source
# File lib/active_scaffold/extensions/reverse_associations.rb, line 42
def self.included(base)
  base.send :include, ActiveScaffold::ReverseAssociation::CommonMethods
end

Protected Instance Methods

reverse_matches(klass) click to toggle source
# File lib/active_scaffold/extensions/reverse_associations.rb, line 48
def reverse_matches(klass)
  reverse_matches = []

  # collect associations that point back to this model and use the same foreign_key
  klass.reflect_on_all_associations.each do |assoc|
    next if assoc == self
    # skip over has_many :through associations
    next if assoc.options[:through]
    next unless assoc.options[:polymorphic] || assoc.class_name == active_record.name

    case [assoc.macro, macro].find_all { |m| m == :has_and_belongs_to_many }.length
      # if both are a habtm, then match them based on the join table
      when 2
        next unless assoc.options[:join_table] == options[:join_table]

      # if only one is a habtm, they do not match
      when 1
        next

      # otherwise, match them based on the foreign_key
      when 0
        if assoc.foreign_key.is_a? Array
          next unless assoc.foreign_key == foreign_key
        else
          next unless assoc.foreign_key.to_sym == foreign_key.to_sym
        end
    end

    reverse_matches << assoc
  end
  reverse_matches
end