lib/magic_grid/collection.rb in magic_grid-0.12.3 vs lib/magic_grid/collection.rb in magic_grid-0.12.4
- old
+ new
@@ -3,19 +3,19 @@
module MagicGrid
class Collection
DEFAULTS = {
- per_page: 30,
- searchable: [],
- search_method: :search,
- listener_handler: nil,
- default_col: 0,
- default_order: :asc,
- post_filter: false,
- collection_post_filter: true,
- count: nil,
+ :per_page => 30,
+ :searchable => [],
+ :search_method => :search,
+ :listener_handler => nil,
+ :default_col => 0,
+ :default_order => :asc,
+ :post_filter => false,
+ :collection_post_filter => true,
+ :count => nil,
}
def initialize(collection, opts = {})
@collection = collection || []
self.options = opts
@@ -26,16 +26,18 @@
@searches = []
@post_filters = []
@post_filter_callbacks = []
@paginations = []
@searchable_columns = []
+ @@kaminari_class = defined?(Kaminari) ? Kaminari : nil
end
- delegate :quoted_table_name, :map, :count, to: :collection
+ delegate :quoted_table_name, :map, :count, :to => :collection
attr_accessor :searchable_columns
attr_reader :current_page, :original_count, :total_pages, :per_page, :searches
+ cattr_accessor :kaminari_class
def options=(opts)
@options = DEFAULTS.merge(opts || {})
end
@@ -83,11 +85,11 @@
result = collection
unless searchable_columns.empty?
begin
search_cols = searchable_columns.map {|c| c.custom_sql || c.name }
clauses = search_cols.map {|c| c << " LIKE :search" }.join(" OR ")
- result = collection.where(clauses, {search: "%#{q}%"})
+ result = collection.where(clauses, {:search => "%#{q}%"})
rescue
MagicGrid.logger.debug "Given collection doesn't respond to :where well"
end
end
result
@@ -179,11 +181,11 @@
def per_page=(n)
@original_count = self.count @collection
@per_page = n
if @per_page
- @total_pages = @original_count / @per_page
+ @total_pages = calculate_total_pages(@per_page, @original_count)
else
@total_pages = 1
end
end
@@ -211,17 +213,17 @@
@current_page = bound_current_page(@current_page,
@per_page,
total_entries)
if collection.respond_to? :paginate
- collection.paginate(page: @current_page,
- per_page: @per_page,
- total_entries: total_entries)
+ collection.paginate(:page => @current_page,
+ :per_page => @per_page,
+ :total_entries => total_entries)
elsif collection.respond_to? :page
collection.page(@current_page).per(@per_page)
- elsif collection.is_a?(Array) and Module.const_defined?(:Kaminari)
- Kaminari.paginate_array(collection).page(@current_page).per(@per_page)
+ elsif collection.is_a?(Array) and @@kaminari_class
+ @@kaminari_class.paginate_array(collection).page(@current_page).per(@per_page)
else
default_paginate(collection, @current_page, @per_page)
end
end
@@ -254,18 +256,28 @@
def collection
@reduced_collection ||= apply_all_operations(@collection)
end
- private
+ def calculate_total_pages(per_page, total_entries)
+ pages = total_entries / per_page
+ pages += 1 if total_entries % per_page > 0
+ if pages < 1
+ 1
+ else
+ pages
+ end
+ end
def bound_current_page(page, per_page, total_entries)
- pages = total_entries / per_page
- pages = 1 if pages == 0
- if page > pages
+ pages = calculate_total_pages(per_page, total_entries)
+ if page < 1
+ 1
+ elsif page > pages
pages
else
page
end
end
+
end
end