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.



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, 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"
  @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

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/reactive_record/active_record/associations.rb', line 59

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

#inverse_ofObject



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

#klassObject



55
56
57
# File 'lib/reactive_record/active_record/associations.rb', line 55

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