Module: ActiveFedora::Reflection::ClassMethods

Defined in:
lib/active_fedora/reflection.rb

Instance Method Summary collapse

Instance Method Details

#add_reflection(name, reflection) ⇒ Object


24
25
26
27
# File 'lib/active_fedora/reflection.rb', line 24

def add_reflection(name, reflection)
  # FIXME this is where the problem with association_spec is caused (key is string now)
  self.reflections = self.reflections.merge(name => reflection)
end

#child_resource_reflectionsObject


43
44
45
# File 'lib/active_fedora/reflection.rb', line 43

def child_resource_reflections
  reflections.select { |_, reflection| reflection.kind_of?(AssociationReflection) && reflection.macro == :contains }
end

#create_reflection(macro, name, options, active_fedora) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_fedora/reflection.rb', line 11

def create_reflection(macro, name, options, active_fedora)
  klass = case macro
    when :has_many, :belongs_to, :has_and_belongs_to_many, :contains
      AssociationReflection
    when :rdf, :singular_rdf
      RDFPropertyReflection
  end
  reflection = klass.new(macro, name, options, active_fedora)
  add_reflection name, reflection

  reflection
end

#outgoing_reflectionsObject


39
40
41
# File 'lib/active_fedora/reflection.rb', line 39

def outgoing_reflections
  reflections.select { |_, reflection| reflection.kind_of? RDFPropertyReflection }
end

#reflect_on_all_autosave_associationsObject


62
63
64
# File 'lib/active_fedora/reflection.rb', line 62

def reflect_on_all_autosave_associations
  reflections.values.select { |reflection| reflection.options[:autosave] }
end

#reflect_on_association(association) ⇒ Object

Returns the AssociationReflection object for the association (use the symbol).

Account.reflect_on_association(:owner)             # returns the owner AssociationReflection
Invoice.reflect_on_association(:line_items).macro  # returns :has_many

52
53
54
55
56
57
58
59
60
# File 'lib/active_fedora/reflection.rb', line 52

def reflect_on_association(association)
  val = reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
  unless val
    # When a has_many is paired with a has_and_belongs_to_many the assocation will have a plural name
    association = association.to_s.pluralize.to_sym
    val = reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
  end
  val
end

#reflectionsObject

Returns a hash containing all AssociationReflection objects for the current class. Example:

Invoice.reflections
Account.reflections

35
36
37
# File 'lib/active_fedora/reflection.rb', line 35

def reflections
  read_inheritable_attribute(:reflections) || write_inheritable_attribute(:reflections, {})
end