Commit a5b96417 authored by Shinya Maeda's avatar Shinya Maeda

with_status to represent_status

parent 3b635002
......@@ -18,10 +18,7 @@ class BuildEntity < Grape::Entity
expose :created_at
expose :updated_at
expose :details do
expose :detailed_status, as: :status, with: StatusEntity
end
expose :detailed_status, as: :status, with: StatusEntity
private
......
class BuildSerializer < BaseSerializer
entity BuildEntity
def only_status
tap { @status_only = { only: [{ details: [:status] }] } }
end
def represent(resource, opts = {})
if @status_only.present?
opts.merge!(@status_only)
end
super(resource, opts)
end
def represent_status(resource)
data = represent(resource, { only: [:status] })
data[:status]
end
end
......@@ -11,23 +11,20 @@ class PipelineSerializer < BaseSerializer
@paginator.present?
end
def only_status
tap { @status_only = { only: [{ details: [:status] }] } }
end
def represent(resource, opts = {})
if resource.is_a?(ActiveRecord::Relation)
resource = resource.includes(project: :namespace)
end
if @status_only.present?
opts.merge!(@status_only)
end
if paginated?
super(@paginator.paginate(resource), opts)
else
super(resource, opts)
end
end
def represent_status(resource)
data = represent(resource, { only: [{ details: [:status] }] })
data[:details][:status]
end
end
......@@ -10,16 +10,20 @@ describe BuildSerializer do
subject { serializer.represent(resource) }
describe '#represent' do
context 'when used with status' do
let(:serializer) do
described_class.new(user: user)
.only_status
# TODO:
end
describe '#represent_status' do
context 'when represents only status' do
let(:status) do
Gitlab::Ci::Status::Success.new(double('object'), double('user'))
end
let(:resource) { create(:ci_build) }
let(:resource) { create(:ci_build, status: :success) }
subject { serializer.represent_status(resource) }
it 'serializes only status' do
expect(subject[:details][:status]).not_to be_empty
expect(subject[:details].keys.count).to eq 1
expect(subject[:favicon]).to eq(status.favicon)
end
end
end
......
......@@ -93,17 +93,19 @@ describe PipelineSerializer do
end
end
end
end
context 'when used with status' do
let(:serializer) do
described_class.new(user: user)
.only_status
describe '#represent_status' do
context 'when represents only status' do
let(:status) do
Gitlab::Ci::Status::Success.new(double('object'), double('user'))
end
let(:resource) { create(:ci_empty_pipeline) }
let(:resource) { create(:ci_pipeline, status: :success) }
subject { serializer.represent_status(resource) }
it 'serializes only status' do
expect(subject[:details][:status]).not_to be_empty
expect(subject[:details].keys.count).to eq 1
expect(subject[:favicon]).to eq(status.favicon)
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