From d5e5631def3e26dbcde48b3a29feb62a2b9a5b86 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Tue, 25 Jun 2024 10:27:17 -0500 Subject: [PATCH] Display withdrawn attribute on user version index --- app/controllers/user_versions_controller.rb | 13 +------------ app/models/user_version.rb | 8 ++++++++ spec/requests/user_versions_inventory_spec.rb | 6 ++++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/controllers/user_versions_controller.rb b/app/controllers/user_versions_controller.rb index ad3ba0809..cf6c4a8e4 100644 --- a/app/controllers/user_versions_controller.rb +++ b/app/controllers/user_versions_controller.rb @@ -5,7 +5,7 @@ class UserVersionsController < ApplicationController def index repository_object = RepositoryObject.find_by!(external_identifier: params[:object_id]) - render json: { user_versions: user_versions_content(repository_object.user_versions) } + render json: { user_versions: repository_object.user_versions.map(&:as_json) } end def show @@ -15,15 +15,4 @@ def show rescue RepositoryObjectVersion::NoCocina json_api_error(status: :not_found, message: 'No Cocina for specified version') end - - private - - def user_versions_content(user_versions) - user_versions.map do |user_version| - { - userVersion: user_version.version, - version: user_version.repository_object_version.version - } - end - end end diff --git a/app/models/user_version.rb b/app/models/user_version.rb index 0c0156065..098bd7c43 100644 --- a/app/models/user_version.rb +++ b/app/models/user_version.rb @@ -10,4 +10,12 @@ def repository_object_version_is_closed # Validate that the repository object version is closed errors.add(:repository_object_version, 'cannot set a user version to an open RepositoryObjectVersion') if repository_object_version.open? end + + def as_json + { + userVersion: version, + version: repository_object_version.version, + withdrawn: + } + end end diff --git a/spec/requests/user_versions_inventory_spec.rb b/spec/requests/user_versions_inventory_spec.rb index 5b4974078..62a025e9c 100644 --- a/spec/requests/user_versions_inventory_spec.rb +++ b/spec/requests/user_versions_inventory_spec.rb @@ -18,8 +18,10 @@ headers: { 'Authorization' => "Bearer #{jwt}" } expect(response).to have_http_status(:ok) - expect(response.body).to eq '{"user_versions":[{"userVersion":1,"version":1},' \ - '{"userVersion":2,"version":2}]}' + expect(response.parsed_body).to eq('user_versions' => [ + { 'userVersion' => 1, 'version' => 1, 'withdrawn' => false }, + { 'userVersion' => 2, 'version' => 2, 'withdrawn' => false } + ]) end end end