Use serialization for project boards

- Add serializers and Grape::Entity
- Replace to_json
- Add specs
This commit is contained in:
charlieablett 2019-06-06 20:13:18 +12:00
parent 7468ed5fd2
commit 27dd0b8e36
7 changed files with 72 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class BoardSerializer < BaseSerializer
entity BoardSimpleEntity
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class BoardSimpleEntity < Grape::Entity
expose :id
end

View File

@ -0,0 +1,6 @@
---
title: Refactor Board JSON serialization to use Grape::Entity instead of manual serialization
(to_json)
merge_request:
author:
type: changed

View File

@ -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

View File

@ -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