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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/reactive_record/active_record/associations.rb', line 42

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.sub(/s$/, '')) || name.camelize
  @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.



36
37
38
# File 'lib/reactive_record/active_record/associations.rb', line 36

def association_foreign_key
  @association_foreign_key
end

#attributeObject (readonly)

Returns the value of attribute attribute.



37
38
39
# File 'lib/reactive_record/active_record/associations.rb', line 37

def attribute
  @attribute
end

#macroObject (readonly)

Returns the value of attribute macro.



38
39
40
# File 'lib/reactive_record/active_record/associations.rb', line 38

def macro
  @macro
end

#owner_classObject (readonly)

Returns the value of attribute owner_class.



39
40
41
# File 'lib/reactive_record/active_record/associations.rb', line 39

def owner_class
  @owner_class
end

#sourceObject (readonly)

Returns the value of attribute source.



40
41
42
# File 'lib/reactive_record/active_record/associations.rb', line 40

def source
  @source
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/reactive_record/active_record/associations.rb', line 109

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

#find_inverseObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/reactive_record/active_record/associations.rb', line 93

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



84
85
86
87
# File 'lib/reactive_record/active_record/associations.rb', line 84

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

#inverse_ofObject



89
90
91
# File 'lib/reactive_record/active_record/associations.rb', line 89

def inverse_of
  @inverse_of ||= inverse.attribute
end

#klassObject



105
106
107
# File 'lib/reactive_record/active_record/associations.rb', line 105

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

#source_associationsObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/reactive_record/active_record/associations.rb', line 73

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?



53
54
55
56
57
58
59
60
61
# File 'lib/reactive_record/active_record/associations.rb', line 53

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



65
66
67
68
69
70
71
# File 'lib/reactive_record/active_record/associations.rb', line 65

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