Class: ActiveFedora::Reflection::AssociationReflection

Inherits:
MacroReflection 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].freeze
INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:conditions, :through, :polymorphic, :foreign_key].freeze

Instance Attribute Summary collapse

Attributes inherited from MacroReflection

#active_fedora, #name, #options, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #autosave=, #compute_class, #klass

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #class_name, #constraints, #through_reflection?

Constructor Details

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

Returns a new instance of AssociationReflection.



267
268
269
270
# File 'lib/active_fedora/reflection.rb', line 267

def initialize(name, scope, options, active_fedora)
  super
  @constructable = calculate_constructable(macro, options)
end

Instance Attribute Details

#parent_reflectionObject

:nodoc:



265
266
267
# File 'lib/active_fedora/reflection.rb', line 265

def parent_reflection
  @parent_reflection
end

Instance Method Details

#association_classObject

Raises:

  • (NotImplementedError)


353
354
355
# File 'lib/active_fedora/reflection.rb', line 353

def association_class
  raise NotImplementedError
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


361
362
363
# File 'lib/active_fedora/reflection.rb', line 361

def belongs_to?
  false
end

#check_validity!Object



300
301
302
# File 'lib/active_fedora/reflection.rb', line 300

def check_validity!
  check_validity_of_inverse!
end

#check_validity_of_inverse!Object



304
305
306
307
# File 'lib/active_fedora/reflection.rb', line 304

def check_validity_of_inverse!
  return if options[:polymorphic] || !(has_inverse? && inverse_of.nil?)
  raise InverseOfAssociationNotFoundError, self
end

#collect_join_chainObject Also known as: chain

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



311
312
313
# File 'lib/active_fedora/reflection.rb', line 311

def collect_join_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)


336
337
338
# File 'lib/active_fedora/reflection.rb', line 336

def collection?
  false
end

#constructable?Boolean

:nodoc:

Returns:

  • (Boolean)


272
273
274
# File 'lib/active_fedora/reflection.rb', line 272

def constructable? # :nodoc:
  @constructable
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.



279
280
281
# File 'lib/active_fedora/reflection.rb', line 279

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

#foreign_keyObject



283
284
285
# File 'lib/active_fedora/reflection.rb', line 283

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

#has_and_belongs_to_many?Boolean

Returns:

  • (Boolean)


369
370
371
# File 'lib/active_fedora/reflection.rb', line 369

def has_and_belongs_to_many?
  false
end

#has_inverse?Boolean

Returns:

  • (Boolean)


316
317
318
# File 'lib/active_fedora/reflection.rb', line 316

def has_inverse?
  inverse_name
end

#has_many?Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/active_fedora/reflection.rb', line 365

def has_many?
  false
end

#inverse_ofObject



320
321
322
323
324
# File 'lib/active_fedora/reflection.rb', line 320

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass._reflect_on_association inverse_name
end

#macroObject

Returns the macro type.

has_many :clients returns :has_many

Raises:

  • (NotImplementedError)


329
330
331
# File 'lib/active_fedora/reflection.rb', line 329

def macro
  raise NotImplementedError
end

#predicateObject

Returns the RDF predicate as defined by the :predicate option



288
289
290
# File 'lib/active_fedora/reflection.rb', line 288

def predicate
  options[:predicate]
end

#predicate_for_solrObject



292
293
294
# File 'lib/active_fedora/reflection.rb', line 292

def predicate_for_solr
  predicate.fragment || predicate.to_s.rpartition(/\//).last
end

#solr_keyObject



296
297
298
# File 'lib/active_fedora/reflection.rb', line 296

def solr_key
  @solr_key ||= ActiveFedora.index_field_mapper.solr_name(predicate_for_solr, :symbol)
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)


349
350
351
# File 'lib/active_fedora/reflection.rb', line 349

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