Skip to content

Commit

Permalink
Merge pull request #2644 from brianzelip/ANW-1493-rep-file-version
Browse files Browse the repository at this point in the history
ANW-1493 Representative File Version: Backend Rules
  • Loading branch information
Brian Hoffman committed Mar 4, 2022
2 parents 723b263 + 5e73963 commit eaf639e
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/app/lib/bulk_import/digital_object_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create(title, thumb, link, id, publish, archival_object, report, link_publis
fv.publish = thumb_publish
fv.xlink_actuate_attribute = "onLoad"
fv.xlink_show_attribute = "embed"
fv.is_representative = true
fv.is_representative = thumb_publish
files.push fv
end
dig_o = JSONModel(:digital_object).new._always_valid!
Expand Down
19 changes: 19 additions & 0 deletions backend/app/model/digital_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ def self.sequel_to_jsonmodel(objs, opts = {})
end
end

jsons.each do |json|
# Compute representative_file_version property, ANW-1493
fvs = json[:file_versions]

published_representative_fv = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && (fv["is_representative"] == true || fv["is_representative"] == 1) }

published_image_thumbnail_fvs = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && fv["is_representative"] != true && fv["is_representative"] != 1 && fv["use_statement"] == 'image-thumbnail' }

published_fvs = fvs.select { |fv| (fv["publish"] == true || fv["publish"] == 1) && fv["is_representative"] != true && fv["is_representative"] != 1 && fv["use_statement"] != 'image-thumbnail' }

if published_representative_fv.count > 0
json["representative_file_version"] = published_representative_fv.first
elsif published_image_thumbnail_fvs.count > 0
json["representative_file_version"] = published_image_thumbnail_fvs.first
elsif published_fvs.count > 0
json["representative_file_version"] = published_fvs.first
end
end

jsons
end

Expand Down
18 changes: 18 additions & 0 deletions backend/app/model/file_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,22 @@ def self.sequel_to_jsonmodel(objs, opts = {})
jsons
end

def validate
is_published = false
if self[:publish] == true || self[:publish] == 1
is_published = true
end

is_representative = false
if self[:is_representative] == true || self[:is_representative] == 1
is_representative = true
end

if !is_published && is_representative
raise Sequel::ValidationFailed.new("File version must be published to be representative.")
end

super
end

end
113 changes: 113 additions & 0 deletions backend/spec/model_digital_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,119 @@

end

it "doesn't allow an unpublished file_version to be representative" do
json = build(:json_digital_object, {
:publish => true,
:file_versions => [build(:json_file_version, {
:publish => false,
:is_representative => true,
:file_uri => 'http://foo.com/bar1',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar2',
:use_statement => 'image-service'
})
]})

expect {
DigitalObject.create_from_json(json)
}.to raise_error(Sequel::ValidationFailed)

json = build(:json_digital_object, {
:publish => true,
:file_versions => [build(:json_file_version, {
:publish => true,
:is_representative => true,
:file_uri => 'http://foo.com/bar1',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar2',
:use_statement => 'image-service'
})
]})

expect {
DigitalObject.create_from_json(json)
}.not_to raise_error
end

it "has a representative_file_version read-only value of the published file_version marked 'is_representative' if there is a file_version marked 'is_representative'" do
json = build(:json_digital_object, {
:publish => true,
:file_versions => [build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar1',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => true,
:is_representative => true,
:file_uri => 'http://foo.com/bar2',
:use_statement => 'image-service'
})
]})

do1 = DigitalObject.create_from_json(json)
do1_json = DigitalObject.to_jsonmodel(do1.id)

expect(do1_json.representative_file_version).to eq(do1_json.file_versions[1])
end

it "has a representative_file_version read-only value of the first published file_version with a use-statement marked 'image-thumbnail' if there is no file_version marked 'is_representative'" do
json = build(:json_digital_object, {
:publish => true,
:file_versions => [build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar1',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar2',
:use_statement => 'image-thumbnail'
}),
build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar3',
:use_statement => 'image-thumbnail'
})
]})

do1 = DigitalObject.create_from_json(json)
do1_json = DigitalObject.to_jsonmodel(do1.id)

expect(do1_json.representative_file_version).to eq(do1_json.file_versions[1])
end

it "has a representative_file_version read-only value of the first published file_version if there is no file_version marked 'is_representative' and there is no published file_version with a use-statement marked 'image-thumbnail'" do
json = build(:json_digital_object, {
:publish => true,
:file_versions => [build(:json_file_version, {
:publish => false,
:file_uri => 'http://foo.com/bar1',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => true,
:file_uri => 'http://foo.com/bar2',
:use_statement => 'image-service'
}),
build(:json_file_version, {
:publish => false,
:file_uri => 'http://foo.com/bar3',
:use_statement => 'image-thumbnail'
})
]})

do1 = DigitalObject.create_from_json(json)
do1_json = DigitalObject.to_jsonmodel(do1.id)

expect(do1_json.representative_file_version).to eq(do1_json.file_versions[1])
end

it "supports optional captions for file versions" do
obj = create(:json_digital_object, {
Expand Down
4 changes: 4 additions & 0 deletions common/schemas/digital_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
},

"file_versions" => {"type" => "array", "items" => {"type" => "JSONModel(:file_version) object"}},
"representative_file_version" => {
"type" => "JSONModel(:file_version) object",
"readonly" => true
},

"restrictions" => {"type" => "boolean", "default" => false},
"tree" => {
Expand Down

0 comments on commit eaf639e

Please sign in to comment.