Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
2 parents d91673f + 5f62aa5 commit 585a4c36cbe5ef4ef6ea7908824315c2f3279de2 Lester Celestial committed Mar 20, 2013
View
@@ -1,8 +1,11 @@
+services: mongodb
+
script: "bundle exec rake spec"
rvm:
- 1.8.7
- 1.9.3
+ - ruby-head
gemfile:
- gemfiles/active_record_32.gemfile
View
@@ -1,3 +1,11 @@
+== 0.14.1
+
+* Changed the default "truncation" String from "..." to … #264 [pjaspers]
+
+* The theme generator is Github API v3 compatible now! #279 [eitoball]
+
+* Made Kaminari.config.default_per_page changeable again #280 [yuki24]
+
== 0.14.0
* Grape framework support! #218 [mrplum]
View
@@ -1,4 +1,4 @@
-source "http://rubygems.org"
+source 'https://rubygems.org'
# Specify your gem's dependencies in kaminari.gemspec
gemspec
View
@@ -62,10 +62,14 @@ Then bundle:
To show a lot more users per each page (change the +per_page+ value)
User.page(7).per(50)
Note that the +per+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you will never actually use +per_page+ without specifying the +page+ number.
-
+
+ If you would like to specify "no limit" while still using the +per+ scope, you can pass +nil+:
+ User.count # => 1000
+ User.page(1).per(nil).size # => 1000
+
* the +padding+ scope
- Occasionally you need to padding a number of records that is not a multiple of the page size.
+ Occasionally you need to pad a number of records that is not a multiple of the page size.
User.page(7).per(50).padding(3)
Note that the +padding+ scope also is not directly defined on the models.
@@ -87,7 +91,7 @@ Run the following generator command, then edit the generated file.
* changing +page_method_name+
- You can change the method name `page` to `bonzo` or `plant` or whatever you like, in order to play nice with existing `page` method or association or scope or any other plugin that defines `page` method on your models.
+ You can change the method name +page+ to +bonzo+ or +plant+ or whatever you like, in order to play nice with existing +page+ method or association or scope or any other plugin that defines +page+ method on your models.
=== Configuring default +per_page+ value for each model
@@ -104,7 +108,7 @@ Run the following generator command, then edit the generated file.
* +max_paginates_per+
You can specify max +per_page+ value per each model using the following declarative DSL.
- If the variable that specified via +per+ scope is more than this variable, max +paginates_per+ is used instead of it. Default value is nil, which means you are not imposing any max +per_page+ value.
+ If the variable that specified via +per+ scope is more than this variable, +max_paginates_per+ is used instead of it. Default value is nil, which means you are not imposing any max +per_page+ value.
class User < ActiveRecord::Base
max_paginates_per 100
end
@@ -132,17 +136,17 @@ Run the following generator command, then edit the generated file.
<%= paginate @users %>
This would output several pagination links such as <tt>« First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »</tt>
-* specifing the "inner window" size (4 by default)
+* specifying the "inner window" size (4 by default)
<%= paginate @users, :window => 2 %>
This would output something like <tt>... 5 6 7 8 9 ...</tt> when 7 is the current page.
-* specifing the "outer window" size (0 by default)
+* specifying the "outer window" size (0 by default)
<%= paginate @users, :outer_window => 3 %>
This would output something like <tt>1 2 3 4 ...(snip)... 17 18 19 20</tt> while having 20 pages in total.
-* outer window can be separetely specified by +left+, +right+ (0 by default)
+* outer window can be separately specified by +left+, +right+ (0 by default)
<%= paginate @users, :left => 1, :right => 3 %>
This would output something like <tt>1 ...(snip)... 18 19 20</tt> while having 20 pages in total.
@@ -165,7 +169,7 @@ Run the following generator command, then edit the generated file.
* the +link_to_next_page+ and +link_to_previous_page+ helper method
<%= link_to_next_page @items, 'Next Page' %>
- This simply renders a link to the next page. This would be helpful for creating "Twitter like" pagination feature.
+ This simply renders a link to the next page. This would be helpful for creating a Twitter-like pagination feature.
* the +page_entries_info+ helper method
@@ -223,7 +227,7 @@ Kaminari includes a handy template generator.
% mkdir my_custom_theme
% cp _*.html.* my_custom_theme/
- Next reference that directory when calling the paginate method:
+ Next, reference that directory when calling the +paginate+ method:
<%= paginate @users, :theme => 'my_custom_theme' %>
@@ -238,22 +242,22 @@ However, the <tt>paginate</tt> helper doesn't automatically handle your Array ob
<tt>Kaminari::paginate_array</tt> method converts your Array object into a paginatable Array that accepts <tt>page</tt> method.
Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
-You can specify the `total_count` value through options Hash. This would be helpful when handling an Array-ish object that has a different `count` value from actual `count` such as RSolr search result or when you need to generate a custom pagination. For example:
+You can specify the +total_count+ value through options Hash. This would be helpful when handling an Array-ish object that has a different +count+ value from actual +count+ such as RSolr search result or when you need to generate a custom pagination. For example:
Kaminari.paginate_array([], total_count: 145).page(params[:page]).per(10)
== Creating friendly URLs and caching
-Because of the `page` parameter and Rails 3 routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your `routes.rb`:
+Because of the +page+ parameter and Rails 3 routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your +routes.rb+:
resources :my_resources do
get 'page/:page', :action => :index, :on => :collection
end
-This will create URLs like `/my_resources/page/33` instead of `/my_resources?page=33`. This is now a friendly URL, but it also has other added benefits...
+This will create URLs like <tt>/my_resources/page/33</tt> instead of <tt>/my_resources?page=33</tt>. This is now a friendly URL, but it also has other added benefits...
-Because the `page` parameter is now a URL segment, we can leverage on Rails page caching[http://guides.rubyonrails.org/caching_with_rails.html#page-caching]!
+Because the +page+ parameter is now a URL segment, we can leverage on Rails page caching[http://guides.rubyonrails.org/caching_with_rails.html#page-caching]!
-NOTE: In this example, I've pointed the route to my `:index` action. You may have defined a custom pagination action in your controller - you should point `:action => :your_custom_action` instead.
+NOTE: In this example, I've pointed the route to my <tt>:index</tt> action. You may have defined a custom pagination action in your controller - you should point <tt>:action => :your_custom_action</tt> instead.
== Sinatra/Padrino support
@@ -7,5 +7,5 @@
remote: data-remote
-%>
<span class="last">
- <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} %>
+ <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, :remote => remote %>
</span>
@@ -6,4 +6,4 @@
-# per_page: number of items to fetch per page
-# remote: data-remote
%span.last
- = link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote}
+ = link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, :remote => remote
@@ -6,5 +6,5 @@
per_page : number of items to fetch per page
remote : data-remote
span.last
- == link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote}
+ == link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, :remote => remote
'
@@ -3,5 +3,8 @@ source :rubygems
gem 'railties', '>= 3.2.3'
gem 'activerecord', '>= 3.2.3', :require => 'active_record'
gem 'rspec-rails', '>= 2.0'
+gem 'nokogiri'
+gem 'xpath'
+gem 'mime-types'
gemspec :path => '../'
@@ -8,5 +8,8 @@ gem 'dm-transactions', '>= 1.2.0'
gem 'dm-active_model', '>= 1.2.0'
gem 'dm-sqlite-adapter', '>= 1.2.0'
gem 'rspec-rails', '>= 2.0'
+gem 'nokogiri'
+gem 'xpath'
+gem 'mime-types'
gemspec :path => '../'
@@ -3,5 +3,8 @@ source :rubygems
gem 'railties', '>= 3.2.3'
gem 'mongo_mapper', '>= 0.11.0'
gem 'rspec-rails', '>= 2.0'
+gem 'nokogiri'
+gem 'xpath'
+gem 'mime-types'
gemspec :path => '../'
@@ -3,5 +3,10 @@ source :rubygems
gem 'railties', '>= 3.2.3'
gem 'mongoid', '>= 3.0'
gem 'rspec-rails', '>= 2.0'
+gem 'origin'
+gem 'moped'
+gem 'nokogiri'
+gem 'xpath'
+gem 'mime-types'
gemspec :path => '../'
View
@@ -5,6 +5,8 @@ gem 'sinatra', '>= 1.3'
gem 'padrino-helpers', '>= 0.10.6.c'
gem 'rack-test', '>= 0'
gem 'sinatra-contrib', '>= 1.3'
-gem 'nokogiri', '>= 0'
+gem 'nokogiri'
+gem 'xpath'
+gem 'mime-types'
gemspec :path => '../'
View
@@ -10,15 +10,16 @@ module Kaminari
$stderr.puts <<-EOC if !defined?(Rails) && !defined?(Sinatra) && !defined?(Grape)
warning: no framework detected.
-would you check out if your Gemfile appropriately configured?
+
+Your Gemfile might not be configured properly.
---- e.g. ----
-when Rails:
+Rails:
gem 'kaminari'
-when Sinatra/Padrino:
+Sinatra/Padrino:
gem 'kaminari', :require => 'kaminari/sinatra'
-when Grape:
+Grape:
gem 'kaminari', :require => 'kaminari/grape'
EOC
View
@@ -24,6 +24,7 @@ class Configuration #:nodoc:
config_accessor :left
config_accessor :right
config_accessor :page_method_name
+ config_accessor :max_pages
def param_name
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
@@ -45,5 +46,6 @@ def param_name
config.right = 0
config.page_method_name = :page
config.param_name = :page
+ config.max_pages = nil
end
end
@@ -10,6 +10,11 @@ class << self
def registered(app)
app.register Padrino::Helpers
app.helpers HelperMethods
+ @app = app
+ end
+
+ def view_paths
+ @app.views
end
alias included registered
@@ -31,6 +36,7 @@ def initialize(opts={})
def render(*args)
base = ActionView::Base.new.tap do |a|
+ a.view_paths << SinatraHelpers.view_paths
a.view_paths << File.expand_path('../../../../app/views', __FILE__)
end
base.render(*args)
@@ -125,7 +131,7 @@ def link_to_next_page(scope, name, options = {})
rescue LoadError
-$stderr.puts "[!]You shold install `padrino-helpers' gem if you want to use kaminari's pagination helpers with Sinatra."
+$stderr.puts "[!]You should install `padrino-helpers' gem if you want to use kaminari's pagination helpers with Sinatra."
$stderr.puts "[!]Kaminari::Helpers::SinatraHelper does nothing now..."
module Kaminari::Helpers
@@ -3,27 +3,25 @@ module ActiveRecordRelationMethods
# a workaround for AR 3.0.x that returns 0 for #count when page > 1
# if +limit_value+ is specified, load all the records and count them
if ActiveRecord::VERSION::STRING < '3.1'
- def count #:nodoc:
- limit_value ? length : super
+ def count(column_name = nil, options = {}) #:nodoc:
+ limit_value ? length : super(column_name, options)
end
end
- def total_count #:nodoc:
+ def total_count(column_name = nil, options = {}) #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
@total_count ||= begin
c = except(:offset, :limit, :order)
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
- # a workaround to count the actual model instances on distinct query because count + distinct returns wrong value in some cases. see https://github.com/amatsuda/kaminari/pull/160
- uses_distinct_sql_statement = c.to_sql =~ /DISTINCT/i
- if uses_distinct_sql_statement
- c.length
+ # .group returns an OrderdHash that responds to #count
+ c = c.count(column_name, options)
+ if c.is_a?(ActiveSupport::OrderedHash)
+ c.count
else
- # .group returns an OrderdHash that responds to #count
- c = c.count
- c.respond_to?(:count) ? c.count : c
+ c.respond_to?(:count) ? c.count(column_name, options) : c
end
end
end
@@ -14,9 +14,7 @@ def initialize(original_array = [], options = {})
@_original_array, @_limit_value, @_offset_value, @_total_count = original_array, (options[:limit] || default_per_page).to_i, options[:offset].to_i, options[:total_count]
if options[:limit] && options[:offset]
- class << self
- include Kaminari::PageScopeMethods
- end
+ extend Kaminari::PageScopeMethods
end
if options[:total_count]
@@ -13,7 +13,7 @@ def paginates_per(val)
# This model's default +per_page+ value
# returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
def default_per_page
- @_default_per_page ||= Kaminari.config.default_per_page
+ @_default_per_page || Kaminari.config.default_per_page
end
# Overrides the max +per_page+ value per model
@@ -27,7 +27,21 @@ def max_paginates_per(val)
# This model's max +per_page+ value
# returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt>
def max_per_page
- @_max_per_page ||= Kaminari.config.max_per_page
+ @_max_per_page || Kaminari.config.max_per_page
+ end
+
+ # Overrides the max_pages value per model
+ # class Article < ActiveRecord::Base
+ # max_pages_per 100
+ # end
+ def max_pages_per(val)
+ @_max_pages = val
+ end
+
+ # This model's max_pages value
+ # returns max_pages value unless explicitly overridden via <tt>max_pages_per</tt>
+ def max_pages
+ @_max_pages || Kaminari.config.max_pages
end
end
end
@@ -3,7 +3,9 @@ module PageScopeMethods
# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
# Model.page(3).per(10)
def per(num)
- if (n = num.to_i) <= 0
+ if num.nil?
+ limit(nil).offset(0)
+ elsif (n = num.to_i) <= 0
self
elsif max_per_page && max_per_page < n
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
@@ -18,13 +20,21 @@ def padding(num)
# Total number of pages
def total_pages
- (total_count.to_f / limit_value).ceil
+ return 1 if limit_value.nil?
+
+ total_pages_count = (total_count.to_f / limit_value).ceil
+ if max_pages.present? && max_pages < total_pages_count
+ max_pages
+ else
+ total_pages_count
+ end
end
#FIXME for compatibility. remove num_pages at some time in the future
alias num_pages total_pages
# Current page number
def current_page
+ return 1 if limit_value.nil?
(offset_value / limit_value) + 1
end
View
@@ -1,3 +1,3 @@
module Kaminari
- VERSION = '0.14.0'
+ VERSION = '0.14.1'
end
Oops, something went wrong.

0 comments on commit 585a4c3

Please sign in to comment.