Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
367 additions
and
64 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
52 changes: 52 additions & 0 deletions
52
lib/active_fedora/associations/basic_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,52 @@ | ||
module ActiveFedora | ||
module Associations | ||
class BasicContainsAssociation < SingularAssociation #:nodoc: | ||
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar | ||
def reader(force_reload = false) | ||
super || build | ||
end | ||
|
||
def find_target | ||
reflection.build_association(target_uri).tap do |record| | ||
configure_datastream(record) if reflection.options[:block] | ||
end | ||
end | ||
|
||
def target_uri | ||
"#{owner.uri}/#{reflection.name}" | ||
end | ||
|
||
private | ||
|
||
def raise_on_type_mismatch(record) | ||
return if record.is_a? LoadableFromJson::SolrBackedMetadataFile | ||
super | ||
end | ||
|
||
def replace(record) | ||
if record | ||
raise_on_type_mismatch(record) | ||
@updated = true | ||
end | ||
|
||
self.target = record | ||
end | ||
|
||
def new_record(method, attributes) | ||
record = super | ||
configure_datastream(record) | ||
record | ||
end | ||
|
||
def configure_datastream(record) | ||
# If you called has_metadata with a block, pass the block into the File class | ||
if reflection.options[:block].class == Proc | ||
reflection.options[:block].call(record) | ||
end | ||
if record.new_record? && reflection.options[:autocreate] | ||
record.datastream_will_change! | ||
end | ||
end | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
lib/active_fedora/associations/builder/indirectly_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,26 @@ | ||
module ActiveFedora::Associations::Builder | ||
class IndirectlyContains < CollectionAssociation #:nodoc: | ||
self.macro = :indirectly_contains | ||
self.valid_options += [:has_member_relation, :is_member_of_relation, :inserted_content_relation, :foreign_key, :through] | ||
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 | ||
|
||
raise ArgumentError, "Missing :through option" if !options[:through] | ||
raise ArgumentError, "Missing :foreign_key option" if !options[:foreign_key] | ||
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 |
---|---|---|
@@ -1,52 +1,23 @@ | ||
# This is the parent class of DirectlyContainsAssociation and IndirectlyContainsAssociation | ||
module ActiveFedora | ||
module Associations | ||
class ContainsAssociation < SingularAssociation #:nodoc: | ||
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar | ||
def reader(force_reload = false) | ||
super || build | ||
end | ||
|
||
def find_target | ||
reflection.build_association(target_uri).tap do |record| | ||
configure_datastream(record) if reflection.options[:block] | ||
end | ||
end | ||
class ContainsAssociation < CollectionAssociation #:nodoc: | ||
|
||
def target_uri | ||
"#{owner.uri}/#{reflection.name}" | ||
def reader | ||
@records ||= ContainerProxy.new(self) | ||
end | ||
|
||
private | ||
protected | ||
|
||
def raise_on_type_mismatch(record) | ||
return if record.is_a? LoadableFromJson::SolrBackedMetadataFile | ||
super | ||
end | ||
|
||
def replace(record) | ||
if record | ||
raise_on_type_mismatch(record) | ||
@updated = true | ||
def count_records | ||
load_target.size | ||
end | ||
|
||
self.target = record | ||
end | ||
|
||
def new_record(method, attributes) | ||
record = super | ||
configure_datastream(record) | ||
record | ||
end | ||
|
||
def configure_datastream(record) | ||
# If you called has_metadata with a block, pass the block into the File class | ||
if reflection.options[:block].class == Proc | ||
reflection.options[:block].call(record) | ||
def uri | ||
raise "Can't get uri. Owner isn't saved" if @owner.new_record? | ||
"#{@owner.uri}/#{@reflection.name}" | ||
end | ||
if record.new_record? && reflection.options[:autocreate] | ||
record.datastream_will_change! | ||
end | ||
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
68 changes: 68 additions & 0 deletions
68
lib/active_fedora/associations/indirectly_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,68 @@ | ||
module ActiveFedora | ||
module Associations | ||
class IndirectlyContainsAssociation < ContainsAssociation #:nodoc: | ||
|
||
def insert_record(record, force = true, validate = true) | ||
container.save! | ||
if force | ||
record.save! | ||
else | ||
return false unless record.save(validate: validate) | ||
end | ||
|
||
save_through_record(record) | ||
|
||
return true | ||
end | ||
|
||
def find_target | ||
if container_predicate = options[:has_member_relation] | ||
uris = owner.resource.query(predicate: container_predicate).map { |r| r.object.to_s } | ||
uris.map { |object_uri| klass.find(klass.uri_to_id(object_uri)) } | ||
else | ||
# TODO this is a lot of reads. Avoid this path | ||
container_predicate = ::RDF::Vocab::LDP.contains | ||
proxy_uris = container.resource.query(predicate: container_predicate).map { |r| r.object.to_s } | ||
proxy_uris.map { |uri| proxy_class.find(proxy_class.uri_to_id(uri))[options[:foreign_key]] } | ||
end | ||
|
||
|
||
end | ||
|
||
def container | ||
@container ||= begin | ||
IndirectContainer.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]) | ||
container.inserted_content_relation = Array(options[:inserted_content_relation]) | ||
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 save_through_record(record) | ||
build_proxy_node({}) do |node| | ||
node[options[:foreign_key]] = record | ||
node.save | ||
end | ||
end | ||
|
||
def build_proxy_node(attributes, &block) | ||
proxy_class.new({ id: container.mint_id }.merge(attributes), &block) | ||
end | ||
|
||
def proxy_class | ||
@proxy_class ||= options[:through].constantize | ||
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveFedora | ||
class IndirectContainer < Container | ||
type ::RDF::Vocab::LDP.IndirectContainer | ||
|
||
property :inserted_content_relation, predicate: ::RDF::Vocab::LDP.insertedContentRelation | ||
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
Oops, something went wrong.