From 27dd0b8e3609aae92b8e59dd202a999fc1db5a4e Mon Sep 17 00:00:00 2001 From: charlieablett Date: Thu, 6 Jun 2019 20:13:18 +1200 Subject: [PATCH 1/3] Use serialization for project boards - Add serializers and Grape::Entity - Replace to_json - Add specs --- app/controllers/concerns/boards_actions.rb | 8 +++++++ app/controllers/concerns/boards_responses.rb | 6 ++++- app/serializers/board_serializer.rb | 5 ++++ app/serializers/board_simple_entity.rb | 5 ++++ ...e-serializers-for-project-group-boards.yml | 6 +++++ .../concerns/boards_responses_spec.rb | 23 +++++++++++++++++++ spec/serializers/board_serializer_spec.rb | 20 ++++++++++++++++ 7 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/serializers/board_serializer.rb create mode 100644 app/serializers/board_simple_entity.rb create mode 100644 changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml create mode 100644 spec/controllers/concerns/boards_responses_spec.rb create mode 100644 spec/serializers/board_serializer_spec.rb diff --git a/app/controllers/concerns/boards_actions.rb b/app/controllers/concerns/boards_actions.rb index ed7ea2f0e04..e4123d87137 100644 --- a/app/controllers/concerns/boards_actions.rb +++ b/app/controllers/concerns/boards_actions.rb @@ -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 diff --git a/app/controllers/concerns/boards_responses.rb b/app/controllers/concerns/boards_responses.rb index 8b191c86397..ce2740147f7 100644 --- a/app/controllers/concerns/boards_responses.rb +++ b/app/controllers/concerns/boards_responses.rb @@ -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 diff --git a/app/serializers/board_serializer.rb b/app/serializers/board_serializer.rb new file mode 100644 index 00000000000..70a4c9ae282 --- /dev/null +++ b/app/serializers/board_serializer.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class BoardSerializer < BaseSerializer + entity BoardSimpleEntity +end diff --git a/app/serializers/board_simple_entity.rb b/app/serializers/board_simple_entity.rb new file mode 100644 index 00000000000..f297d993e27 --- /dev/null +++ b/app/serializers/board_simple_entity.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class BoardSimpleEntity < Grape::Entity + expose :id +end diff --git a/changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml b/changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml new file mode 100644 index 00000000000..de67944b7b7 --- /dev/null +++ b/changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml @@ -0,0 +1,6 @@ +--- +title: Refactor Board JSON serialization to use Grape::Entity instead of manual serialization + (to_json) +merge_request: +author: +type: changed diff --git a/spec/controllers/concerns/boards_responses_spec.rb b/spec/controllers/concerns/boards_responses_spec.rb new file mode 100644 index 00000000000..1c837fe8755 --- /dev/null +++ b/spec/controllers/concerns/boards_responses_spec.rb @@ -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 diff --git a/spec/serializers/board_serializer_spec.rb b/spec/serializers/board_serializer_spec.rb new file mode 100644 index 00000000000..8a633e46316 --- /dev/null +++ b/spec/serializers/board_serializer_spec.rb @@ -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 From 714018145cb0ea3265dcfa64c9b4baa33dee588f Mon Sep 17 00:00:00 2001 From: charlieablett Date: Fri, 7 Jun 2019 13:56:40 +1200 Subject: [PATCH 2/3] Add `to_json` to `represent` method call --- app/controllers/concerns/boards_responses.rb | 2 +- ....yml => 3861-use-serializers-for-project-group-boards.yml} | 0 spec/controllers/concerns/boards_responses_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename changelogs/unreleased/{ce-3861-use-serializers-for-project-group-boards.yml => 3861-use-serializers-for-project-group-boards.yml} (100%) diff --git a/app/controllers/concerns/boards_responses.rb b/app/controllers/concerns/boards_responses.rb index ce2740147f7..128409a2eef 100644 --- a/app/controllers/concerns/boards_responses.rb +++ b/app/controllers/concerns/boards_responses.rb @@ -69,7 +69,7 @@ module BoardsResponses end def serialize_as_json(resource) - serializer.represent(resource) + serializer.represent(resource).to_json end def respond_with(resource) diff --git a/changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml b/changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml similarity index 100% rename from changelogs/unreleased/ce-3861-use-serializers-for-project-group-boards.yml rename to changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml diff --git a/spec/controllers/concerns/boards_responses_spec.rb b/spec/controllers/concerns/boards_responses_spec.rb index 1c837fe8755..c080ddf896f 100644 --- a/spec/controllers/concerns/boards_responses_spec.rb +++ b/spec/controllers/concerns/boards_responses_spec.rb @@ -15,9 +15,9 @@ describe BoardsResponses do let!(:board) { create(:board) } it 'serializes properly' do - expected = { id: board.id } + expected = { "id" => board.id } - expect(subject.serialize_as_json(board).to_h).to include(expected) + expect(JSON.parse(subject.serialize_as_json(board))).to include(expected) end end end From 0c29aa0da38104c4c65cbadcc40abe25e56693aa Mon Sep 17 00:00:00 2001 From: charlieablett Date: Mon, 10 Jun 2019 22:30:00 +1200 Subject: [PATCH 3/3] Add `to_json` to `represent` method call --- app/controllers/concerns/boards_responses.rb | 2 +- .../3861-use-serializers-for-project-group-boards.yml | 6 ------ spec/controllers/concerns/boards_responses_spec.rb | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) delete mode 100644 changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml diff --git a/app/controllers/concerns/boards_responses.rb b/app/controllers/concerns/boards_responses.rb index 128409a2eef..7625600e452 100644 --- a/app/controllers/concerns/boards_responses.rb +++ b/app/controllers/concerns/boards_responses.rb @@ -69,7 +69,7 @@ module BoardsResponses end def serialize_as_json(resource) - serializer.represent(resource).to_json + serializer.represent(resource).as_json end def respond_with(resource) diff --git a/changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml b/changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml deleted file mode 100644 index de67944b7b7..00000000000 --- a/changelogs/unreleased/3861-use-serializers-for-project-group-boards.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -title: Refactor Board JSON serialization to use Grape::Entity instead of manual serialization - (to_json) -merge_request: -author: -type: changed diff --git a/spec/controllers/concerns/boards_responses_spec.rb b/spec/controllers/concerns/boards_responses_spec.rb index c080ddf896f..bdebdf94761 100644 --- a/spec/controllers/concerns/boards_responses_spec.rb +++ b/spec/controllers/concerns/boards_responses_spec.rb @@ -17,7 +17,7 @@ describe BoardsResponses do it 'serializes properly' do expected = { "id" => board.id } - expect(JSON.parse(subject.serialize_as_json(board))).to include(expected) + expect(subject.serialize_as_json(board)).to include(expected) end end end