class ContributorsAssociation

Attributes

owner[R]

Public Class Methods

new(owner, reflection) click to toggle source
# File lib/contributors_association.rb, line 8
def initialize(owner, reflection)
  @owner = owner
  @reflection = reflection
end

Public Instance Methods

<<(*records) click to toggle source
# File lib/contributors_association.rb, line 18
def <<(*records)
  result = true
  load_target unless loaded?
  records.flatten.each do |record|
    raise_on_type_mismatch(record)
    add_record_to_target_with_callbacks(record) do |r|
      result &&= insert_record(record)
    end
  end
  # return self so you can do method chaining (e.g. assoc << record1 << record2 )
  result && self
end
build(args={}) click to toggle source
# File lib/contributors_association.rb, line 38
def build args={}
  p = klass.create!(args)
  insert_record(p)
end
delete(record) click to toggle source
# File lib/contributors_association.rb, line 44
def delete record
  owner.send(field_name).delete(record.as_rdf_object)
  load_target
end
loaded?() click to toggle source
# File lib/contributors_association.rb, line 14
def loaded?
  @loaded
end
to_a()
Alias for: to_ary
to_ary() click to toggle source
# File lib/contributors_association.rb, line 31
def to_ary
  load_target unless loaded?
  @target
end
Also aliased as: to_a

Protected Instance Methods

add_record_to_target_with_callbacks(record) { |record| ... } click to toggle source

TODO after upgrade to AF 7 we can remove this method

# File lib/contributors_association.rb, line 70
def add_record_to_target_with_callbacks(record)
  #  callback(:before_add, record)
  yield(record) if block_given?
  @target ||= [] unless loaded?
  if index = @target.index(record)
    @target[index] = record
  else
     @target << record
  end
#  callback(:after_add, record)
#  set_inverse_instance(record, @owner)
  record
end
field_name() click to toggle source
# File lib/contributors_association.rb, line 90
def field_name
  :creator
end
insert_record(record) click to toggle source
# File lib/contributors_association.rb, line 51
def insert_record(record)
  predicate = owner.config_for_term_or_uri(field_name).predicate
  owner.graph.insert([owner.rdf_subject, predicate, record.as_rdf_object ])
  owner.reset_child_cache!
  @target << record
end
klass() click to toggle source
# File lib/contributors_association.rb, line 94
def klass
  Person
end
load_target() click to toggle source
# File lib/contributors_association.rb, line 84
def load_target
  pids = ActiveFedora::Base.pids_from_uris(owner.send(field_name).map(&:to_s))
  @loaded = true
  @target = klass.find(pids)
end
raise_on_type_mismatch(record) click to toggle source

TODO after upgrading to AF 7, we can extend ActiveFedora::Associations::Association and remove this method Raises ActiveFedora::AssociationTypeMismatch unless record is of the kind of the class of the associated objects. Meant to be used as a sanity check when you are about to assign an associated record.

# File lib/contributors_association.rb, line 62
def raise_on_type_mismatch(record)
  unless record.is_a?(@reflection.klass) || record.is_a?(@reflection.class_name.constantize)
    message = "#{@reflection.class_name}(##{@reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
    raise ActiveFedora::AssociationTypeMismatch, message
  end
end