-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can use the containers by calling: class FooHistory < ActiveFedora::Base directly_contains :files, predicate: ::RDF::URI.new("http://example.com/hasFiles"), class_name: 'Thing' end if you don't specify a class_name it will default to ActiveFedora::File
- Loading branch information
Showing
15 changed files
with
280 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lib/active_fedora/associations/builder/directly_contains.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module ActiveFedora::Associations::Builder | ||
class DirectlyContains < CollectionAssociation #:nodoc: | ||
self.macro = :directly_contains | ||
|
||
def build | ||
reflection = super | ||
configure_dependency | ||
reflection | ||
end | ||
|
||
def validate_options | ||
super | ||
if !options[:predicate] | ||
raise ArgumentError, "You must specify a predicate for #{name}" | ||
elsif !options[:predicate].kind_of?(RDF::URI) | ||
raise ArgumentError, "Predicate must be a kind of RDF::URI" | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveFedora | ||
module Associations | ||
class ContainerProxy < CollectionProxy | ||
def initialize(association) | ||
@association = association | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
lib/active_fedora/associations/directly_contains_association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module ActiveFedora | ||
module Associations | ||
class DirectlyContainsAssociation < CollectionAssociation #:nodoc: | ||
|
||
def insert_record(record, force = true, validate = true) | ||
container.save! | ||
if record.new_record? | ||
if force | ||
record.save! | ||
else | ||
return false unless record.save(validate: validate) | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
def reader | ||
@records ||= ContainerProxy.new(self) | ||
end | ||
|
||
def find_target | ||
uris = owner.resource.query(predicate: options[:predicate]).map { |r| r.object.to_s } | ||
if klass <= ActiveFedora::File # a subclass of file | ||
uris.map { |uri| klass.new(uri) } | ||
else | ||
uris.map { |uri| klass.find(klass.uri_to_id(uri)) } | ||
end | ||
end | ||
|
||
def container | ||
@container ||= begin | ||
DirectContainer.find_or_initialize(ActiveFedora::Base.uri_to_id(uri)).tap do |container| | ||
container.parent = @owner | ||
container.member_relation = [@reflection.predicate] | ||
end | ||
end | ||
end | ||
|
||
protected | ||
def initialize_attributes(record) #:nodoc: | ||
record.uri = ActiveFedora::Base.id_to_uri(container.mint_id) | ||
set_inverse_instance(record) | ||
end | ||
|
||
private | ||
|
||
def uri | ||
raise "Can't get uri. Owner isn't saved" if @owner.new_record? | ||
"#{@owner.uri}/#{@reflection.name}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module ActiveFedora | ||
class Container < ActiveFedora::Base | ||
|
||
property :membership_resource, predicate: ::RDF::Vocab::LDP.membershipResource | ||
property :member_relation, predicate: ::RDF::Vocab::LDP.hasMemberRelation | ||
|
||
def parent | ||
@parent || raise("Parent hasn't been set on #{self.class}") | ||
end | ||
|
||
def parent=(parent) | ||
@parent = parent | ||
self.membership_resource = [::RDF::URI(parent.uri)] | ||
end | ||
|
||
def mint_id | ||
"#{id}/#{SecureRandom.uuid}" | ||
end | ||
|
||
def self.find_or_initialize(id) | ||
find(id) | ||
rescue ActiveFedora::ObjectNotFoundError | ||
new(id) | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveFedora | ||
class DirectContainer < Container | ||
type ::RDF::Vocab::LDP.DirectContainer | ||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.