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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
030adf12
Commit
030adf12
authored
Feb 02, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Encapsulate reused pagination component in a class
parent
ac060701
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
28 deletions
+42
-28
app/serializers/environment_serializer.rb
app/serializers/environment_serializer.rb
+9
-1
app/serializers/paginator.rb
app/serializers/paginator.rb
+23
-0
app/serializers/pipeline_serializer.rb
app/serializers/pipeline_serializer.rb
+10
-27
No files found.
app/serializers/environment_serializer.rb
View file @
030adf12
...
@@ -7,12 +7,20 @@ class EnvironmentSerializer < BaseSerializer
...
@@ -7,12 +7,20 @@ class EnvironmentSerializer < BaseSerializer
tap
{
@itemize
=
true
}
tap
{
@itemize
=
true
}
end
end
def
with_pagination
(
request
,
response
)
tap
{
@paginator
=
Paginator
.
new
(
request
,
response
)
}
end
def
itemized?
def
itemized?
@itemize
@itemize
end
end
def
paginated?
defined?
(
@paginator
)
end
def
represent
(
resource
,
opts
=
{})
def
represent
(
resource
,
opts
=
{})
# resource =
paginate(resource) if paginated?
resource
=
@paginator
.
paginate
(
resource
)
if
paginated?
if
itemized?
if
itemized?
itemize
(
resource
).
map
do
|
item
|
itemize
(
resource
).
map
do
|
item
|
...
...
app/serializers/paginator.rb
0 → 100644
View file @
030adf12
class
Paginator
include
API
::
Helpers
::
Pagination
def
initialize
(
request
,
response
)
@request
=
request
@response
=
response
end
private
# Methods needed by `API::Helpers::Pagination`
#
attr_reader
:request
def
params
@request
.
query_parameters
end
def
header
(
header
,
value
)
@response
.
headers
[
header
]
=
value
end
end
app/serializers/pipeline_serializer.rb
View file @
030adf12
class
PipelineSerializer
<
BaseSerializer
class
PipelineSerializer
<
BaseSerializer
class
InvalidResourceError
<
StandardError
;
end
class
InvalidResourceError
<
StandardError
;
end
include
API
::
Helpers
::
Pagination
Struct
.
new
(
'Pagination'
,
:request
,
:response
)
entity
PipelineEntity
entity
PipelineEntity
def
with_pagination
(
request
,
response
)
tap
{
@paginator
=
Paginator
.
new
(
request
,
response
)
}
end
def
paginated?
defined?
(
@paginator
)
end
def
represent
(
resource
,
opts
=
{})
def
represent
(
resource
,
opts
=
{})
if
paginated?
if
paginated?
raise
InvalidResourceError
unless
resource
.
respond_to?
(
:page
)
raise
InvalidResourceError
unless
resource
.
respond_to?
(
:page
)
super
(
paginate
(
resource
.
includes
(
project: :namespace
)),
opts
)
resource
=
resource
.
includes
(
project: :namespace
)
super
(
@paginator
.
paginate
(
resource
),
opts
)
else
else
super
(
resource
,
opts
)
super
(
resource
,
opts
)
end
end
end
end
def
paginated?
defined?
(
@pagination
)
end
def
with_pagination
(
request
,
response
)
tap
{
@pagination
=
Struct
::
Pagination
.
new
(
request
,
response
)
}
end
private
# Methods needed by `API::Helpers::Pagination`
#
def
params
@pagination
.
request
.
query_parameters
end
def
request
@pagination
.
request
end
def
header
(
header
,
value
)
@pagination
.
response
.
headers
[
header
]
=
value
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