Class: ActiveFedora::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- 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
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
- #association_class ⇒ Object
-
#chain ⇒ Object
A chain of reflections from this one back to the owner.
- #check_validity! ⇒ Object
- #check_validity_of_inverse! ⇒ Object
-
#create_association(*options) ⇒ Object
Creates a new instance of the associated class, and immediately saves it with ActiveFedora::Base#save.
- #foreign_key ⇒ Object
- #has_inverse? ⇒ Boolean
-
#initialize(macro, name, options, active_fedora) ⇒ AssociationReflection
constructor
:nodoc:.
- #inverse_of ⇒ Object
-
#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
#belongs_to?, #build_association, #class_name, #collection?, #has_and_belongs_to_many?, #has_many?, #klass
Constructor Details
#initialize(macro, name, options, active_fedora) ⇒ AssociationReflection
:nodoc:
167 168 169 170 |
# File 'lib/active_fedora/reflection.rb', line 167 def initialize(macro, name, , active_fedora) super @collection = [:has_many, :has_and_belongs_to_many, :directly_contains, :indirectly_contains].include?(macro) end |
Instance Method Details
#association_class ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/active_fedora/reflection.rb', line 242 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 |
#chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
212 213 214 |
# File 'lib/active_fedora/reflection.rb', line 212 def chain [self] end |
#check_validity! ⇒ Object
198 199 200 |
# File 'lib/active_fedora/reflection.rb', line 198 def check_validity! check_validity_of_inverse! end |
#check_validity_of_inverse! ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/active_fedora/reflection.rb', line 202 def check_validity_of_inverse! unless [:polymorphic] if has_inverse? && inverse_of.nil? raise InverseOfAssociationNotFoundError.new(self) end end 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.
175 176 177 |
# File 'lib/active_fedora/reflection.rb', line 175 def create_association(*) klass.create(*) end |
#foreign_key ⇒ Object
179 180 181 |
# File 'lib/active_fedora/reflection.rb', line 179 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key end |
#has_inverse? ⇒ Boolean
218 219 220 |
# File 'lib/active_fedora/reflection.rb', line 218 def has_inverse? inverse_name end |
#inverse_of ⇒ Object
222 223 224 225 226 |
# File 'lib/active_fedora/reflection.rb', line 222 def inverse_of return unless inverse_name @inverse_of ||= klass.reflect_on_association inverse_name end |
#predicate ⇒ Object
Returns the RDF predicate as defined by the :predicate option
184 185 186 |
# File 'lib/active_fedora/reflection.rb', line 184 def predicate [:predicate] end |
#predicate_for_solr ⇒ Object
188 189 190 |
# File 'lib/active_fedora/reflection.rb', line 188 def predicate_for_solr predicate.fragment || predicate.to_s.rpartition(/\//).last end |
#solr_key ⇒ Object
192 193 194 195 196 |
# File 'lib/active_fedora/reflection.rb', line 192 def solr_key @solr_key ||= begin ActiveFedora::SolrQueryBuilder.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
238 239 240 |
# File 'lib/active_fedora/reflection.rb', line 238 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |