Last active
September 14, 2018 10:29
-
-
Save sshaw/83f7ad7ce9c8f92a833f6d6530a2495c to your computer and use it in GitHub Desktop.
Ruby utility methods for pagination page and per page that make sure you'll always have a valid number. Use them your controllers or model or anywhere where you process page info. Moved to https://github.com/sshaw/page_number.git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = "page_number" | |
s.version = "0.0.2" | |
s.date = "2016-09-19" | |
s.summary = "Utility methods for pagination page and per page that make sure you'll always have a valid number." | |
s.description =<<-DESC | |
Utility methods for pagination page and per page that make sure you'll always have a valid number. | |
Use them your controllers or model or anywhere where you process page info. | |
DESC | |
s.authors = ["Skye Shaw"] | |
s.email = "skye.shaw@gmail.com" | |
s.files = Dir["*.rb"] | |
s.require_paths = ["."] | |
s.homepage = "https://gist.github.com/sshaw/83f7ad7ce9c8f92a833f6d6530a2495c" | |
s.license = "MIT" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Utility methods for pagination page, and per page values. | |
# https://gist.github.com/sshaw/83f7ad7ce9c8f92a833f6d6530a2495c | |
# | |
# By: Skye Shaw (https://github.com/sshaw) | |
# Date: 2016-06-20 | |
# | |
# === Usage | |
# | |
# require "page_number" | |
# | |
# class BaseController | |
# include PageNumber | |
# | |
# protected | |
# | |
# def page_sizes | |
# [ 25, 50, 100 ] | |
# end | |
# end | |
# | |
# class SubController < BaseController | |
# def action | |
# User.all(:page => page(params[:page]), :per_page => per_page(params[:per_page])) | |
# end | |
# end | |
# | |
module PageNumber | |
def page(n) | |
n = __int(n) | |
return default_page if n < 1 | |
max_page_number && n > max_page_number ? max_page_number : n | |
end | |
alias :__page :page | |
def per_page(n) | |
n = __int(n) | |
return default_per_page if n < 1 | |
page_sizes && !page_sizes.include?(n) ? default_per_page : n | |
end | |
alias :__per_page :per_page | |
# Override these as you see fit. | |
protected | |
def default_per_page | |
10 | |
end | |
def default_page | |
1 | |
end | |
def max_page_number | |
end | |
def page_sizes | |
end | |
private | |
def __int(n) | |
case | |
when n.respond_to?(:to_i) | |
n.to_i | |
when n.respond_to?(:to_int) | |
n.to_int | |
else | |
-1 | |
end | |
end | |
end |
You know you can do this?
gem "page_number", :gist => "83f7ad7ce9c8f92a833f6d6530a2495c"
Does that work for you?
Checkout Bundler's docs on gist source.
Yes, I know, and it works for me. (Thanks for the information!)
But what if this gist is changed or removed unexpectedly? Ruby gem is more trustable for gem users, I guess.
Thanks ❤️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I mean, I'd like you to open source it as a rubygem.
https://rubygems.org/search?utf8=%E2%9C%93&query=page_number
I wanna use it just by doing
gem 'page_number'
. :)Or can I upload this on rubygem?