Skip to content
Snippets Groups Projects
Commit b65890a1 authored by Anusha Ranganathan's avatar Anusha Ranganathan
Browse files

Bug fix/save nested attributes

parent 885d21df
No related branches found
No related tags found
3 merge requests!115Tombstone not available for dataset,!930.3 release,!84Bug fix/save nested attributes
module Hyrax
module Actors
module ComplexAttributes
def apply_save_data_to_curation_concern(env)
super
# Clear this, it will be re-added by update_complex_metadata
env.curation_concern.updated_subresources = []
env = update_complex_metadata(env, env.curation_concern)
end
# Call execute on the ActiveTriple::Resources (ATR), like ComplexInstrument
# Due to a bug somewhere in ActiveFedora, changes to ATR
# are not being committed to the resource graph because they are BufferedTransactions
# This method runs execute on the graphs of each ATR
# Additionally adds all ATR to top-level property updates_subresources
# this seems to be necessary to get ATR nested within other ATR to update
def update_complex_metadata(env, resource_to_update)
complex_attributes.each do |attribute|
next unless resource_to_update.respond_to?(attribute)
next if resource_to_update.send(attribute).empty?
resource_to_update.send(attribute).each do |resource|
update_complex_metadata(env, resource)
resource.graph.execute
resource_to_update.send(attribute.to_s).push(resource)
env.curation_concern.updated_subresources.push(resource_to_update.send(attribute))
end
end
env
end
def complex_attributes
%w[
complex_date
complex_funding_reference
complex_identifier
complex_person
complex_relation
]
end
end
end
end
......@@ -3,6 +3,7 @@
module Hyrax
module Actors
class CrcDatasetActor < Hyrax::Actors::BaseActor
include Hyrax::Actors::ComplexAttributes
end
end
end
end
\ No newline at end of file
......@@ -3,6 +3,7 @@
module Hyrax
module Actors
class DatasetActor < Hyrax::Actors::BaseActor
include Hyrax::Actors::ComplexAttributes
end
end
end
......@@ -112,6 +112,9 @@ class CrcDataset < ActiveFedora::Base
index.as :stored_searchable, :facetable
end
# Required due to bug saving nested resources
property :updated_subresources, predicate: ::RDF::Vocab::Rdms.updatedSubresources, class_name: "ActiveTriples::Resource"
# This must be included at the end, because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::BasicMetadata
......@@ -121,6 +124,7 @@ class CrcDataset < ActiveFedora::Base
accepts_nested_attributes_for :complex_identifier, reject_if: :identifier_blank, allow_destroy: true
accepts_nested_attributes_for :complex_funding_reference, reject_if: :fundref_blank, allow_destroy: true
accepts_nested_attributes_for :complex_relation, reject_if: :relation_blank, allow_destroy: true
accepts_nested_attributes_for :updated_subresources, allow_destroy: true
def complex_subjects
ComplexSubject.where(parent_source_identifier: source.first)
......
......@@ -85,6 +85,9 @@ class Dataset < ActiveFedora::Base
property :complex_relation, predicate: ::RDF::Vocab::DC.relation, class_name:"ComplexRelation"
# Required due to bug saving nested resources
property :updated_subresources, predicate: ::RDF::Vocab::Rdms.updatedSubresources, class_name: "ActiveTriples::Resource"
# This must be included at the end, because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::BasicMetadata
......@@ -94,4 +97,5 @@ class Dataset < ActiveFedora::Base
accepts_nested_attributes_for :complex_identifier, reject_if: :identifier_blank, allow_destroy: true
accepts_nested_attributes_for :complex_funding_reference, reject_if: :fundref_blank, allow_destroy: true
accepts_nested_attributes_for :complex_relation, reject_if: :relation_blank, allow_destroy: true
accepts_nested_attributes_for :updated_subresources, allow_destroy: true
end
......@@ -38,6 +38,8 @@ module RDF
#File Set
property 'forComplexIdentifier'
property'forComplexType'
# for nested metadata
property 'updatedSubresources'
end
end
end
require 'rails_helper'
RSpec.describe Hyrax::Actors::ComplexAttributes do
let(:env) { double(curation_concern: double(updated_subresources: [], :updated_subresources= => []) ) }
before do
class SuperClass
def apply_save_data_to_curation_concern(env)
end
end
class TestClass < SuperClass
include Hyrax::Actors::ComplexAttributes
end
class ExampleWork < ActiveFedora::Base
property :complex_person, predicate: ::RDF::URI.new('http://example.org#person'), class_name: "ComplexPerson"
accepts_nested_attributes_for :complex_person
end
end
after do
Object.send(:remove_const, :ExampleWork)
Object.send(:remove_const, :TestClass)
Object.send(:remove_const, :SuperClass)
end
let(:test) { TestClass.new }
describe '#apply_save_data_to_curation_concern' do
before { test.apply_save_data_to_curation_concern(env) }
it { expect(env.curation_concern).to have_received(:updated_subresources=).with([]) }
end
describe '#update_complex_metadata' do
let(:resource) { double(complex_person: ExampleWork.new(complex_person_attributes: [{ first_name: 'John' }]).complex_person) }
before { test.update_complex_metadata(env, resource) }
it { expect(env.curation_concern.updated_subresources).to eql([resource.complex_person]) }
end
describe '#complex_attributes' do
subject { test.complex_attributes }
it { is_expected.to match_array(%w[complex_date complex_funding_reference complex_identifier
complex_person complex_relation]) }
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment