Skip to content

Commit

Permalink
RES-1 add offset paginator; tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
polakowski committed Mar 12, 2018
1 parent 421fad8 commit 137468b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
resourcey (0.0.3)
resourcey (0.1.0)
active_model_serializers (~> 0.10.6)
activerecord (>= 4)
railties (>= 4)
Expand Down
2 changes: 1 addition & 1 deletion lib/resourcey/controller_pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module ControllerPagination

def paginated_resources
return resources unless self.pagination_enabled

paginator = current_paginator_class.new(params)
paginator.paginate(resources)
end
Expand Down
19 changes: 18 additions & 1 deletion lib/resourcey/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(params)
end

def parse_params(params)
params.permit(self.class.allowed_params)
params.permit(self.allowed_params)
end

def paginate(*args)
Expand Down Expand Up @@ -51,3 +51,20 @@ def paginate(scope)

attr_reader :page, :per_page
end

class OffsetPaginator < Resourcey::Paginator
permit_params :offset, :limit

def setup(opts)
@offset = opts[:offset]
@limit = opts[:limit]
end

def paginate(scope)
scope.offset(offset).limit(limit)
end

private

attr_reader :offset, :limit
end
1 change: 1 addition & 0 deletions spec/fixtures/paginators.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'fixtures/paginators/custom_paginator'
require 'fixtures/paginators/invalid_paginator'
5 changes: 2 additions & 3 deletions spec/fixtures/paginators/custom_paginator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class CustomPaginator < Resourcey::Paginator
permit_params :from, :to
permit_params :from_to

def setup(opts)
@from = opts[:from]
@to = opts[:to]
@from, @to = opts[:from_to].split(',').map(&:to_i)
end

def paginate(scope)
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/paginators/invalid_paginator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class InvalidPaginator < Resourcey::Paginator
permit_params :foo

def setup(opts)
@foo = opts[:foo]
end

private

attr_reader :foo
end
16 changes: 16 additions & 0 deletions spec/resourcey/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,21 @@
include('content' => 'four'),
])
end

context 'with offset paginator' do
it 'uses correct paginator' do
PaginatedPostsController.paginate_with :offset

create(:post, content: 'Oo')
create(:post, content: 'Ooo')
create(:post, content: 'Oooo')

expect(OffsetPaginator).to receive(:new).and_call_original

get :index, params: { offset: 1, limit: 1 }

PaginatedPostsController.paginate
end
end
end
end
42 changes: 39 additions & 3 deletions spec/resourcey/paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

describe '#paginate' do
it 'raises error for #setup' do
params = build_params(nil, foo: 'bar')
params = build_params(foo: 'bar')
expect { Resourcey::Paginator.new(params) }.to raise_error(Resourcey::Errors::NotImplemented)
end
end
Expand All @@ -22,7 +22,7 @@
create(:user, name: 'Four')
create(:user, name: 'Five')

params = build_params(nil, page: 2, per_page: 2)
params = build_params(page: 2, per_page: 2)

paginator = PagedPaginator.new(params)
pagination = paginator.paginate(User.all)
Expand All @@ -36,6 +36,30 @@
end
end

describe OffsetPaginator do
describe '#paginate' do
it 'paginates' do
create(:user, name: 'Aaa')
create(:user, name: 'Bbb')
create(:user, name: 'Ccc')
create(:user, name: 'Ddd')
create(:user, name: 'Eee')

params = build_params(offset: 1, limit: 3)

paginator = OffsetPaginator.new(params)
pagination = paginator.paginate(User.all)

expect(pagination.count).to eq 3
expect(pagination).to match_array([
have_attributes(name: 'Bbb'),
have_attributes(name: 'Ccc'),
have_attributes(name: 'Ddd')
])
end
end
end

describe CustomPaginator do
describe '#paginate' do
it 'paginates' do
Expand All @@ -45,7 +69,7 @@
create(:user, name: 'Dd')
create(:user, name: 'Ee')

params = build_params(nil, from: 2, to: 4)
params = build_params(from_to: '2,4')

paginator = CustomPaginator.new(params)
pagination = paginator.paginate(User.all)
Expand All @@ -59,3 +83,15 @@
end
end
end

describe InvalidPaginator do
describe '#paginate' do
it 'throws error on calling paginate' do
params = build_params(foo: 2)

paginator = InvalidPaginator.new(params)

expect { paginator.paginate }.to raise_error(Resourcey::Errors::NotImplemented)
end
end
end
14 changes: 2 additions & 12 deletions spec/support/action_params.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
module Support
module ActionParams
def build_params(parent, params = {})
ActionController::Parameters.new(build_params_hash(parent, params))
end

private

def build_params_hash(parent, params)
return params if parent.nil?

Hash.new.tap do |result|
result[parent] = params
end
def build_params(params = {})
ActionController::Parameters.new(params)
end
end
end

0 comments on commit 137468b

Please sign in to comment.