Class: ActiveFedora::Reflection::ClassMethods::AssociationReflection

Inherits:
MacroReflection
  • Object
show all
Defined in:
lib/active_fedora/reflection.rb

Overview

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

Constant Summary collapse

VALID_AUTOMATIC_INVERSE_MACROS =
[:has_many, :has_and_belongs_to_many, :belongs_to]
INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:conditions, :through, :polymorphic, :foreign_key]

Instance Attribute Summary

Attributes inherited from MacroReflection

#active_fedora, #macro, #name, #options

Instance Method Summary collapse

Methods inherited from MacroReflection

#belongs_to?, #class_name, #collection?, #klass

Constructor Details

#initialize(macro, name, options, active_fedora) ⇒ AssociationReflection

:nodoc:



145
146
147
148
# File 'lib/active_fedora/reflection.rb', line 145

def initialize(macro, name, options, active_fedora)
  super
  @collection = [:has_many, :has_and_belongs_to_many].include?(macro)
end

Instance Method Details

#association_classObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/active_fedora/reflection.rb', line 196

def association_class
  case macro
  when :belongs_to
    Associations::BelongsToAssociation
  when :has_and_belongs_to_many
    Associations::HasAndBelongsToManyAssociation
  when :has_many
    Associations::HasManyAssociation
  end
end

#build_association(*options) ⇒ Object

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



153
154
155
# File 'lib/active_fedora/reflection.rb', line 153

def build_association(*options)
  klass.new(*options)
end

#chainObject

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



170
171
172
# File 'lib/active_fedora/reflection.rb', line 170

def chain
  [self]
end

#create_association(*options) ⇒ Object

Creates a new instance of the associated class, and immediately saves it with ActiveFedora::Base#save. options will be passed to the class’s creation method. Returns the newly created object.



160
161
162
# File 'lib/active_fedora/reflection.rb', line 160

def create_association(*options)
  klass.create(*options)
end

#foreign_keyObject



164
165
166
# File 'lib/active_fedora/reflection.rb', line 164

def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end

#inverse_ofObject



176
177
178
179
180
# File 'lib/active_fedora/reflection.rb', line 176

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass.reflect_on_association inverse_name
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)


192
193
194
# File 'lib/active_fedora/reflection.rb', line 192

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