Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
acfe3940
Commit
acfe3940
authored
Oct 28, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add PoC for resource serializers
parent
20235117
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
140 additions
and
0 deletions
+140
-0
app/serializers/base_serializer.rb
app/serializers/base_serializer.rb
+24
-0
app/serializers/entity_request.rb
app/serializers/entity_request.rb
+16
-0
app/serializers/environment_entity.rb
app/serializers/environment_entity.rb
+17
-0
app/serializers/environment_serializer.rb
app/serializers/environment_serializer.rb
+3
-0
app/serializers/project_entity.rb
app/serializers/project_entity.rb
+8
-0
app/serializers/request_aware_entity.rb
app/serializers/request_aware_entity.rb
+11
-0
spec/serializers/entity_request_spec.rb
spec/serializers/entity_request_spec.rb
+26
-0
spec/serializers/environment_serializer_spec.rb
spec/serializers/environment_serializer_spec.rb
+35
-0
No files found.
app/serializers/base_serializer.rb
0 → 100644
View file @
acfe3940
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
app/serializers/entity_request.rb
0 → 100644
View file @
acfe3940
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
app/serializers/environment_entity.rb
0 → 100644
View file @
acfe3940
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
app/serializers/environment_serializer.rb
0 → 100644
View file @
acfe3940
class
EnvironmentSerializer
<
BaseSerializer
entity
EnvironmentEntity
end
app/serializers/project_entity.rb
0 → 100644
View file @
acfe3940
class
ProjectEntity
<
Grape
::
Entity
expose
:id
expose
:name
expose
:test
do
|
project
|
'something'
end
end
app/serializers/request_aware_entity.rb
0 → 100644
View file @
acfe3940
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
spec/serializers/entity_request_spec.rb
0 → 100644
View file @
acfe3940
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
spec/serializers/environment_serializer_spec.rb
0 → 100644
View file @
acfe3940
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment