Class: ActiveRecord::Associations::AssociationReflection

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive_record/active_record/associations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_class, macro, name, options = {}) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reactive_record/active_record/associations.rb', line 31

def initialize(owner_class, macro, name, options = {})
  owner_class.reflect_on_all_associations << self
  @owner_class = owner_class
  @macro =       macro
  @options =     options
  @klass_name =  options[:class_name] || (collection? && name.camelize.gsub(/s$/, '')) || name.camelize
  if @klass_name < ActiveRecord::Base
    @klass = @klass_name
    @klass_name = @klass_name.name
  end rescue nil
  @association_foreign_key = options[:foreign_key] || (macro == :belongs_to && "#{name}_id") || "#{@owner_class.name.underscore}_id"
  @source = options[:source] || @klass_name.underscore if options[:through]
  @attribute = name
end

Instance Attribute Details

#association_foreign_keyObject (readonly)

Returns the value of attribute association_foreign_key.



25
26
27
# File 'lib/reactive_record/active_record/associations.rb', line 25

def association_foreign_key
  @association_foreign_key
end

#attributeObject (readonly)

Returns the value of attribute attribute.



26
27
28
# File 'lib/reactive_record/active_record/associations.rb', line 26

def attribute
  @attribute
end

#macroObject (readonly)

Returns the value of attribute macro.



27
28
29
# File 'lib/reactive_record/active_record/associations.rb', line 27

def macro
  @macro
end

#owner_classObject (readonly)

Returns the value of attribute owner_class.



28
29
30
# File 'lib/reactive_record/active_record/associations.rb', line 28

def owner_class
  @owner_class
end

#sourceObject (readonly)

Returns the value of attribute source.



29
30
31
# File 'lib/reactive_record/active_record/associations.rb', line 29

def source
  @source
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/reactive_record/active_record/associations.rb', line 102

def collection?
  [:has_many].include? @macro
end

#find_inverseObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reactive_record/active_record/associations.rb', line 86

def find_inverse
  klass.reflect_on_all_associations.each do |association|
    next if association.association_foreign_key != @association_foreign_key
    next if association.klass != @owner_class
    next if association.attribute == attribute
    return association if klass == association.owner_class
  end
  raise "Association #{@owner_class}.#{attribute} "\
        "(foreign_key: #{@association_foreign_key}) "\
        "has no inverse in #{@klass_name}"
end

#inverseObject



77
78
79
80
# File 'lib/reactive_record/active_record/associations.rb', line 77

def inverse
  @inverse ||=
    through_association ? through_association.inverse : find_inverse
end

#inverse_ofObject



82
83
84
# File 'lib/reactive_record/active_record/associations.rb', line 82

def inverse_of
  @inverse_of ||= inverse.attribute
end

#klassObject



98
99
100
# File 'lib/reactive_record/active_record/associations.rb', line 98

def klass
  @klass ||= Object.const_get(@klass_name)
end

#source_associationsObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/reactive_record/active_record/associations.rb', line 66

def source_associations
  # find all associations that use this association as the source
  # that is final all associations that are using this association as the source in a
  # through relationship
  @source_associations ||= owner_class.reflect_on_all_associations.collect do |sibling|
    sibling.klass.reflect_on_all_associations.select do |assoc|
      assoc.source == attribute
    end
  end.flatten
end

#through_associationObject Also known as: through_association?



46
47
48
49
50
51
52
53
54
# File 'lib/reactive_record/active_record/associations.rb', line 46

def through_association
  return unless @options[:through]
  @through_association ||= @owner_class.reflect_on_all_associations.detect do |association|
    association.attribute == @options[:through]
  end
  raise "Through association #{@options[:through]} for "\
        "#{@owner_class}.#{attribute} not found." unless @through_association
  @through_association
end

#through_associationsObject



58
59
60
61
62
63
64
# File 'lib/reactive_record/active_record/associations.rb', line 58

def through_associations
  # find all associations that use the inverse association as the through association
  # that is find all associations that are using this association in a through relationship
  @through_associations ||= klass.reflect_on_all_associations.select do |assoc|
    assoc.through_association && assoc.inverse == self
  end
end