summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-02 14:37:11 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-02 14:37:11 +0100
commit030adf12ce6dc8e10a9f0bbb34ff55aa818d7aed (patch)
treef5e505f9bb882b4a38319d7c449f13ebf435185a
parentac06070147f23909dfb5d3468a17a29e6b0cd447 (diff)
downloadgitlab-ce-030adf12ce6dc8e10a9f0bbb34ff55aa818d7aed.tar.gz
Encapsulate reused pagination component in a class
-rw-r--r--app/serializers/environment_serializer.rb10
-rw-r--r--app/serializers/paginator.rb23
-rw-r--r--app/serializers/pipeline_serializer.rb37
3 files changed, 42 insertions, 28 deletions
diff --git a/app/serializers/environment_serializer.rb b/app/serializers/environment_serializer.rb
index 8624392e07f..c556424a414 100644
--- a/app/serializers/environment_serializer.rb
+++ b/app/serializers/environment_serializer.rb
@@ -7,12 +7,20 @@ class EnvironmentSerializer < BaseSerializer
tap { @itemize = true }
end
+ def with_pagination(request, response)
+ tap { @paginator = Paginator.new(request, response) }
+ end
+
def itemized?
@itemize
end
+ def paginated?
+ defined?(@paginator)
+ end
+
def represent(resource, opts = {})
- # resource = paginate(resource) if paginated?
+ resource = @paginator.paginate(resource) if paginated?
if itemized?
itemize(resource).map do |item|
diff --git a/app/serializers/paginator.rb b/app/serializers/paginator.rb
new file mode 100644
index 00000000000..c5e38a9c8b5
--- /dev/null
+++ b/app/serializers/paginator.rb
@@ -0,0 +1,23 @@
+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
diff --git a/app/serializers/pipeline_serializer.rb b/app/serializers/pipeline_serializer.rb
index b2de6c5832e..7c8dfad3b4b 100644
--- a/app/serializers/pipeline_serializer.rb
+++ b/app/serializers/pipeline_serializer.rb
@@ -1,41 +1,24 @@
class PipelineSerializer < BaseSerializer
class InvalidResourceError < StandardError; end
- include API::Helpers::Pagination
- Struct.new('Pagination', :request, :response)
entity PipelineEntity
+ def with_pagination(request, response)
+ tap { @paginator = Paginator.new(request, response) }
+ end
+
+ def paginated?
+ defined?(@paginator)
+ end
+
def represent(resource, opts = {})
if paginated?
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
super(resource, opts)
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