Permalink
Browse files

switch to kaminari gem for sorting

  • Loading branch information...
1 parent 62a21e1 commit 69f54783aef58d307308c5cfda1ef26d231d93cd @vhochstein committed May 26, 2012
View
@@ -5,6 +5,8 @@ source "http://rubygems.org"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
+gem "kaminari"
+
group :development do
gem "shoulda", ">= 0"
gem "bundler", ">= 1.0.0"
@@ -528,7 +528,7 @@ background-color: #005CB8;
color: #ccc;
}
-.active-scaffold-footer .active-scaffold-pagination {
+.active-scaffold-footer .pagination {
float: right;
white-space: nowrap;
margin-right: 5px;
@@ -8,11 +8,12 @@
<%= render :partial => 'list_messages', :locals => {:columns => columns} %>
<tbody class="records" id="<%= active_scaffold_tbody_id %>">
<% if !@records.empty? -%>
- <%= render :partial => 'list_record', :collection => @page.items, :locals => { :hidden => false, :columns => columns, :action_links => active_scaffold_config.action_links.member} %>
+ <%= render :partial => 'list_record', :collection => @records, :locals => { :hidden => false, :columns => columns, :action_links => active_scaffold_config.action_links.member} %>
<% end -%>
<% if columns.any? {|c| c.calculation?} -%>
<%= render :partial => 'list_calculations', :locals => {:columns => columns} %>
<% end -%>
</tbody>
</table>
<%= render :partial => 'list_pagination' %>
+<% save_current_page_num %>
@@ -11,7 +11,7 @@
<p class="filtered-message" <%= ' style="display:none;" '.html_safe unless @filtered %>>
<%= @filtered.is_a?(Array) ? render(:partial => 'human_conditions', :locals => {:columns => @filtered}) : as_(active_scaffold_config.list.filtered_message) %>
</p>
- <p id="<%= empty_message_id %>" class="empty-message" <%= ' style="display:none;" '.html_safe unless @page.items.empty? %>>
+ <p id="<%= empty_message_id %>" class="empty-message" <%= ' style="display:none;" '.html_safe unless @records.empty? %>>
<%= as_(active_scaffold_config.list.no_entries_message) %>
</p>
</td>
@@ -23,7 +23,7 @@
action_links.add(search_link) -%>
<%= render :partial => 'list_actions', :locals => {:record => record, :url_options => params_for(:escape => false, :search => ''), :action_links => action_links.member} %>
<% else %>
- <td class='actions'><%= '<p class="empty-message">&nbsp;</p>'.html_safe if @page.items.empty? %></td>
+ <td class='actions'><%= '<p class="empty-message">&nbsp;</p>'.html_safe if @records.empty? %></td>
<% end -%>
</tr>
@@ -1,11 +1,8 @@
<% if active_scaffold_config.list.pagination -%>
<div class="active-scaffold-footer">
-<% unless @page.pager.infinite? -%>
- <div class="active-scaffold-found"><span class="active-scaffold-records"><%= @page.pager.count -%></span> <%=as_(:found, :count => @page.pager.count) %></div>
+<% unless active_scaffold_config.list.pagination == :infinite -%>
+ <div class="active-scaffold-found"><span class="active-scaffold-records"><%= @records.total_count -%></span> <%=as_(:found, :count => @records.total_count) %></div>
<% end -%>
- <div class="active-scaffold-pagination">
- <%= render :partial => 'list_pagination_links', :locals => { :current_page => @page } if @page.pager.infinite? || @page.pager.number_of_pages > 1 %>
- </div>
- <br clear="both" /><%# a hack for the Rico Corner problem %>
+ <%= paginate @records, :window => active_scaffold_config.list.page_links_window, :params => params_for(:action => :index), :remote => true %>
</div>
<% end -%>
View
@@ -13,7 +13,6 @@
require 'active_scaffold_assets'
require 'active_scaffold/active_record_permissions'
-require 'active_scaffold/paginator'
require 'active_scaffold/responds_to_parent'
require 'active_scaffold/version'
@@ -3,6 +3,7 @@ module List
def self.included(base)
base.before_filter :list_authorized_filter, :only => [:index, :row]
base.helper_method :list_columns
+ base.helper_method :save_current_page_num
end
def index
@@ -73,12 +74,8 @@ def do_list
})
end
- page = find_page(options);
- if page.items.blank? && !page.pager.infinite?
- page = page.pager.last
- active_scaffold_config.list.user.page = page.number
- end
- @page, @records = page, page.items
+ @records = find_page(options);
+ @records
end
def each_record_in_page
@@ -174,6 +171,12 @@ def action_confirmation_formats
(default_formats + active_scaffold_config.formats).uniq
end
+ def save_current_page_num
+ if !(active_scaffold_config.list.pagination == :infinite)
+ active_scaffold_config.list.user.page = @records.current_page
+ end
+ end
+
private
def list_authorized_filter
raise ActiveScaffold::ActionNotAllowed unless list_authorized?
@@ -243,7 +243,7 @@ def find_if_allowed(id, crud_type, klass = beginning_of_chain)
return record
end
- # returns a Paginator::Page (not from ActiveRecord::Paginator) for the given parameters
+ # returns a records relation for the current page
# options may include:
# * :sorting - a Sorting DataStructure (basically an array of hashes of field => direction, e.g. [{:field1 => 'asc'}, {:field2 => 'desc'}]). please note that multi-column sorting has some limitations: if any column in a multi-field sort uses method-based sorting, it will be ignored. method sorting only works for single-column sorting.
# * :per_page
@@ -256,6 +256,7 @@ def find_page(options = {})
full_includes = (active_scaffold_includes.blank? ? nil : active_scaffold_includes)
options[:per_page] ||= 999999999
options[:page] ||= 1
+ #TODO not supported by kaminari
options[:count_includes] ||= full_includes unless search_conditions.nil?
klass = beginning_of_chain
@@ -264,37 +265,19 @@ def find_page(options = {})
finder_options = { :order => options[:sorting].try(:clause),
:where => search_conditions,
:joins => joins_for_finder,
- :includes => options[:count_includes]}
+ :includes => add_association_to_includes_for_sorting(options[:sorting], full_includes)}
finder_options.merge! custom_finder_options
- # NOTE: we must use :include in the count query, because some conditions may reference other tables
- count_query = append_to_query(klass, finder_options.reject{|k, v| [:select, :order].include?(k)})
- count = count_query.count unless options[:pagination] == :infinite
-
- # Converts count to an integer if ActiveRecord returned an OrderedHash
- # that happens when finder_options contains a :group key
- count = count.length if count.is_a? ActiveSupport::OrderedHash
-
- full_includes = add_association_to_includes_for_sorting(options[:sorting], full_includes)
-
- finder_options.merge! :includes => full_includes
-
# we build the paginator differently for method- and sql-based sorting
- if options[:sorting] and options[:sorting].sorts_by_method?
- pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page|
- sorted_collection = sort_collection_by_column(append_to_query(klass, finder_options).all, *options[:sorting].first)
- sorted_collection = sorted_collection.slice(offset, per_page) if options[:pagination]
- sorted_collection
- end
+ records = if options[:sorting] && options[:sorting].sorts_by_method?
+ Kaminari.paginate_array(sort_collection_by_column(append_to_query(klass, finder_options).all, *options[:sorting].first))
else
- pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page|
- finder_options.merge!(:offset => offset, :limit => per_page) if options[:pagination]
- append_to_query(klass, finder_options).all
- end
+ append_to_query(klass, finder_options)
end
- pager.page(options[:page])
+ records = records.page(options[:page]).per(options[:per_page]) if options[:pagination]
+ records
end
# if someone excludes association from includes in configuration
@@ -349,7 +332,6 @@ def merge_conditions(*conditions)
# TODO: this should reside on the column, not the controller
def sort_collection_by_column(collection, column, order)
- Rails.logger.info("das hier ja was bin im sort_collection")
sorter = column.sort[:method]
collection = collection.sort_by { |record|
value = (sorter.is_a? Proc) ? record.instance_eval(&sorter) : record.instance_eval(sorter)
@@ -314,11 +314,11 @@ def inplace_edit_tag_attributes(column)
def mark_column_heading
if active_scaffold_config.mark.mark_all_mode == :page then
all_marked = true
- @page.items.each do |record|
+ @records.each do |record|
all_marked = false if !marked_records.entries.include?(record.id)
end
else
- all_marked = (marked_records.length >= @page.pager.count)
+ all_marked = (marked_records.length >= @records.total_count)
end
tag_options = {:id => "#{controller_id}_mark_heading", :class => "mark_heading in_place_editor_field"}
tag_options['data-ie_url'] = url_for({:controller => params_for[:controller], :action => 'mark_all', :eid => params[:eid]})
@@ -377,7 +377,7 @@ background-color: #005CB8;
color: #ccc;
}
-.active-scaffold-footer .active-scaffold-pagination {
+.active-scaffold-footer .pagination {
float: right;
white-space: nowrap;
margin-right: 5px;

0 comments on commit 69f5478

Please sign in to comment.