Skip to content

Commit

Permalink
Merge pull request #676 from projecthydra/8.0.0.rc3
Browse files Browse the repository at this point in the history
Can assign single ActiveTriples::Resource to single-valued attribute (fi...
  • Loading branch information
jcoyne committed Jan 7, 2015
2 parents d06db81 + c26db08 commit 7f366cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/active_fedora/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ def array_setter(field, args)
end
raise UnknownAttributeError, "#{self.class} does not have an attribute `#{field}'" unless self.class.defined_attributes.key?(field)
if self.class.multiple?(field)
unless args.nil? || args.respond_to?(:each)
raise ArgumentError, "Cannot set the multi-valued attribute `#{field}' to a scalar value."
unless args.nil? || ( args.respond_to?(:each) && !args.kind_of?(ActiveTriples::Resource) )
raise ArgumentError, "Cannot set the multi-valued attribute `#{field}' to a single value."
end
elsif args.respond_to?(:each) # unique
raise ArgumentError, "Cannot set the single-valued attribute `#{field}' to an enumerable value."
unless args.kind_of?(ActiveTriples::Resource)
raise ArgumentError, "Cannot set the single-valued attribute `#{field}' to multiple values."
end
end
self.class.defined_attributes[field].writer(self, args)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_fedora/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveFedora
VERSION = "8.0.0.rc2"
VERSION = "8.0.0.rc3"
end
30 changes: 27 additions & 3 deletions spec/unit/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,27 @@ class BarHistory3 < BarHistory2
before :all do
class BarRdfDatastream < ActiveFedora::NtriplesRDFDatastream
property :title, :predicate => RDF::DC.title
property :description, :predicate => RDF::DC.description, :multivalue => false
property :description, :predicate => RDF::DC.description
property :author, :predicate => RDF::FOAF.Person
property :editor, :predicate => RDF::FOAF.Person
end
class BarHistory4 < ActiveFedora::Base
has_metadata 'rdfish', :type=>BarRdfDatastream
has_attributes :title, :description, datastream: 'rdfish', multiple: true
has_attributes :title, datastream: 'rdfish', multiple: true
has_attributes :description, datastream: 'rdfish', multiple: false
has_attributes :author, datastream: 'rdfish', multiple: false
has_attributes :editor, datastream: 'rdfish', multiple: true
end
class PersonResource < ActiveTriples::Resource
configure type: RDF::FOAF.Person
property :person_name, predicate: RDF::FOAF.name
end
end

after :all do
Object.send(:remove_const, :BarHistory4)
Object.send(:remove_const, :BarRdfDatastream)
Object.send(:remove_const, :PersonResource)
end

subject { BarHistory4.new }
Expand All @@ -241,13 +251,27 @@ class BarHistory4 < ActiveFedora::Base
subject.title = ["Title1", "Title2"]
subject.title_changed?.should be true
end
it "should not accept a single literal value" do
expect { subject.title = "Title1" }.to raise_error
end
it "should not accept a single ActiveTriples::Resource" do
editor = PersonResource.new.tap { |p| p.person_name = "Sally" }
expect { subject.editor = editor }.to raise_error
end
end
describe "with a single-valued field" do
it "should be able to track change status" do
subject.description_changed?.should be false
subject.description = ["A brief description"]
subject.description = "A brief description"
subject.description_changed?.should be true
end
it "should not accept an array of literal values" do
expect { subject.description = ["A brief description", "A longer description"] }.to raise_error
end
it "should accept an ActiveTriples::Resource" do
author = PersonResource.new.tap { |p| p.person_name = "Bob" }
expect { subject.author = author }.not_to raise_error
end
end
end

Expand Down

0 comments on commit 7f366cd

Please sign in to comment.