Class: ActiveRecord::Associations::AssociationReflection
- Defined in:
- lib/reactive_record/active_record/associations.rb
Instance Attribute Summary collapse
-
#association_foreign_key ⇒ Object
readonly
Returns the value of attribute association_foreign_key.
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#macro ⇒ Object
readonly
Returns the value of attribute macro.
-
#owner_class ⇒ Object
readonly
Returns the value of attribute owner_class.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #collection? ⇒ Boolean
- #find_inverse ⇒ Object
-
#initialize(owner_class, macro, name, options = {}) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse ⇒ Object
- #inverse_of ⇒ Object
- #klass ⇒ Object
- #source_associations ⇒ Object
- #through_association ⇒ Object (also: #through_association?)
- #through_associations ⇒ Object
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, = {}) owner_class.reflect_on_all_associations << self @owner_class = owner_class @macro = macro @options = @klass_name = [: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 = [:foreign_key] || (macro == :belongs_to && "#{name}_id") || "#{@owner_class.name.underscore}_id" @source = [:source] || @klass_name.underscore if [:through] @attribute = name end |
Instance Attribute Details
#association_foreign_key ⇒ Object (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 |
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
26 27 28 |
# File 'lib/reactive_record/active_record/associations.rb', line 26 def attribute @attribute end |
#macro ⇒ Object (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_class ⇒ Object (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 |
#source ⇒ Object (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
102 103 104 |
# File 'lib/reactive_record/active_record/associations.rb', line 102 def collection? [:has_many].include? @macro end |
#find_inverse ⇒ Object
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 |
#inverse ⇒ Object
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_of ⇒ Object
82 83 84 |
# File 'lib/reactive_record/active_record/associations.rb', line 82 def inverse_of @inverse_of ||= inverse.attribute end |
#klass ⇒ Object
98 99 100 |
# File 'lib/reactive_record/active_record/associations.rb', line 98 def klass @klass ||= Object.const_get(@klass_name) end |
#source_associations ⇒ Object
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_association ⇒ Object 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_associations ⇒ Object
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 |