rails_api_paginate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 666081da8f1a0c52c4d1b349a72138c69ce223661ad8037dae394e1b88f32680
4
+ data.tar.gz: aac8be088b54d75d201f3b141da8f117db4a93cfc05699466628c71c491bea9a
5
+ SHA512:
6
+ metadata.gz: 1466f6f5184402b475b88ad5a4f34073992e3cff1bce88cef1b61ec6663b98615c357ada2705ba225239b8ee1cf37c31410a3fdc9f5aebd0cca99e3589da1dc1
7
+ data.tar.gz: fbd222e09f91032fa73287a561f86b2fadd4683182b55966d153464477277feaa56804dd2f546a067248065b2fb264b25ba081afca35a3fae51bb04ad8820893
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_api_paginate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Usman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # RailsApiPaginate
2
+
3
+ Add pagination support to rails API-Only applications for ActiveRecord relations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails_api_paginate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Next, you need to run the generator:
18
+
19
+ $ rails g rails_api_paginate:install
20
+
21
+ ## Usage
22
+
23
+ Include RailsApiPaginate in controller where you need to paginate ActiveRecord relations.
24
+
25
+ ```ruby
26
+ include RailsApiPaginate
27
+ ```
28
+
29
+ And then:
30
+
31
+ ```ruby
32
+ records, meta = paginate(collection)
33
+ ```
34
+
35
+ Example:
36
+
37
+ ```ruby
38
+ posts = Post.all
39
+ posts, meta = paginate(posts)
40
+ ```
41
+
42
+ The `meta` object is a `Hash` that holds pagination details, e.g.
43
+
44
+ ```ruby
45
+ {
46
+ current_page: 2,
47
+ next_page: 3,
48
+ prev_page: 1,
49
+ total_pages: 4,
50
+ total_items: 100
51
+ }
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/uxman-sherwani/rails_api_paginate.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_api_paginate"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ module RailsApiPaginate
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+ desc 'Creates RailsApiPaginate initializer for your application'
6
+
7
+ def copy_initializer
8
+ template 'rails_api_paginate_config.rb', 'config/initializers/rails_api_paginate_config.rb'
9
+
10
+ puts 'Install complete! Update config/initializers/rails_api_paginate_config.rb to your needs.'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails_api_paginate'
2
+
3
+ RailsApiPaginate.configure do |config|
4
+ config.default_per_page = 25
5
+ config.max_per_page = 100
6
+ end
@@ -0,0 +1,3 @@
1
+ module RailsApiPaginate
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails_api_paginate/version'
2
+
3
+ module RailsApiPaginate
4
+ mattr_accessor :default_per_page, :max_per_page
5
+
6
+ def self.setup
7
+ yield self
8
+ end
9
+
10
+ def paginate(collection)
11
+ current_page = (params[:page].to_i)
12
+ current_page = 1 if current_page < 1
13
+
14
+ per_page = params[:per_page].to_i
15
+ per_page = default_per_page if per_page < 1 || per_page > max_per_page
16
+
17
+ total_items = collection.count
18
+ total_pages = (total_items.to_f / per_page).ceil
19
+
20
+ next_page = current_page + 1 if current_page < total_pages
21
+
22
+ prev_page = current_page - 1 if current_page > 1
23
+ prev_page = total_pages - 1 if current_page > total_pages
24
+
25
+ records = collection.limit(per_page).offset(per_page * (current_page - 1))
26
+
27
+ return records, {
28
+ current_page: current_page,
29
+ next_page: next_page,
30
+ prev_page: prev_page,
31
+ total_pages: total_pages,
32
+ total_items: total_items
33
+ }
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rails_api_paginate/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rails_api_paginate'
7
+ spec.version = RailsApiPaginate::VERSION
8
+ spec.authors = ['Usman']
9
+ spec.email = ['uxman.sherwani@gmail.com']
10
+
11
+ spec.summary = %q{Pagination support for rails API-Only applications.}
12
+ spec.description = %q{Add pagination support to rails API-Only applications for ActiveRecord relations.}
13
+ spec.homepage = 'https://github.com/uxman-sherwani/rails_api_paginate'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_runtime_dependency 'rails', '~> 5.0'
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_api_paginate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Usman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Add pagination support to rails API-Only applications for ActiveRecord
56
+ relations.
57
+ email:
58
+ - uxman.sherwani@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/generators/rails_api_paginate/install_generator.rb
71
+ - lib/generators/templates/rails_api_paginate_config.rb
72
+ - lib/rails_api_paginate.rb
73
+ - lib/rails_api_paginate/version.rb
74
+ - rails_api_paginate.gemspec
75
+ homepage: https://github.com/uxman-sherwani/rails_api_paginate
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Pagination support for rails API-Only applications.
99
+ test_files: []