Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return array of version uris #628

Merged
merged 1 commit into from
Dec 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/active_fedora/fixity_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def status

def get_fixity_response_from_fedora
uri = target + "/fcr:fixity"
ActiveFedora.fedora.connection.get(uri)
ActiveFedora.fedora.connection.get(encoded_url(uri))
end

def fixity_graph
Expand All @@ -36,5 +36,13 @@ def status_url
::RDF::URI("http://fedora.info/definitions/v4/repository#status")
end

def encoded_url uri
if uri.match("fcr:versions")
uri.gsub(/fcr:versions/,"fcr%3aversions")
else
uri
end
end

end
end
25 changes: 12 additions & 13 deletions lib/active_fedora/versionable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,33 @@ def model_type
end
end

# Returns an array of RDF::Literal objects where the version label matches
# our own version label and excludes auto-snapshot versions from Fedora.
# Returns an array of uris matching our own version label, excluding auto-snapshot versions from Fedora.
def versions
results = versions_graph.query([nil, ::RDF::URI.new('http://fedora.info/definitions/v4/repository#hasVersionLabel'), nil])
numbered_versions(results)
numbered_versions(results).map { |v| version_uri(v.to_s) }
end

def versions_graph
@versions_graph ||= ::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(versions_request)
end

def versions_url
uri + '/fcr:versions'
end

def create_version
resp = ActiveFedora.fedora.connection.post(versions_url, nil, {slug: version_name})
resp = ActiveFedora.fedora.connection.post(versions_uri, nil, {slug: version_name})
@versions_graph = nil
resp.success?
end

# This method does not rely on the internal versionable flag of the object, instead
# it queries Fedora directly to figure out if there are versions for the resource.
def has_versions?
ActiveFedora.fedora.connection.head(versions_url)
ActiveFedora.fedora.connection.head(versions_uri)
true
rescue Ldp::NotFound
false
end

def restore_version label
resp = ActiveFedora.fedora.connection.patch(version_url(label), nil)
resp = ActiveFedora.fedora.connection.patch(version_uri(label), nil)
@versions_graph = nil
reload
refresh_attributes if self.respond_to?("refresh_attributes")
Expand All @@ -62,7 +57,7 @@ def restore_version label

def versions_request
resp = begin
ActiveFedora.fedora.connection.get(versions_url)
ActiveFedora.fedora.connection.get(versions_uri)
rescue Ldp::NotFound
return ''
end
Expand All @@ -74,10 +69,14 @@ def versions_request
resp.body
end

def version_url label
versions_url + '/' + label
def versions_uri
uri + '/fcr:versions'
end

def version_uri label
versions_uri + '/' + label
end

def version_name
if versions.empty?
"version1"
Expand Down
46 changes: 32 additions & 14 deletions spec/integration/versionable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class WithVersions < ActiveFedora::Base

it "should have one version" do
expect(subject.versions.size).to eq 1
expect(subject.versions.first).to be_kind_of ::RDF::Literal
expect(subject.versions.first).to be_kind_of String
expect(subject.versions.first).to end_with "version1"
end

context "two times" do
Expand All @@ -41,8 +42,8 @@ class WithVersions < ActiveFedora::Base

it "should have two versions" do
expect(subject.versions.size).to eq 2
subject.versions.each do |version|
expect(version).to be_kind_of ::RDF::Literal
subject.versions.each_index do |index|
expect(subject.versions[index]).to end_with "version"+(index+1).to_s
end
end

Expand Down Expand Up @@ -126,7 +127,7 @@ class MockAFBase < ActiveFedora::Base
end

it "should have one version" do
expect(subject.versions.first).to be_kind_of ::RDF::Literal
expect(subject.versions.first).to end_with "version1"
end

it "should have a title" do
Expand All @@ -146,8 +147,8 @@ class MockAFBase < ActiveFedora::Base

it "should have two versions" do
expect(subject.versions.size).to eq 2
subject.versions.each do |version|
expect(version).to be_kind_of ::RDF::Literal
subject.versions.each_index do |index|
expect(subject.versions[index]).to end_with "version"+(index+1).to_s
end
end

Expand Down Expand Up @@ -255,8 +256,7 @@ class MockAFBase < ActiveFedora::Base
end

it "should have one version" do
expect(subject.versions.size).to eq 1
expect(subject.versions.first).to be_kind_of ::RDF::Literal
expect(subject.versions.first).to end_with "version1"
end

it "should have a title" do
Expand All @@ -277,8 +277,8 @@ class MockAFBase < ActiveFedora::Base

it "should have two unique versions" do
expect(subject.versions.size).to eq 2
subject.versions.each do |version|
expect(version).to be_kind_of ::RDF::Literal
subject.versions.each_index do |index|
expect(subject.versions[index]).to end_with "version"+(index+1).to_s
end
end

Expand Down Expand Up @@ -384,7 +384,7 @@ class MockAFBase < ActiveFedora::Base
expect(subject.versions.size).to eq 1
expect(subject.original_name).to eql(first_name)
expect(subject.content.size).to eq first_file.size
expect(subject.versions.first).to be_kind_of ::RDF::Literal
expect(subject.versions.first).to end_with "version1"
end

context "two times" do
Expand All @@ -401,8 +401,24 @@ class MockAFBase < ActiveFedora::Base
expect(subject.versions.size).to eq 2
expect(subject.original_name).to eql(second_name)
expect(subject.content.size).to eq second_file.size
subject.versions.each do |version|
expect(version).to be_kind_of ::RDF::Literal
subject.versions.each_index do |index|
expect(subject.versions[index]).to end_with "version"+(index+1).to_s
end
end

context "with fixity checking" do
let(:results) do
results = Array.new
subject.versions.each do |uri|
results << ActiveFedora::FixityService.new(uri).check
end
return results
end

it "should report on the fixity of each version" do
results.each do |result|
expect(result).to be true
end
end
end

Expand Down Expand Up @@ -436,7 +452,9 @@ class MockAFBase < ActiveFedora::Base
expect(subject.versions.size).to eq 3
expect(subject.original_name).to eql(first_name)
expect(subject.content.size).to eq first_file.size
expect(subject.versions.first).to be_kind_of ::RDF::Literal
subject.versions.each_index do |index|
expect(subject.versions[index]).to end_with "version"+(index+1).to_s
end
end

end
Expand Down