Commit 27dd0b8e authored by charlieablett's avatar charlieablett

Use serialization for project boards

- Add serializers and Grape::Entity
- Replace to_json
- Add specs
parent 7468ed5f
...@@ -35,4 +35,12 @@ module BoardsActions ...@@ -35,4 +35,12 @@ module BoardsActions
boards.find(params[:id]) boards.find(params[:id])
end end
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 end
...@@ -69,7 +69,7 @@ module BoardsResponses ...@@ -69,7 +69,7 @@ module BoardsResponses
end end
def serialize_as_json(resource) def serialize_as_json(resource)
resource.as_json(only: [:id]) serializer.represent(resource)
end end
def respond_with(resource) def respond_with(resource)
...@@ -80,4 +80,8 @@ module BoardsResponses ...@@ -80,4 +80,8 @@ module BoardsResponses
end end
end end
end end
def serializer
BoardSerializer.new
end
end end
# frozen_string_literal: true
class BoardSerializer < BaseSerializer
entity BoardSimpleEntity
end
# frozen_string_literal: true
class BoardSimpleEntity < Grape::Entity
expose :id
end
---
title: Refactor Board JSON serialization to use Grape::Entity instead of manual serialization
(to_json)
merge_request:
author:
type: changed
# 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
# 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment