diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-02-02 16:37:11 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-02-02 16:37:11 +0300 |
commit | 030adf12ce6dc8e10a9f0bbb34ff55aa818d7aed (patch) | |
tree | f5e505f9bb882b4a38319d7c449f13ebf435185a /app/serializers | |
parent | ac06070147f23909dfb5d3468a17a29e6b0cd447 (diff) |
Encapsulate reused pagination component in a class
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/environment_serializer.rb | 10 | ||||
-rw-r--r-- | app/serializers/paginator.rb | 23 | ||||
-rw-r--r-- | app/serializers/pipeline_serializer.rb | 37 |
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 |