Class: ActiveFedora::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- AbstractReflection
- MacroReflection
- ActiveFedora::Reflection::AssociationReflection
- 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
BasicContainsReflection, BelongsToReflection, DirectlyContainsOneReflection, DirectlyContainsReflection, FilterReflection, HasAndBelongsToManyReflection, HasManyReflection, HasSubresourceReflection, IndirectlyContainsReflection, OrdersReflection, 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
-
#parent_reflection ⇒ Object
:nodoc:.
Attributes inherited from MacroReflection
#active_fedora, #name, #options, #scope
Instance Method Summary collapse
- #association_class ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. - #check_validity! ⇒ Object
- #check_validity_of_inverse! ⇒ Object
-
#collect_join_chain ⇒ Object
(also: #chain)
A chain of reflections from this one back to the owner.
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
-
#constructable? ⇒ Boolean
:nodoc:.
-
#create_association(*options) ⇒ Object
Creates a new instance of the associated class, and immediately saves it with ActiveFedora::Base#save.
- #foreign_key ⇒ Object
- #has_and_belongs_to_many? ⇒ Boolean
- #has_inverse? ⇒ Boolean
- #has_many? ⇒ Boolean
-
#initialize(name, scope, options, active_fedora) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse_of ⇒ Object
-
#macro ⇒ Object
Returns the macro type.
-
#predicate ⇒ Object
Returns the RDF predicate as defined by the :predicate option.
- #predicate_for_solr ⇒ Object
- #solr_key ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
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.
268 269 270 271 |
# File 'lib/active_fedora/reflection.rb', line 268 def initialize(name, scope, , active_fedora) super @constructable = calculate_constructable(macro, ) end |
Instance Attribute Details
#parent_reflection ⇒ Object
:nodoc:
266 267 268 |
# File 'lib/active_fedora/reflection.rb', line 266 def parent_reflection @parent_reflection end |
Instance Method Details
#association_class ⇒ Object
356 357 358 |
# File 'lib/active_fedora/reflection.rb', line 356 def association_class raise NotImplementedError end |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
364 365 366 |
# File 'lib/active_fedora/reflection.rb', line 364 def belongs_to? false end |
#check_validity! ⇒ Object
303 304 305 |
# File 'lib/active_fedora/reflection.rb', line 303 def check_validity! check_validity_of_inverse! end |
#check_validity_of_inverse! ⇒ Object
307 308 309 310 |
# File 'lib/active_fedora/reflection.rb', line 307 def check_validity_of_inverse! return if [:polymorphic] || !(has_inverse? && inverse_of.nil?) raise InverseOfAssociationNotFoundError, self end |
#collect_join_chain ⇒ Object Also known as: chain
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
314 315 316 |
# File 'lib/active_fedora/reflection.rb', line 314 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.
339 340 341 |
# File 'lib/active_fedora/reflection.rb', line 339 def collection? false end |
#constructable? ⇒ Boolean
:nodoc:
273 274 275 |
# File 'lib/active_fedora/reflection.rb', line 273 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.
280 281 282 |
# File 'lib/active_fedora/reflection.rb', line 280 def create_association(*) klass.create(*) end |
#foreign_key ⇒ Object
284 285 286 |
# File 'lib/active_fedora/reflection.rb', line 284 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key end |
#has_and_belongs_to_many? ⇒ Boolean
372 373 374 |
# File 'lib/active_fedora/reflection.rb', line 372 def has_and_belongs_to_many? false end |
#has_inverse? ⇒ Boolean
319 320 321 |
# File 'lib/active_fedora/reflection.rb', line 319 def has_inverse? inverse_name end |
#has_many? ⇒ Boolean
368 369 370 |
# File 'lib/active_fedora/reflection.rb', line 368 def has_many? false end |
#inverse_of ⇒ Object
323 324 325 326 327 |
# File 'lib/active_fedora/reflection.rb', line 323 def inverse_of return unless inverse_name @inverse_of ||= klass._reflect_on_association inverse_name end |
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
332 333 334 |
# File 'lib/active_fedora/reflection.rb', line 332 def macro raise NotImplementedError end |
#predicate ⇒ Object
Returns the RDF predicate as defined by the :predicate option
289 290 291 |
# File 'lib/active_fedora/reflection.rb', line 289 def predicate [:predicate] end |
#predicate_for_solr ⇒ Object
293 294 295 |
# File 'lib/active_fedora/reflection.rb', line 293 def predicate_for_solr predicate.fragment || predicate.to_s.rpartition(/\//).last end |
#solr_key ⇒ Object
297 298 299 300 301 |
# File 'lib/active_fedora/reflection.rb', line 297 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
352 353 354 |
# File 'lib/active_fedora/reflection.rb', line 352 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |