Use serialization for project boards
- Add serializers and Grape::Entity - Replace to_json - Add specs
This commit is contained in:
parent
7468ed5fd2
commit
27dd0b8e36
|
@ -35,4 +35,12 @@ module BoardsActions
|
|||
boards.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
def serializer
|
||||
BoardSerializer.new(current_user: current_user)
|
||||
end
|
||||
|
||||
def serialize_as_json(resource)
|
||||
serializer.represent(resource, serializer: 'board', include_full_project_path: board.group_board?)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -69,7 +69,7 @@ module BoardsResponses
|
|||
end
|
||||
|
||||
def serialize_as_json(resource)
|
||||
resource.as_json(only: [:id])
|
||||
serializer.represent(resource)
|
||||
end
|
||||
|
||||
def respond_with(resource)
|
||||
|
@ -80,4 +80,8 @@ module BoardsResponses
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def serializer
|
||||
BoardSerializer.new
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class BoardSerializer < BaseSerializer
|
||||
entity BoardSimpleEntity
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class BoardSimpleEntity < Grape::Entity
|
||||
expose :id
|
||||
end
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: Refactor Board JSON serialization to use Grape::Entity instead of manual serialization
|
||||
(to_json)
|
||||
merge_request:
|
||||
author:
|
||||
type: changed
|
|
@ -0,0 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe BoardsResponses do
|
||||
let(:controller_class) do
|
||||
Class.new do
|
||||
include BoardsResponses
|
||||
end
|
||||
end
|
||||
|
||||
subject(:controller) { controller_class.new }
|
||||
|
||||
describe '#serialize_as_json' do
|
||||
let!(:board) { create(:board) }
|
||||
|
||||
it 'serializes properly' do
|
||||
expected = { id: board.id }
|
||||
|
||||
expect(subject.serialize_as_json(board).to_h).to include(expected)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe BoardSerializer do
|
||||
let(:resource) { create(:board) }
|
||||
let(:json_entity) do
|
||||
described_class.new
|
||||
.represent(resource, serializer: serializer)
|
||||
.with_indifferent_access
|
||||
end
|
||||
|
||||
context 'serialization' do
|
||||
let(:serializer) { 'board' }
|
||||
|
||||
it 'matches issue_sidebar json schema' do
|
||||
expect(json_entity).to match_schema('board')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue