Skip to content

Commit

Permalink
The uri() method should return an RDF::URI
Browse files Browse the repository at this point in the history
instead of a string
  • Loading branch information
jcoyne committed Jun 29, 2015
1 parent 07c5cfd commit 11a8f32
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
11 changes: 7 additions & 4 deletions lib/active_fedora/file.rb
@@ -1,6 +1,7 @@
require 'deprecation'

module ActiveFedora
# An LDP NonRDFSource. The base class for a bytestream stored in the repository.
class File
extend ActiveModel::Callbacks
extend ActiveSupport::Autoload
Expand All @@ -25,14 +26,14 @@ class File
define_model_callbacks :save, :create, :destroy
define_model_callbacks :initialize, only: :after

# @param parent_or_url_or_hash [ActiveFedora::Base, String, Hash, NilClass] the parent resource or the URI of this resource
# @param parent_or_url_or_hash [ActiveFedora::Base, RDF::URI, String, Hash, NilClass] the parent resource or the URI of this resource
# @param path [String] the path partial relative to the resource
# @param options [Hash]
def initialize(parent_or_url_or_hash = nil, path=nil, options={})
case parent_or_url_or_hash
when Hash
@ldp_source = build_ldp_resource_via_uri
when nil, String
when nil, String, ::RDF::URI
@ldp_source = build_ldp_resource_via_uri parent_or_url_or_hash
when ActiveFedora::Base
Deprecation.warn File, "Initializing a file by passing a container is deprecated. Initialize with a uri instead. This capability will be removed in active-fedora 10.0"
Expand All @@ -44,16 +45,18 @@ def initialize(parent_or_url_or_hash = nil, path=nil, options={})
@ldp_source = build_ldp_resource_via_uri(uri, nil)

else
raise "The first argument to #{self} must be a String or an ActiveFedora::Base. You provided a #{parent_or_url.class}"
raise "The first argument to #{self} must be a String or an ActiveFedora::Base. You provided a #{parent_or_url_or_hash.class}"
end

@attributes = {}.with_indifferent_access
end

# @return [true, false] true if the objects are equal or when the objects have uris
# and the uris are equal
def ==(comparison_object)
super ||
comparison_object.instance_of?(self.class) &&
id.present? &&
uri.value.present? &&
comparison_object.uri == uri
end

Expand Down
5 changes: 2 additions & 3 deletions lib/active_fedora/identifiable.rb
Expand Up @@ -52,10 +52,9 @@ def pid
id
end

# @return [RDF::URI] the uri for this resource
def uri
# TODO could we return a RDF::URI instead?
uri = @ldp_source.try(:subject_uri)
uri.value == '' ? uri : uri.to_s
@ldp_source.subject_uri
end


Expand Down
2 changes: 1 addition & 1 deletion spec/integration/directly_contains_one_association_spec.rb
Expand Up @@ -45,7 +45,7 @@ class VersionedFileWithMetadata < ActiveFedora::File
end
it "relies on info from the :through association, including class_name" do
expect(page_image.files).to include(primary_file)
expect(primary_file.uri).to include("/files/")
expect(primary_file.uri.to_s).to include("/files/")
expect(subject.class).to eq FileWithMetadata
end
end
Expand Down
11 changes: 9 additions & 2 deletions spec/unit/pathing_spec.rb
Expand Up @@ -3,6 +3,7 @@
describe ActiveFedora::Base do
describe ".uri_prefix" do
let(:path) { "foo" }

before do
class FooHistory < ActiveFedora::Base
def uri_prefix
Expand All @@ -11,18 +12,24 @@ def uri_prefix
property :title, predicate: ::RDF::DC.title
end
end

after do
Object.send(:remove_const, :FooHistory)
end

subject { FooHistory.new(title: ["Root foo"]) }

it { is_expected.to have_uri_prefix }
it "should use the root path in the uri" do

it "uses the root path in the uri" do
expect(subject.uri_prefix).to eql path
end

context "when the object is saved" do
before { subject.save }

it "should persist the path in the uri" do
expect(subject.uri).to include(path)
expect(subject.uri.to_s).to include(path)
end

end
Expand Down

0 comments on commit 11a8f32

Please sign in to comment.