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]
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?, #has_and_belongs_to_many?, #has_many?, #klass

Constructor Details

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

:nodoc:


166
167
168
169
# File 'lib/active_fedora/reflection.rb', line 166

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

Instance Method Details

#association_classObject


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/active_fedora/reflection.rb', line 245

def association_class
  case macro
  when :contains
    Associations::ContainsAssociation
  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
  end
end

#build_association(*options) ⇒ Object

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


174
175
176
# File 'lib/active_fedora/reflection.rb', line 174

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.


215
216
217
# File 'lib/active_fedora/reflection.rb', line 215

def chain
  [self]
end

#check_validity!Object


201
202
203
# File 'lib/active_fedora/reflection.rb', line 201

def check_validity!
  check_validity_of_inverse!
end

#check_validity_of_inverse!Object


205
206
207
208
209
210
211
# File 'lib/active_fedora/reflection.rb', line 205

def check_validity_of_inverse!
  unless options[: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.


181
182
183
# File 'lib/active_fedora/reflection.rb', line 181

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

#foreign_keyObject


185
186
187
# File 'lib/active_fedora/reflection.rb', line 185

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

#has_inverse?Boolean

Returns:

  • (Boolean)

221
222
223
# File 'lib/active_fedora/reflection.rb', line 221

def has_inverse?
  inverse_name
end

#inverse_ofObject


225
226
227
228
229
# File 'lib/active_fedora/reflection.rb', line 225

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


190
191
192
# File 'lib/active_fedora/reflection.rb', line 190

def predicate
  options[:predicate]
end

#solr_keyObject


194
195
196
197
198
199
# File 'lib/active_fedora/reflection.rb', line 194

def solr_key
  @solr_key ||= begin
    predicate_string = predicate.fragment || predicate.to_s.rpartition(/\//).last
    ActiveFedora::SolrQueryBuilder.solr_name(predicate_string, :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)

241
242
243
# File 'lib/active_fedora/reflection.rb', line 241

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