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.

Direct Known Subclasses

RDFPropertyReflection

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, #macro, #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(macro, name, scope, options, active_fedora) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



252
253
254
255
# File 'lib/active_fedora/reflection.rb', line 252

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

Instance Attribute Details

#parent_reflectionObject

:nodoc:



250
251
252
# File 'lib/active_fedora/reflection.rb', line 250

def parent_reflection
  @parent_reflection
end

Instance Method Details

#association_classObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/active_fedora/reflection.rb', line 333

def association_class
  case macro
  when :contains
    Associations::BasicContainsAssociation
  when :belongs_to
    Associations::BelongsToAssociation
  when :has_and_belongs_to_many
    Associations::HasAndBelongsToManyAssociation
  when :has_many
    Associations::HasManyAssociation
  when :singular_rdf
    Associations::SingularRDF
  when :rdf
    Associations::RDF
  when :directly_contains
    Associations::DirectlyContainsAssociation
  when :directly_contains_one
    Associations::DirectlyContainsOneAssociation
  when :indirectly_contains
    Associations::IndirectlyContainsAssociation
  end
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


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

def belongs_to?
  macro == :belongs_to
end

#check_validity!Object



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

def check_validity!
  check_validity_of_inverse!
end

#check_validity_of_inverse!Object



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

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.



298
299
300
# File 'lib/active_fedora/reflection.rb', line 298

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)


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

def collection?
  [:has_many, :has_and_belongs_to_many, :directly_contains, :indirectly_contains].include?(macro)
end

#constructable?Boolean

:nodoc:

Returns:

  • (Boolean)


257
258
259
# File 'lib/active_fedora/reflection.rb', line 257

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.



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

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

#foreign_keyObject



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

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

#has_and_belongs_to_many?Boolean

Returns:

  • (Boolean)


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

def has_and_belongs_to_many?
  macro == :has_and_belongs_to_many
end

#has_inverse?Boolean

Returns:

  • (Boolean)


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

def has_inverse?
  inverse_name
end

#has_many?Boolean

Returns:

  • (Boolean)


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

def has_many?
  macro == :has_many
end

#inverse_ofObject



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

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass._reflect_on_association inverse_name
end

#predicateObject

Returns the RDF predicate as defined by the :predicate option



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

def predicate
  options[:predicate]
end

#predicate_for_solrObject



277
278
279
# File 'lib/active_fedora/reflection.rb', line 277

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

#solr_keyObject



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

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


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

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