Skip to content

Commit

Permalink
Add optional prefix to resource URI
Browse files Browse the repository at this point in the history
  • Loading branch information
awead committed Jun 24, 2015
1 parent a0ff559 commit c91e43f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/active_fedora.rb
Expand Up @@ -88,6 +88,7 @@ module ActiveFedora #:nodoc:
autoload :NomDatastream
autoload :NullRelation
autoload :OmDatastream
autoload :Pathing
autoload :Persistence
autoload :ProfileIndexingService
autoload :Property
Expand Down
1 change: 1 addition & 0 deletions lib/active_fedora/base.rb
Expand Up @@ -51,6 +51,7 @@ class Base
include Versionable
include LoadableFromJson
include Schema
include Pathing
end

ActiveSupport.run_load_hooks(:active_fedora, Base)
Expand Down
24 changes: 24 additions & 0 deletions lib/active_fedora/pathing.rb
@@ -0,0 +1,24 @@
module ActiveFedora
module Pathing
extend ActiveSupport::Concern

included do
def uri_prefix
nil
end

def has_uri_prefix?
!uri_prefix.nil?
end

def root_resource_path
if has_uri_prefix?
ActiveFedora.fedora.base_path + "/" + self.uri_prefix
else
ActiveFedora.fedora.base_path
end
end
end

end
end
14 changes: 13 additions & 1 deletion lib/active_fedora/persistence.rb
Expand Up @@ -184,10 +184,22 @@ def assign_rdf_subject
@ldp_source = if !id && new_id = assign_id
LdpResource.new(ActiveFedora.fedora.connection, self.class.id_to_uri(new_id), @resource)
else
LdpResource.new(ActiveFedora.fedora.connection, @ldp_source.subject, @resource, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
LdpResource.new(ActiveFedora.fedora.connection, @ldp_source.subject, @resource, ActiveFedora.fedora.host + base_path_for_resource)
end
end

def base_path_for_resource
init_root_path if self.has_uri_prefix?
self.root_resource_path
end

def init_root_path
path = self.root_resource_path.gsub(/^\//,"")
ActiveFedora.fedora.connection.head(path)
rescue Ldp::NotFound
ActiveFedora.fedora.connection.put(path, "")
end

def assign_uri_to_attached_files
attached_files.each do |name, ds|
ds.uri= "#{uri}/#{name}"
Expand Down
30 changes: 30 additions & 0 deletions spec/unit/pathing_spec.rb
@@ -0,0 +1,30 @@
require 'spec_helper'

describe ActiveFedora::Base do
describe ".uri_prefix" do
let(:path) { "foo" }
before do
class FooHistory < ActiveFedora::Base
def uri_prefix
"foo"
end
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
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)
end

end
end
end

0 comments on commit c91e43f

Please sign in to comment.