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
Jérome Perrin
gitlab-ce
Commits
b0a4635b
Commit
b0a4635b
authored
Nov 04, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify implementation of entity serializers
parent
e49fb264
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
14 additions
and
31 deletions
+14
-31
app/serializers/base_serializer.rb
app/serializers/base_serializer.rb
+2
-8
app/serializers/build_entity.rb
app/serializers/build_entity.rb
+1
-1
app/serializers/commit_entity.rb
app/serializers/commit_entity.rb
+3
-3
app/serializers/deployment_entity.rb
app/serializers/deployment_entity.rb
+1
-1
app/serializers/entity_request.rb
app/serializers/entity_request.rb
+0
-4
app/serializers/environment_entity.rb
app/serializers/environment_entity.rb
+1
-1
app/serializers/request_aware_entity.rb
app/serializers/request_aware_entity.rb
+6
-5
spec/serializers/entity_request_spec.rb
spec/serializers/entity_request_spec.rb
+0
-8
No files found.
app/serializers/base_serializer.rb
View file @
b0a4635b
class
BaseSerializer
class
BaseSerializer
def
initialize
(
parameters
=
{})
def
initialize
(
parameters
=
{})
@entity
=
self
.
class
.
entity_class
@request
=
EntityRequest
.
new
(
parameters
)
@request
=
EntityRequest
.
new
(
parameters
)
@opts
=
{
request:
@request
}
end
def
set
(
parameters
)
@request
.
merge!
(
parameters
)
self
end
end
def
represent
(
resource
,
opts
=
{})
def
represent
(
resource
,
opts
=
{})
@entity
.
represent
(
resource
,
@opts
.
reverse_merge
(
opts
))
self
.
class
.
entity_class
.
represent
(
resource
,
opts
.
merge
(
request:
@request
))
end
end
def
self
.
entity
(
entity_class
)
def
self
.
entity
(
entity_class
)
...
...
app/serializers/build_entity.rb
View file @
b0a4635b
...
@@ -19,6 +19,6 @@ class BuildEntity < Grape::Entity
...
@@ -19,6 +19,6 @@ class BuildEntity < Grape::Entity
private
private
def
url_to
(
route
,
build
)
def
url_to
(
route
,
build
)
@urls
.
send
(
"
#{
route
}
_url"
,
build
.
project
.
namespace
,
build
.
project
,
build
)
send
(
"
#{
route
}
_url"
,
build
.
project
.
namespace
,
build
.
project
,
build
)
end
end
end
end
app/serializers/commit_entity.rb
View file @
b0a4635b
...
@@ -4,9 +4,9 @@ class CommitEntity < API::Entities::RepoCommit
...
@@ -4,9 +4,9 @@ class CommitEntity < API::Entities::RepoCommit
expose
:author
,
using:
UserEntity
expose
:author
,
using:
UserEntity
expose
:commit_url
do
|
commit
|
expose
:commit_url
do
|
commit
|
@urls
.
namespace_project_tree_url
(
namespace_project_tree_url
(
@
request
.
project
.
namespace
,
request
.
project
.
namespace
,
@
request
.
project
,
request
.
project
,
id:
commit
.
id
)
id:
commit
.
id
)
end
end
end
end
app/serializers/deployment_entity.rb
View file @
b0a4635b
...
@@ -11,7 +11,7 @@ class DeploymentEntity < Grape::Entity
...
@@ -11,7 +11,7 @@ class DeploymentEntity < Grape::Entity
end
end
expose
:ref_url
do
|
deployment
|
expose
:ref_url
do
|
deployment
|
@urls
.
namespace_project_tree_url
(
namespace_project_tree_url
(
deployment
.
project
.
namespace
,
deployment
.
project
.
namespace
,
deployment
.
project
,
deployment
.
project
,
id:
deployment
.
ref
)
id:
deployment
.
ref
)
...
...
app/serializers/entity_request.rb
View file @
b0a4635b
...
@@ -5,10 +5,6 @@ class EntityRequest
...
@@ -5,10 +5,6 @@ class EntityRequest
# that is present in the controller (see #20045).
# that is present in the controller (see #20045).
#
#
def
initialize
(
parameters
)
def
initialize
(
parameters
)
merge!
(
parameters
)
end
def
merge!
(
parameters
)
parameters
.
each
do
|
key
,
value
|
parameters
.
each
do
|
key
,
value
|
define_singleton_method
(
key
)
{
value
}
define_singleton_method
(
key
)
{
value
}
end
end
...
...
app/serializers/environment_entity.rb
View file @
b0a4635b
...
@@ -10,7 +10,7 @@ class EnvironmentEntity < Grape::Entity
...
@@ -10,7 +10,7 @@ class EnvironmentEntity < Grape::Entity
expose
:stoppable?
expose
:stoppable?
expose
:environment_url
do
|
environment
|
expose
:environment_url
do
|
environment
|
@urls
.
namespace_project_environment_url
(
namespace_project_environment_url
(
environment
.
project
.
namespace
,
environment
.
project
.
namespace
,
environment
.
project
,
environment
.
project
,
environment
)
environment
)
...
...
app/serializers/request_aware_entity.rb
View file @
b0a4635b
module
RequestAwareEntity
module
RequestAwareEntity
attr_reader
:request
extend
ActiveSupport
::
Concern
def
initialize
(
object
,
options
=
{})
included
do
super
(
object
,
options
)
include
Gitlab
::
Routing
.
url_helpers
end
@request
=
options
.
fetch
(
:request
)
def
request
@
urls
=
Gitlab
::
Routing
.
url_helpers
@
options
.
fetch
(
:request
)
end
end
end
end
spec/serializers/entity_request_spec.rb
View file @
b0a4635b
...
@@ -15,12 +15,4 @@ describe EntityRequest do
...
@@ -15,12 +15,4 @@ describe EntityRequest do
expect
{
subject
.
some_method
}.
to
raise_error
NoMethodError
expect
{
subject
.
some_method
}.
to
raise_error
NoMethodError
end
end
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
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