Skip to content

Commit

Permalink
[wip] Ensuring that ordered proxies properly resolve to target nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgriffiniii committed Sep 20, 2022
1 parent b1ba571 commit c53f863
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 17 deletions.
10 changes: 10 additions & 0 deletions lib/active_fedora/aggregation/ordered_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def each
end
end

def to_a
elements = []

each do |proxy|
elements << proxy
end

elements
end

private

def first_head
Expand Down
9 changes: 6 additions & 3 deletions lib/active_fedora/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ def association(name) # :nodoc:
association = association_instance_get(name)

if association.nil?
reflection = self.class._reflect_on_association(name)
association = reflection.association_class.new(self, reflection) if reflection
association_instance_set(name, association) if association
reflection = self.class._reflect_on_association(name)

if reflection
association = reflection.association_class.new(self, reflection)
association_instance_set(name, association) if association
end
end

association
Expand Down
21 changes: 15 additions & 6 deletions lib/active_fedora/associations/builder/orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ def self.valid_options(options)

def self.define_readers(mixin, name)
super
mixin.redefine_method(target_accessor(name)) do
association(name).target_reader

new_method_name = target_accessor(name)
mixin.redefine_method(new_method_name) do
target_association = association(name)
target_association.target_reader.targets
end
mixin.redefine_method("#{target_accessor(name, pluralize: false)}_ids") do
association(name).target_ids_reader

new_ids_method_name = "#{target_accessor(name, pluralize: false)}_ids"
mixin.redefine_method(new_ids_method_name) do
target_association = association(name)
target_association.target_ids_reader
end
mixin.redefine_method("#{target_accessor(name)}=") do |nodes|
association(name).target_writer(nodes)

new_mutate_method_name = "#{target_accessor(name)}="
mixin.redefine_method(new_mutate_method_name) do |nodes|
target_association = association(name)
target_association.target_writer(nodes)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def raise_on_type_mismatch!(record)
end

def find_or_initialize_target(&block)
# If the reflection inherits from the ActiveFedora::File Class, build the association
if reflection.klass < ActiveFedora::File
reflection.build_association(id: target_uri, &block)
else
Expand Down
3 changes: 3 additions & 0 deletions lib/active_fedora/associations/orders_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def load_target
# Append a target node to the end of the order.
# @param [ActiveFedora::Base] record Record to append
def append_target(record, _skip_callbacks = false)
owner.save unless owner.persisted?
record.save unless record.persisted?

unordered_association.concat(record) unless unordered_association.target.include?(record)
target.append_target(record, proxy_in: owner)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/active_fedora/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Base
include LoadableFromJson
include Schema
include Aggregation::BaseExtension

def self.find(*args)
super(*args)
end
end

ActiveSupport.run_load_hooks(:active_fedora, Base)
Expand Down
11 changes: 7 additions & 4 deletions lib/active_fedora/orders/ordered_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ def empty?
# @option [::RDF::URI, ActiveFedora::Base] :proxy_in Proxy in to
# assert on the created node.
def append_target(target, proxy_in: nil)
target.save unless target.persisted?

node = build_node(new_node_subject)
node.target = target
node.proxy_in = proxy_in

append_to(node, tail.prev)
end

Expand Down Expand Up @@ -176,6 +179,10 @@ def proxy_in
proxies.first
end

def ordered_reader
ActiveFedora::Aggregation::OrderedReader.new(self)
end

private

attr_reader :node_cache
Expand All @@ -192,10 +199,6 @@ def append_to(source, append_node)
@changed = true
end

def ordered_reader
ActiveFedora::Aggregation::OrderedReader.new(self)
end

def build_node(subject = nil)
return nil unless subject
node_cache.fetch(subject) do
Expand Down
10 changes: 9 additions & 1 deletion lib/active_fedora/orders/target_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ def clear
association.delete_at(0) while to_ary.present?
end

def reader
@reader = association.reader
end

def targets
@targets = reader.map(&:target)
end

def to_ary
association.reader.map(&:target).dup
@elements = targets.dup
end
alias to_a to_ary

Expand Down
2 changes: 2 additions & 0 deletions lib/active_fedora/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def create_or_update(*args)
def _create_record(_options = {})
assign_rdf_subject
serialize_attached_files

@ldp_source = @ldp_source.create

assign_uri_to_contained_resources
save_contained_resources
refresh
Expand Down
12 changes: 9 additions & 3 deletions lib/active_fedora/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def find(*args)
return to_a.find { |*block_args| yield(*block_args) } if block_given?
options = args.extract_options!
options = options.dup
cast = if @klass == ActiveFedora::Base && !options.key?(:cast)
# cast = if @klass == ActiveFedora::Base && !options.key?(:cast)
cast = if (@klass == ActiveFedora::Base || @klass.ancestors.include?(ActiveFedora::Base)) && !options.key?(:cast)
true
else
options.delete(:cast)
Expand Down Expand Up @@ -179,7 +180,9 @@ def load_from_fedora(id, cast)
raise ActiveFedora::ObjectNotFoundError, "No ID provided for #{klass.name}." if id.empty?
resource = ActiveFedora.fedora.ldp_resource_service.build(klass, id)
raise_record_not_found_exception!(id) if resource.new?
class_to_load(resource, cast).allocate.init_with_resource(resource) # Triggers the find callback
new_class = class_to_load(resource, cast)
built = new_class.allocate
built.init_with_resource(resource) # Triggers the find callback
end

def raise_record_not_found_exception!(id)
Expand All @@ -190,7 +193,10 @@ def class_to_load(resource, cast)
if @klass == ActiveFedora::Base && cast == false
ActiveFedora::Base
else
resource_class = ActiveFedora.model_mapper.classifier(resource).best_model
return @klass if @klass.parents.include?(ActiveFedora::Aggregation)

model_classifier = ActiveFedora.model_mapper.classifier(resource)
resource_class = model_classifier.best_model
raise ActiveFedora::ModelMismatch, "Expected #{@klass}. Got: #{resource_class}" unless equivalent_class?(resource_class)
resource_class
end
Expand Down

0 comments on commit c53f863

Please sign in to comment.