Commit acfe3940 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add PoC for resource serializers

parent 20235117
class BaseSerializer
def initialize(request = {})
@request = EntityRequest.new(request)
@opts = { request: @request }
end
def set(opts)
@request.merge!(opts)
self
end
def represent(resource, opts = {})
self.class.entity_class
.represent(resource, @opts.reverse_merge(opts))
end
def self.entity(entity_class)
@entity_class ||= entity_class
end
def self.entity_class
@entity_class
end
end
class EntityRequest
# We use EntityRequest object to collect parameters and variables
# from the controller. Because options that are being passed to the entity
# do appear in each entity object in the chain, we need a way to pass data
# that is present in the controller (see #20045).
#
def initialize(parameters)
merge!(parameters)
end
def merge!(parameters)
parameters.each do |key, value|
define_singleton_method(key) { value }
end
end
end
class EnvironmentEntity < Grape::Entity
include RequestAwareEntity
include Gitlab::Routing.url_helpers
expose :id
expose :name
expose :project, with: ProjectEntity
expose :last_deployment,
as: :deployment,
using: API::Entities::Deployment
expose :environment_path
def environment_path
request.path
end
end
class EnvironmentSerializer < BaseSerializer
entity EnvironmentEntity
end
class ProjectEntity < Grape::Entity
expose :id
expose :name
expose :test do |project|
'something'
end
end
module RequestAwareEntity
# We use SerializableRequest class to collect parameters and variables
# from the controller. Because options that are being passed to the entity
# are appear in each entity in the chain, we need a way to access data
# that is present in the controller (see #20045).
#
def request
options[:request] ||
raise(StandardError, 'Request not set!!')
end
end
require 'spec_helper'
describe EntityRequest do
subject do
described_class.new(user: 'user', project: 'some project')
end
describe 'methods created' do
it 'defines accessible attributes' do
expect(subject.user).to eq 'user'
expect(subject.project).to eq 'some project'
end
it 'raises error when attribute is not defined' do
expect { subject.some_method }.to raise_error NoMethodError
end
end
describe '#merge!' do
before { subject.merge!(build: 'some build') }
it 'appends parameters' do
expect(subject.build).to eq 'some build'
end
end
end
require 'spec_helper'
describe EnvironmentSerializer do
let(:serializer) do
described_class.new(path: 'some path').represent(resource)
end
context 'when there is a single object provided' do
let(:resource) { create(:environment) }
it 'shows json' do
puts serializer.to_json
end
it 'it generates payload for single object' do
expect(parsed_json).to be_an_instance_of Hash
end
end
context 'when there is a collection of objects provided' do
let(:resource) { create_list(:environment, 2) }
it 'shows json' do
puts serializer.to_json
end
it 'generates payload for collection' do
expect(parsed_json).to be_an_instance_of Array
end
end
def parsed_json
JSON.parse(serializer.to_json)
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