Class: ActiveNode::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/active_node/reflection.rb

Overview

Holds all the meta-data about an association as it was specified in the Active Record class.

Direct Known Subclasses

ThroughReflection

Instance Attribute Summary

Attributes inherited from MacroReflection

#macro, #model, #name, #options, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #class_name, #direction, #klass, #type

Constructor Details

#initialize(*args) ⇒ AssociationReflection

:nodoc: Returns the target association’s class.

class Author < ActiveRecord::Base
  has_many :books
end

Author.reflect_on_association(:books).klass
# => Book

Note: Do not call klass.new or klass.create to instantiate a new association object. Use build_association or create_association instead. This allows plugins to hook into association object creation. def klass

@klass ||= model.send(:compute_type, class_name)

end



161
162
163
164
# File 'lib/active_node/reflection.rb', line 161

def initialize(*args)
  super
  @collection = [:has_many].include?(macro)
end

Instance Method Details

#association_classObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/active_node/reflection.rb', line 206

def association_class
  case macro
    when :has_many
      if options[:through]
        Associations::HasManyThroughAssociation
      else
        Associations::HasManyAssociation
      end
    when :has_one
      if options[:through]
        Associations::HasOneThroughAssociation
      else
        Associations::HasOneAssociation
      end
  end
end

#build_association(attributes, &block) ⇒ Object

Returns a new, unsaved instance of the associated class. attributes will be passed to the class’s constructor.



168
169
170
# File 'lib/active_node/reflection.rb', line 168

def build_association(attributes, &block)
  klass.new(attributes, &block)
end

#chainObject

A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.



182
183
184
# File 'lib/active_node/reflection.rb', line 182

def chain
  [self]
end

#collection?Boolean

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

Returns:

  • (Boolean)


189
190
191
# File 'lib/active_node/reflection.rb', line 189

def collection?
  @collection
end

#source_reflectionObject



176
177
178
# File 'lib/active_node/reflection.rb', line 176

def source_reflection
  nil
end

#through_reflectionObject



172
173
174
# File 'lib/active_node/reflection.rb', line 172

def through_reflection
  nil
end

#validate?Boolean

Returns whether or not the association should be validated as part of the parent’s validation.

Unless you explicitly disable validation with validate: false, validation will take place when:

  • you explicitly enable validation; validate: true

  • you use autosave; autosave: true

  • the association is a has_many association

Returns:

  • (Boolean)


202
203
204
# File 'lib/active_node/reflection.rb', line 202

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end