Skip to content

Instantly share code, notes, and snippets.

@sshaw
Last active September 14, 2018 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshaw/83f7ad7ce9c8f92a833f6d6530a2495c to your computer and use it in GitHub Desktop.
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
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
#
# 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
@toshimaru
Copy link

Why not open source it?

@sshaw
Copy link
Author

sshaw commented Jul 24, 2016

Why not open source it?

What do you mean, this is open sourced..?

@toshimaru
Copy link

toshimaru commented Aug 16, 2016

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?

@sshaw
Copy link
Author

sshaw commented Aug 19, 2016

You know you can do this?

gem "page_number", :gist => "83f7ad7ce9c8f92a833f6d6530a2495c"

Does that work for you?

Checkout Bundler's docs on gist source.

@toshimaru
Copy link

toshimaru commented Aug 29, 2016

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.

@sshaw
Copy link
Author

sshaw commented Mar 18, 2017

@toshimaru
Copy link

Thanks ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment