Class: ActiveFedora::Orders::Association

Inherits:
Associations::CollectionAssociation
  • Object
show all
Defined in:
lib/active_fedora/orders/association.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Association

Returns a new instance of Association.



4
5
6
7
# File 'lib/active_fedora/orders/association.rb', line 4

def initialize(*args)
  super
  @target = find_target
end

Instance Method Details

#append_target(record, skip_callbacks = false, &block) ⇒ Object

Append a target node to the end of the order.

Parameters:

  • record (ActiveFedora::Base)

    Record to append



51
52
53
54
55
56
# File 'lib/active_fedora/orders/association.rb', line 51

def append_target(record, skip_callbacks=false, &block)
  unless unordered_association.target.include?(record)
    unordered_association.concat(record)
  end
  target.append_target(record, proxy_in: owner)
end

#delete_at(loc) ⇒ Object

Delete whatever node is at a specific position

Parameters:

  • loc (Integer)

    Position to delete



81
82
83
# File 'lib/active_fedora/orders/association.rb', line 81

def delete_at(loc)
  target.delete_at(loc)
end

#delete_record(record) ⇒ Object

Delete a list node

Parameters:



95
96
97
# File 'lib/active_fedora/orders/association.rb', line 95

def delete_record(record)
  target.delete_node(record)
end

#delete_records(records, _method = nil) ⇒ Object

Delete multiple list nodes.

Parameters:



87
88
89
90
91
# File 'lib/active_fedora/orders/association.rb', line 87

def delete_records(records, _method=nil)
  records.each do |record|
    delete_record(record)
  end
end

#find_reflectionObject



30
31
32
# File 'lib/active_fedora/orders/association.rb', line 30

def find_reflection
  reflection
end

#find_targetObject



41
42
43
# File 'lib/active_fedora/orders/association.rb', line 41

def find_target
  ordered_proxies
end

#insert_record(record, force = true, validate = true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_fedora/orders/association.rb', line 99

def insert_record(record, force=true, validate=true)
  record.save_target
  list_container.save
  # NOTE: This turns out to be pretty cheap, but should we be doing it
  # elsewhere?
  unless list_container.changed?
    owner.head = [list_container.head_id.first]
    owner.tail = [list_container.tail_id.first]
    owner.save
  end
end

#insert_target_at(loc, record) ⇒ Object

Insert a target node in a specific position

Parameters:

  • loc (Integer)

    Position to insert record.

  • record (ActiveFedora::Base)

    Record to insert



61
62
63
64
65
66
# File 'lib/active_fedora/orders/association.rb', line 61

def insert_target_at(loc, record)
  unless unordered_association.target.include?(record)
    unordered_association.concat(record)
  end
  target.insert_at(loc, record, proxy_in: owner)
end

#insert_target_id_at(loc, id) ⇒ Object

Insert a target ID in a specific position

Parameters:

  • loc (Integer)

    Position to insert record ID

  • id (String)

    ID of record to insert.



71
72
73
74
75
76
77
# File 'lib/active_fedora/orders/association.rb', line 71

def insert_target_id_at(loc, id)
  raise ArgumentError.new "ID can not be nil" if id.nil?
  unless unordered_association.ids_reader.include?(id)
    raise ArgumentError.new "#{id} is not a part of #{unordered_association.reflection.name}"
  end
  target.insert_proxy_for_at(loc, ActiveFedora::Base.id_to_uri(id), proxy_in: owner)
end

#inspectObject



9
10
11
# File 'lib/active_fedora/orders/association.rb', line 9

def inspect
  "#<ActiveFedora::Orders::Association:#{object_id}>"
end

#load_targetObject



45
46
47
# File 'lib/active_fedora/orders/association.rb', line 45

def load_target
  @target = find_target
end

#reader(*args) ⇒ Object



13
14
15
16
# File 'lib/active_fedora/orders/association.rb', line 13

def reader(*args)
  @proxy ||= ActiveFedora::Orders::CollectionProxy.new(self)
  super
end

#replace(new_ordered_list) ⇒ Object



34
35
36
37
38
# File 'lib/active_fedora/orders/association.rb', line 34

def replace(new_ordered_list)
  raise unless new_ordered_list.kind_of? ActiveFedora::Orders::OrderedList
  list_container.ordered_self = new_ordered_list
  @target = find_target
end

#scope(*args) ⇒ Object



111
112
113
# File 'lib/active_fedora/orders/association.rb', line 111

def scope(*args)
  @scope ||= ActiveFedora::Relation.new(klass)
end

#target_readerObject



26
27
28
# File 'lib/active_fedora/orders/association.rb', line 26

def target_reader
  @target_proxy ||= TargetProxy.new(self)
end

#target_writer(nodes) ⇒ Object

Meant to override all nodes with the given nodes.

Parameters:

  • nodes (Array<ActiveFedora::Base>)

    Nodes to set as ordered members



20
21
22
23
24
# File 'lib/active_fedora/orders/association.rb', line 20

def target_writer(nodes)
  target_reader.clear
  target_reader.concat(nodes)
  target_reader
end