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

Add structural metadata param to additional endpoints #5021

Merged
merged 3 commits into from
Dec 21, 2022
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
2 changes: 1 addition & 1 deletion app/controllers/admin/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def edit
# GET /collections/1/items
def items
mos = paginate @collection.media_objects
render json: mos.to_a.collect{|mo| [mo.id, mo.as_json] }.to_h
render json: mos.to_a.collect { |mo| [mo.id, mo.as_json(include_structure: params[:include_structure] == "true")] }.to_h
end

# POST /collections
Expand Down
7 changes: 2 additions & 5 deletions app/controllers/media_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,8 @@ def custom_update
end

def index
respond_to do |format|
format.json {
paginate json: MediaObject.accessible_by(current_ability, :index)
}
end
mos = paginate MediaObject.accessible_by(current_ability, :index)
render json: mos.to_a.collect { |mo| mo.as_json(include_structure: params[:include_structure] == "true") }
end

def show
Expand Down
21 changes: 21 additions & 0 deletions spec/controllers/admin_collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,27 @@
#TODO add check that mediaobject is serialized to json properly
end

context "with structure" do
let!(:mf_1) { FactoryBot.create(:master_file, :with_structure, media_object: collection.media_objects[0]) }
let!(:mf_2) { FactoryBot.create(:master_file, :with_structure, media_object: collection.media_objects[1]) }

it "should not return structure by default" do
get 'items', params: { id: collection.id, format: 'json' }
expect(JSON.parse(response.body)[collection.media_objects[0].id]["files"][0]["structure"]).to be_blank
expect(JSON.parse(response.body)[collection.media_objects[1].id]["files"][0]["structure"]).to be_blank
end
it "should return structure if requested" do
get 'items', params: { id: collection.id, format: 'json', include_structure: true }
expect(JSON.parse(response.body)[collection.media_objects[0].id]["files"][0]["structure"]).to eq mf_1.structuralMetadata.content
expect(JSON.parse(response.body)[collection.media_objects[1].id]["files"][0]["structure"]).to eq mf_2.structuralMetadata.content
end
it "should not return structure if requested" do
get 'items', params: { id: collection.id, format: 'json', include_structure: false}
expect(JSON.parse(response.body)[collection.media_objects[0].id]["files"][0]["structure"]).not_to eq mf_1.structuralMetadata.content
expect(JSON.parse(response.body)[collection.media_objects[1].id]["files"][0]["structure"]).not_to eq mf_2.structuralMetadata.content
end
end

context 'user is a collection manager' do
let(:manager) { FactoryBot.create(:manager) }
before(:each) do
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/media_objects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,22 @@
expect(json.second['published']).to eq(private_media_object.published?)
expect(json.second['summary']).to eq(private_media_object.abstract)
end

context "with structure" do
let!(:master_file) { FactoryBot.create(:master_file, :with_structure, media_object: media_object) }
it "should not return structure by default" do
get 'index', params: { format: 'json' }
expect(json.first["files"][0]["structure"]).to be_blank
end
it "should return structure if requested" do
get 'index', params: { format: 'json', include_structure: true }
expect(json.first["files"][0]["structure"]).to eq master_file.structuralMetadata.content
end
it "should not return structure if requested" do
get 'index', params: { format: 'json', include_structure: false}
expect(json.first["files"][0]["structure"]).not_to eq master_file.structuralMetadata.content
end
end
end

context 'user is not an administrator' do
Expand Down