Class: Hydra::AccessControl::CollectionRelationship

Inherits:
Object
  • Object
show all
Defined in:
app/models/hydra/access_control.rb

Overview

This is like a has_many :through relationship

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ CollectionRelationship

Returns a new instance of CollectionRelationship.



47
48
49
50
# File 'app/models/hydra/access_control.rb', line 47

def initialize(owner, reflection)
  @owner = owner
  @relationship = @owner.send(reflection)
end

Instance Method Details

#build(attributes) ⇒ Object

adds one to the target.



75
76
77
78
79
# File 'app/models/hydra/access_control.rb', line 75

def build(attributes)
  @relationship.build(attributes) do |record|
    record.access_to = @owner.owner
  end
end

#create(attributes) ⇒ Object



81
82
83
# File 'app/models/hydra/access_control.rb', line 81

def create(attributes)
  build(attributes).tap(&:save!)
end

#find(id) ⇒ Object

TODO: if directly_contained relationships supported find, we could just delegate find.



65
66
67
68
69
70
71
72
# File 'app/models/hydra/access_control.rb', line 65

def find(id)
  return to_a.find { |record| record.id == id } if @relationship.loaded?

  unless id.start_with?(@owner.id)
    raise ArgumentError, "requested ACL (#{id}) is not a member of #{@owner.id}"
  end
  ActiveFedora::Base.find(id)
end

#replace(*args) ⇒ Object



85
86
87
# File 'app/models/hydra/access_control.rb', line 85

def replace(*args)
  @relationship.replace(*args)
end

#resetObject

The graph stored in @owner is now stale, so reload it and clear all caches



53
54
55
56
57
# File 'app/models/hydra/access_control.rb', line 53

def reset
  @owner.reload
  @relationship.proxy_association.reload
  self
end