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.
Instance Method Summary collapse
- #collection? ⇒ Boolean
-
#initialize(owner_class, macro, name, options = {}) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse_of ⇒ Object
- #klass ⇒ Object
Constructor Details
#initialize(owner_class, macro, name, options = {}) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/reactive_record/active_record/associations.rb', line 30 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" @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 |
Instance Method Details
#collection? ⇒ Boolean
59 60 61 |
# File 'lib/reactive_record/active_record/associations.rb', line 59 def collection? [:has_many].include? @macro end |
#inverse_of ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/reactive_record/active_record/associations.rb', line 44 def inverse_of unless @options[:through] or @inverse_of inverse_association = klass.reflect_on_all_associations.detect do | association | association.association_foreign_key == @association_foreign_key and association.klass == @owner_class and association.attribute != attribute and klass == association.owner_class end raise "Association #{@owner_class}.#{attribute} (foreign_key: #{@association_foreign_key}) has no inverse in #{@klass_name}" unless inverse_association @inverse_of = inverse_association.attribute end @inverse_of end |