Implement inventory column data type name mappings [SCI-2772]

This commit is contained in:
Oleksii Kriuchykhin 2018-10-16 15:35:02 +02:00
parent 5190ac7a08
commit ed46115b3f
6 changed files with 31 additions and 21 deletions

View file

@ -84,9 +84,15 @@ module Api
'Wrong object type within parameters'
end
params.require(:data).require(:attributes)
params
.permit(data: { attributes: %i(name data_type) })[:data]
.merge(created_by: @current_user)
new_params = params
.permit(data: { attributes: %i(name data_type) })[:data]
.merge(created_by: @current_user)
if new_params[:attributes][:data_type].present?
new_params[:attributes][:data_type] =
Extends::API_REPOSITORY_DATA_TYPE_MAPPINGS
.key(new_params.dig(:attributes, :data_type))
end
new_params
end
def update_inventory_column_params
@ -94,7 +100,7 @@ module Api
raise ActionController::BadRequest,
'Object ID mismatch in URL and request body'
end
if params.require(:data).require(:attributes).include?(:data_type)
if inventory_column_params[:attributes].include?(:data_type)
raise ActionController::BadRequest,
'Update of data_type attribute is not allowed'
end

View file

@ -119,7 +119,7 @@ module Api
'Wrong object type within parameters'
end
params.require(:data).require(:attributes)
params.permit(data: { attributes: %i(name uid) })[:data]
params.permit(data: { attributes: :name })[:data]
end
def update_inventory_item_params

View file

@ -8,23 +8,17 @@ module Api
attribute :repository_column_id, key: :column_id
def value
value =
case object.value_type
when 'RepositoryTextValue'
object.repository_text_value
when 'RepositoryDateValue'
object.repository_date_value
when 'RepositoryListValue'
object.repository_list_value
when 'RepositoryAssetValue'
object.repository_asset_value
end
ActiveModelSerializers::SerializableResource.new(
value,
object.value,
class_name: object.value_type,
namespace: Api::V1,
adapter: :attribute
).as_json
end
def value_type
Extends::API_REPOSITORY_DATA_TYPE_MAPPINGS[object.value_type]
end
end
end
end

View file

@ -9,8 +9,14 @@ module Api
key: :inventory_list_items,
serializer: InventoryListItemSerializer,
class_name: 'RepositoryListItem',
if: -> { object.data_type == 'RepositoryListValue' &&
!instance_options[:hide_list_items] }
if: (lambda do
object.data_type == 'RepositoryListValue' &&
!instance_options[:hide_list_items]
end)
def data_type
Extends::API_REPOSITORY_DATA_TYPE_MAPPINGS[object.data_type]
end
end
end
end

View file

@ -66,6 +66,11 @@ class Extends
# Array used for injecting names of additional authentication methods for API
API_PLUGABLE_AUTH_METHODS = [:azure_jwt_auth]
API_REPOSITORY_DATA_TYPE_MAPPINGS = { 'RepositoryTextValue' => 'text',
'RepositoryDateValue' => 'date',
'RepositoryListValue' => 'list',
'RepositoryAssetValue' => 'file' }
OMNIAUTH_PROVIDERS = [:linkedin]
INITIAL_USER_OPTIONS = {}

View file

@ -181,8 +181,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
{ type: 'inventory_columns',
attributes: {
name: Faker::Name.unique.name,
data_type:
RepositoryColumn.data_types.values.first
data_type: 'text'
} } }
end