api-pagination 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6af37b20cc366bdb21326a922f7d2fd145178307
4
- data.tar.gz: adfcf9ea355a22dbeb2c739f69d23af8767000c1
3
+ metadata.gz: 2c576834fa7e045c8227f8ac9eb178dedc241533
4
+ data.tar.gz: 6c620fe958d4b4c87e8df6d28faf4a24c500e2e4
5
5
  SHA512:
6
- metadata.gz: 4e0e335c9901191fd8f85202ff25967e09574bc1fb6fdd574a051e0aeb8c0865f17893ec423fee635ab23a3a278025fe82adb5c50a327ff87b288b4ac9aafca5
7
- data.tar.gz: f230b0babeb25fffcd26542ba373389a2e62188cd8a6fd3c963719e291f963b121f554e19d71f50de77b08459f735d6b8ed272cef15f589be72679b86f7687bf
6
+ metadata.gz: b22e56913a84c875f177f1e3acb401bcb4decf175ed31023efe6cfdc33782d7d67e1b84e85cf8f0a9454181aa2284047d24c0f8c43b9b7328266c8e28500beaf
7
+ data.tar.gz: c23680b6346a5dab04bbecfd6a18ec4d54190f2907b488936743108eaf4657b66d02724ab64feab558f731ca80e1287784c3fa2c7e7634df163e44b167a3c38a
@@ -7,7 +7,7 @@ module ApiPagination
7
7
 
8
8
  def paginate(collection, options = {})
9
9
  options[:page] ||= 1
10
- options[:per_page] ||= 25
10
+ options[:per_page] = (options[:per_page].to_i <= 0 ? 25 : options[:per_page])
11
11
 
12
12
  case ApiPagination.paginator
13
13
  when :kaminari
@@ -1,8 +1,8 @@
1
1
  module ApiPagination
2
2
  class Version
3
3
  MAJOR = 3
4
- MINOR = 0
5
- PATCH = 2
4
+ MINOR = 1
5
+ PATCH = 0
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, PATCH].join('.')
@@ -2,10 +2,15 @@ module Rails
2
2
  module Pagination
3
3
  protected
4
4
 
5
- def paginate(options)
5
+ def paginate(*options_or_collection)
6
+ options = options_or_collection.extract_options!
7
+ collection = options_or_collection.first
8
+
9
+ return _paginate_collection(collection, options) if collection
10
+
6
11
  collection = options[:json] || options[:xml]
12
+ collection = _paginate_collection(collection, options)
7
13
 
8
- collection = _paginate_collection(collection, options)
9
14
  options[:json] = collection if options[:json]
10
15
  options[:xml] = collection if options[:xml]
11
16
 
@@ -53,6 +53,16 @@ describe NumbersController, :type => :controller do
53
53
  it_behaves_like 'an endpoint with a middle page'
54
54
  end
55
55
  end
56
+
57
+ context 'providing a block' do
58
+ it 'yields to the block instead of implicitly rendering' do
59
+ get :index_with_custom_render, :count => 100
60
+
61
+ json = { numbers: (1..10).map { |n| { number: n } } }.to_json
62
+
63
+ expect(response.body).to eq(json)
64
+ end
65
+ end
56
66
  end
57
67
  end
58
68
 
@@ -24,7 +24,7 @@ if ApiPagination.paginator == :will_paginate
24
24
 
25
25
  it 'returns a Sequel::Dataset' do
26
26
  collection = ApiPagination.paginate(people)
27
- expect(collection.kind_of?(Sequel::Dataset)).to be_true
27
+ expect(collection.kind_of?(Sequel::Dataset)).to be_truthy
28
28
  end
29
29
  end
30
30
  end
@@ -1,6 +1,3 @@
1
- require 'coveralls'
2
- Coveralls.wear_merged!
3
-
4
1
  require 'support/numbers_controller'
5
2
  require 'support/numbers_api'
6
3
  require 'api-pagination'
@@ -39,14 +39,25 @@ module ControllerExampleGroup
39
39
  end
40
40
 
41
41
  Rails.application.routes.draw do
42
- resources :numbers, :only => [:index]
42
+ resources :numbers, :only => [:index] do
43
+ get :index_with_custom_render, on: :collection
44
+ end
45
+ end
46
+
47
+ class NumbersSerializer
48
+ def initialize(numbers)
49
+ @numbers = numbers
50
+ end
51
+
52
+ def to_json(options = {})
53
+ { numbers: @numbers.map { |n| { number: n } } }.to_json
54
+ end
43
55
  end
44
56
 
45
57
  class NumbersController < ActionController::Base
46
58
  include Rails.application.routes.url_helpers
47
59
 
48
60
  def index
49
- page = params.fetch(:page, 1).to_i
50
61
  total = params.fetch(:count).to_i
51
62
 
52
63
  if params[:with_headers]
@@ -57,4 +68,12 @@ class NumbersController < ActionController::Base
57
68
 
58
69
  paginate :json => (1..total).to_a, :per_page => 10
59
70
  end
71
+
72
+ def index_with_custom_render
73
+ total = params.fetch(:count).to_i
74
+ numbers = (1..total).to_a
75
+ numbers = paginate numbers, :per_page => 10
76
+
77
+ render json: NumbersSerializer.new(numbers)
78
+ end
60
79
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-23 00:00:00.000000000 Z
11
+ date: 2014-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: grape
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.0.0
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: sequel
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -151,4 +137,3 @@ test_files:
151
137
  - spec/support/shared_examples/first_page.rb
152
138
  - spec/support/shared_examples/last_page.rb
153
139
  - spec/support/shared_examples/middle_page.rb
154
- has_rdoc: