lib/magic_grid/definition.rb in magic_grid-0.9.3.1 vs lib/magic_grid/definition.rb in magic_grid-0.10.0
- old
+ new
@@ -1,12 +1,12 @@
-require 'will_paginate/view_helpers/action_view'
+#require 'will_paginate/view_helpers/action_view'
module MagicGrid
class Definition
- include WillPaginate::ActionView
+ #include WillPaginate::ActionView
attr_accessor :columns, :collection, :magic_id, :options, :params,
- :current_sort_col, :current_order, :default_order
+ :current_sort_col, :current_order, :default_order, :per_page
DEFAULTS = {
:class => [],
:top_pager => false,
:bottom_pager => true,
@@ -52,11 +52,12 @@
@columns = cols_or_opts
else
raise "I have no idea what that is, but it's not a Hash or an Array"
end
@default_order = @options[:default_order]
- @params = controller.try(:params) || {}
+ @params = controller && controller.params || {}
+ @per_page = @options[:per_page]
@collection = collection
begin
#if @collection.respond_to? :table
table_name = @collection.quoted_table_name
table_columns = @collection.table.columns.map {|c| c.name}
@@ -170,13 +171,28 @@
end
if @options[:post_filter] and @options[:post_filter].respond_to?(:call)
@collection = @options[:post_filter].call(@collection)
end
# Paginate at the very end, after all sorting, filtering, etc..
- if @options[:per_page]
- @collection = @collection.paginate(:page => param(:page, 1),
- :per_page => @options[:per_page])
+ if @per_page
+ if @collection.respond_to? :paginate
+ @collection = @collection.paginate(:page => current_page,
+ :per_page => @per_page)
+ elsif @collection.respond_to? :page
+ @collection = @collection.page(current_page).per(@per_page)
+ elsif @collection.is_a?(Array) and Module.const_defined?(:Kaminari)
+ @collection = Kaminari.paginate_array(@collection).page(current_page).per(@per_page)
+ else
+ original = @collection
+ @collection = @collection.to_enum.each_slice(@per_page).drop(current_page - 1).first.to_a
+ class << @collection
+ attr_accessor :current_page, :total_pages, :original_count
+ end
+ @collection.current_page = current_page
+ @collection.original_count = original.count
+ @collection.total_pages = original.count / @per_page
+ end
end
end
def param_key(key)
"#{@magic_id}_#{key}".to_sym
@@ -186,9 +202,13 @@
@params.fetch(param_key(key), default)
end
def base_params
@params.merge :magic_grid_id => @magic_id
+ end
+
+ def current_page
+ [param(:page, 1).to_i, 1].max
end
def order(something)
case something
when 1, "1", :desc, :DESC, "desc", "DESC"