From 1e8261ac2aed059c73ddd4a5af76a5dcc3568f94 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 21 Oct 2019 11:53:18 +0200 Subject: [PATCH] Update InventoryColumns API for new Status Type --- .../api/v1/inventory_columns_controller.rb | 1 + app/models/repository_column.rb | 1 + .../api/v1/inventory_column_serializer.rb | 8 +++++ config/initializers/extends.rb | 3 +- .../v1/inventory_columns_controller_spec.rb | 31 ++++++++++++++++++- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/inventory_columns_controller.rb b/app/controllers/api/v1/inventory_columns_controller.rb index a98316e04..8be48c93d 100644 --- a/app/controllers/api/v1/inventory_columns_controller.rb +++ b/app/controllers/api/v1/inventory_columns_controller.rb @@ -14,6 +14,7 @@ module Api def index columns = @inventory.repository_columns .includes(:repository_list_items) + .includes(:repository_status_items) .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: columns, diff --git a/app/models/repository_column.rb b/app/models/repository_column.rb index dc17dbce6..914d5bc7a 100644 --- a/app/models/repository_column.rb +++ b/app/models/repository_column.rb @@ -27,6 +27,7 @@ class RepositoryColumn < ApplicationRecord scope :list_type, -> { where(data_type: 'RepositoryListValue') } scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') } + scope :status_type, -> { where(data_type: 'RepositoryStatusValue') } def self.name_like(query) where('repository_columns.name ILIKE ?', "%#{query}%") diff --git a/app/serializers/api/v1/inventory_column_serializer.rb b/app/serializers/api/v1/inventory_column_serializer.rb index aee1733fc..e2e3a6a52 100644 --- a/app/serializers/api/v1/inventory_column_serializer.rb +++ b/app/serializers/api/v1/inventory_column_serializer.rb @@ -13,6 +13,14 @@ module Api object.data_type == 'RepositoryListValue' && !instance_options[:hide_list_items] end) + has_many :repository_status_items, + key: :repository_status_items, + serializer: InventoryStatusItemSerializer, + class_name: 'RepositoryStatusItem', + if: (lambda do + object.data_type == 'RepositoryStatusValue' && + !instance_options[:hide_list_items] + end) def data_type Extends::API_REPOSITORY_DATA_TYPE_MAPPINGS[object.data_type] diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 2900d9cb2..992ded3b7 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -72,7 +72,8 @@ class Extends API_REPOSITORY_DATA_TYPE_MAPPINGS = { 'RepositoryTextValue' => 'text', 'RepositoryDateValue' => 'date', 'RepositoryListValue' => 'list', - 'RepositoryAssetValue' => 'file' } + 'RepositoryAssetValue' => 'file', + 'RepositoryStatusValue' => 'status' } OMNIAUTH_PROVIDERS = [:linkedin] diff --git a/spec/requests/api/v1/inventory_columns_controller_spec.rb b/spec/requests/api/v1/inventory_columns_controller_spec.rb index cdf8e0ca7..6aef00ada 100644 --- a/spec/requests/api/v1/inventory_columns_controller_spec.rb +++ b/spec/requests/api/v1/inventory_columns_controller_spec.rb @@ -22,6 +22,10 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do repository: @valid_inventory, data_type: :RepositoryListValue) create(:repository_list_item, repository: @valid_inventory, repository_column: list_column, data: Faker::Name.unique.name) + status_column = create(:repository_column, name: Faker::Name.unique.name, + repository: @valid_inventory, data_type: :RepositoryStatusValue) + create(:repository_status_item, repository: @valid_inventory, + repository_column: status_column, status: Faker::Name.unique.name, icon: 'icon') create(:repository_column, name: Faker::Name.unique.name, repository: @valid_inventory, data_type: :RepositoryAssetValue) @@ -124,8 +128,33 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do ) end + it 'Valid status column response' do + status_column = @valid_inventory.repository_columns.status_type.first + hash_body = nil + get api_v1_team_inventory_column_path( + id: status_column.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(status_column, + serializer: Api::V1::InventoryColumnSerializer) + .as_json[:data] + ) + expect(hash_body[:data]).to include('relationships') + expect(hash_body[:included]).to match( + ActiveModelSerializers::SerializableResource + .new(@valid_inventory.repository_columns.limit(10), + each_serializer: Api::V1::InventoryColumnSerializer, + include: :inventory_status_items) + .as_json[:included] + ) + end + it 'Valid file column response' do - file_column = @valid_inventory.repository_columns.third + file_column = @valid_inventory.repository_columns.asset_type.first hash_body = nil get api_v1_team_inventory_column_path( id: file_column.id,