Update GET /inventory_items, GET /inventory_items/:id endpoints

This commit is contained in:
Luka Murn 2018-10-06 18:37:40 +02:00
parent 9e78de6ef2
commit d235dd7807
4 changed files with 23 additions and 15 deletions

View file

@ -16,9 +16,10 @@ module Api
repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES
).page(params.dig(:page, :number))
.per(params.dig(:page, :size))
incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil
render jsonapi: items,
each_serializer: InventoryItemSerializer,
include: :inventory_cells
include: incl
end
def create

View file

@ -7,12 +7,6 @@ module Api
attributes :id, :value_type, :value
attribute :repository_column_id, key: :column_id
def value_type
type_id = RepositoryColumn
.data_types[object.repository_column.data_type]
I18n.t("api.v1.inventory_data_types.t#{type_id}")
end
def value
value =
case object.value_type

View file

@ -7,7 +7,8 @@ module Api
attributes :name
has_many :repository_cells, key: :inventory_cells,
serializer: InventoryCellSerializer,
class_name: 'RepositoryCell'
class_name: 'RepositoryCell',
unless: -> { object.repository_cells.empty? }
end
end
end

View file

@ -75,6 +75,24 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
include: :inventory_cells)
.as_json[:data]
)
expect(hash_body).not_to include('included')
end
it 'Response with correct inventory items, included cells' do
hash_body = nil
get api_v1_team_inventory_items_path(
team_id: @teams.first.id,
inventory_id: @teams.first.repositories.first.id,
include: 'inventory_cells'
), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(@valid_inventory.repository_rows.limit(10),
each_serializer: Api::V1::InventoryItemSerializer,
include: :inventory_cells)
.as_json[:data]
)
expect(hash_body[:included]).to match(
ActiveModelSerializers::SerializableResource
.new(@valid_inventory.repository_rows.limit(10),
@ -98,13 +116,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
include: :inventory_cells)
.as_json[:data]
)
expect(hash_body[:included]).to match(
ActiveModelSerializers::SerializableResource
.new(@valid_inventory.repository_rows.limit(100),
each_serializer: Api::V1::InventoryItemSerializer,
include: :inventory_cells)
.as_json[:included]
)
expect(hash_body).not_to include('included')
end
it 'When invalid request, user in not member of the team' do