-
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, has_member_relation: ::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
20 changed files
with
421 additions
and
70 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
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
module ActiveFedora::Associations::Builder | ||
class DirectlyContains < CollectionAssociation #:nodoc: | ||
self.macro = :directly_contains | ||
self.valid_options += [:has_member_relation, :is_member_of_relation] | ||
self.valid_options -= [:predicate] | ||
|
||
def build | ||
reflection = super | ||
configure_dependency | ||
reflection | ||
end | ||
|
||
def validate_options | ||
super | ||
if !options[:has_member_relation] && !options[:is_member_of_relation] | ||
raise ArgumentError, "You must specify a predicate for #{name}" | ||
elsif !options[:has_member_relation].kind_of?(RDF::URI) && !options[:is_member_of_relation].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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
module ActiveFedora | ||
module Associations | ||
class DirectlyContainsAssociation < CollectionAssociation #:nodoc: | ||
|
||
def insert_record(record, force = true, validate = true) | ||
container.save! | ||
if force | ||
record.save! | ||
else | ||
record.save(validate: validate) | ||
end | ||
end | ||
|
||
def reader | ||
@records ||= ContainerProxy.new(self) | ||
end | ||
|
||
def find_target | ||
query_node = if container_predicate = options[:has_member_relation] | ||
owner | ||
else | ||
container_predicate = ::RDF::Vocab::LDP.contains | ||
container | ||
end | ||
|
||
uris = query_node.resource.query(predicate: container_predicate).map { |r| r.object.to_s } | ||
|
||
uris.map { |object_uri| klass.find(klass.uri_to_id(object_uri)) } | ||
end | ||
|
||
def container | ||
@container ||= begin | ||
DirectContainer.find_or_initialize(ActiveFedora::Base.uri_to_id(uri)).tap do |container| | ||
container.parent = @owner | ||
container.has_member_relation = Array(options[:has_member_relation]) | ||
container.is_member_of_relation = Array(options[:is_member_of_relation]) | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def count_records | ||
load_target.size | ||
end | ||
|
||
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
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,30 @@ | ||
module ActiveFedora | ||
# This is the base class for ldp containers, it is not an ldp:BasicContainer | ||
class Container < ActiveFedora::Base | ||
|
||
property :membership_resource, predicate: ::RDF::Vocab::LDP.membershipResource | ||
property :has_member_relation, predicate: ::RDF::Vocab::LDP.hasMemberRelation | ||
property :is_member_of_relation, predicate: ::RDF::Vocab::LDP.isMemberOfRelation | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveFedora | ||
class FileRelation < Relation | ||
def load_from_fedora(id, _) | ||
klass.new(klass.id_to_uri(id)) | ||
end | ||
end | ||
end |
Oops, something went wrong.