Skip to content

Commit

Permalink
switch to kaminari gem for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
vhochstein committed May 26, 2012
1 parent 62a21e1 commit 69f5478
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 46 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -5,6 +5,8 @@ source "http://rubygems.org"


# Add dependencies to develop your gem here. # Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc. # Include everything needed to run rake, tests, features, etc.
gem "kaminari"

group :development do group :development do
gem "shoulda", ">= 0" gem "shoulda", ">= 0"
gem "bundler", ">= 1.0.0" gem "bundler", ">= 1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion frontends/default/stylesheets/stylesheet.css
Expand Up @@ -528,7 +528,7 @@ background-color: #005CB8;
color: #ccc; color: #ccc;
} }


.active-scaffold-footer .active-scaffold-pagination { .active-scaffold-footer .pagination {
float: right; float: right;
white-space: nowrap; white-space: nowrap;
margin-right: 5px; margin-right: 5px;
Expand Down
3 changes: 2 additions & 1 deletion frontends/default/views/_list.html.erb
Expand Up @@ -8,11 +8,12 @@
<%= render :partial => 'list_messages', :locals => {:columns => columns} %> <%= render :partial => 'list_messages', :locals => {:columns => columns} %>
<tbody class="records" id="<%= active_scaffold_tbody_id %>"> <tbody class="records" id="<%= active_scaffold_tbody_id %>">
<% if !@records.empty? -%> <% 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 -%> <% end -%>
<% if columns.any? {|c| c.calculation?} -%> <% if columns.any? {|c| c.calculation?} -%>
<%= render :partial => 'list_calculations', :locals => {:columns => columns} %> <%= render :partial => 'list_calculations', :locals => {:columns => columns} %>
<% end -%> <% end -%>
</tbody> </tbody>
</table> </table>
<%= render :partial => 'list_pagination' %> <%= render :partial => 'list_pagination' %>
<% save_current_page_num %>
4 changes: 2 additions & 2 deletions frontends/default/views/_list_messages.html.erb
Expand Up @@ -11,7 +11,7 @@
<p class="filtered-message" <%= ' style="display:none;" '.html_safe unless @filtered %>> <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) %> <%= @filtered.is_a?(Array) ? render(:partial => 'human_conditions', :locals => {:columns => @filtered}) : as_(active_scaffold_config.list.filtered_message) %>
</p> </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) %> <%= as_(active_scaffold_config.list.no_entries_message) %>
</p> </p>
</td> </td>
Expand All @@ -23,7 +23,7 @@
action_links.add(search_link) -%> action_links.add(search_link) -%>
<%= render :partial => 'list_actions', :locals => {:record => record, :url_options => params_for(:escape => false, :search => ''), :action_links => action_links.member} %> <%= render :partial => 'list_actions', :locals => {:record => record, :url_options => params_for(:escape => false, :search => ''), :action_links => action_links.member} %>
<% else %> <% 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 -%> <% end -%>


</tr> </tr>
Expand Down
9 changes: 3 additions & 6 deletions frontends/default/views/_list_pagination.html.erb
@@ -1,11 +1,8 @@
<% if active_scaffold_config.list.pagination -%> <% if active_scaffold_config.list.pagination -%>
<div class="active-scaffold-footer"> <div class="active-scaffold-footer">
<% unless @page.pager.infinite? -%> <% unless active_scaffold_config.list.pagination == :infinite -%>
<div class="active-scaffold-found"><span class="active-scaffold-records"><%= @page.pager.count -%></span> <%=as_(:found, :count => @page.pager.count) %></div> <div class="active-scaffold-found"><span class="active-scaffold-records"><%= @records.total_count -%></span> <%=as_(:found, :count => @records.total_count) %></div>
<% end -%> <% end -%>
<div class="active-scaffold-pagination"> <%= paginate @records, :window => active_scaffold_config.list.page_links_window, :params => params_for(:action => :index), :remote => true %>
<%= 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 %>
</div> </div>
<% end -%> <% end -%>
1 change: 0 additions & 1 deletion lib/active_scaffold.rb
Expand Up @@ -13,7 +13,6 @@


require 'active_scaffold_assets' require 'active_scaffold_assets'
require 'active_scaffold/active_record_permissions' require 'active_scaffold/active_record_permissions'
require 'active_scaffold/paginator'
require 'active_scaffold/responds_to_parent' require 'active_scaffold/responds_to_parent'


require 'active_scaffold/version' require 'active_scaffold/version'
Expand Down
15 changes: 9 additions & 6 deletions lib/active_scaffold/actions/list.rb
Expand Up @@ -3,6 +3,7 @@ module List
def self.included(base) def self.included(base)
base.before_filter :list_authorized_filter, :only => [:index, :row] base.before_filter :list_authorized_filter, :only => [:index, :row]
base.helper_method :list_columns base.helper_method :list_columns
base.helper_method :save_current_page_num
end end


def index def index
Expand Down Expand Up @@ -73,12 +74,8 @@ def do_list
}) })
end end


page = find_page(options); @records = find_page(options);
if page.items.blank? && !page.pager.infinite? @records
page = page.pager.last
active_scaffold_config.list.user.page = page.number
end
@page, @records = page, page.items
end end


def each_record_in_page def each_record_in_page
Expand Down Expand Up @@ -174,6 +171,12 @@ def action_confirmation_formats
(default_formats + active_scaffold_config.formats).uniq (default_formats + active_scaffold_config.formats).uniq
end 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 private
def list_authorized_filter def list_authorized_filter
raise ActiveScaffold::ActionNotAllowed unless list_authorized? raise ActiveScaffold::ActionNotAllowed unless list_authorized?
Expand Down
34 changes: 8 additions & 26 deletions lib/active_scaffold/finder.rb
Expand Up @@ -243,7 +243,7 @@ def find_if_allowed(id, crud_type, klass = beginning_of_chain)
return record return record
end end


# returns a Paginator::Page (not from ActiveRecord::Paginator) for the given parameters # returns a records relation for the current page
# options may include: # 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. # * :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 # * :per_page
Expand All @@ -256,6 +256,7 @@ def find_page(options = {})
full_includes = (active_scaffold_includes.blank? ? nil : active_scaffold_includes) full_includes = (active_scaffold_includes.blank? ? nil : active_scaffold_includes)
options[:per_page] ||= 999999999 options[:per_page] ||= 999999999
options[:page] ||= 1 options[:page] ||= 1
#TODO not supported by kaminari
options[:count_includes] ||= full_includes unless search_conditions.nil? options[:count_includes] ||= full_includes unless search_conditions.nil?


klass = beginning_of_chain klass = beginning_of_chain
Expand All @@ -264,37 +265,19 @@ def find_page(options = {})
finder_options = { :order => options[:sorting].try(:clause), finder_options = { :order => options[:sorting].try(:clause),
:where => search_conditions, :where => search_conditions,
:joins => joins_for_finder, :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 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 # we build the paginator differently for method- and sql-based sorting
if options[:sorting] and options[:sorting].sorts_by_method? records = if options[:sorting] && options[:sorting].sorts_by_method?
pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page| Kaminari.paginate_array(sort_collection_by_column(append_to_query(klass, finder_options).all, *options[:sorting].first))
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
else else
pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page| append_to_query(klass, finder_options)
finder_options.merge!(:offset => offset, :limit => per_page) if options[:pagination]
append_to_query(klass, finder_options).all
end
end end
pager.page(options[:page]) records = records.page(options[:page]).per(options[:per_page]) if options[:pagination]
records
end end


# if someone excludes association from includes in configuration # if someone excludes association from includes in configuration
Expand Down Expand Up @@ -349,7 +332,6 @@ def merge_conditions(*conditions)


# TODO: this should reside on the column, not the controller # TODO: this should reside on the column, not the controller
def sort_collection_by_column(collection, column, order) def sort_collection_by_column(collection, column, order)
Rails.logger.info("das hier ja was bin im sort_collection")
sorter = column.sort[:method] sorter = column.sort[:method]
collection = collection.sort_by { |record| collection = collection.sort_by { |record|
value = (sorter.is_a? Proc) ? record.instance_eval(&sorter) : record.instance_eval(sorter) value = (sorter.is_a? Proc) ? record.instance_eval(&sorter) : record.instance_eval(sorter)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_scaffold/helpers/list_column_helpers.rb
Expand Up @@ -314,11 +314,11 @@ def inplace_edit_tag_attributes(column)
def mark_column_heading def mark_column_heading
if active_scaffold_config.mark.mark_all_mode == :page then if active_scaffold_config.mark.mark_all_mode == :page then
all_marked = true all_marked = true
@page.items.each do |record| @records.each do |record|
all_marked = false if !marked_records.entries.include?(record.id) all_marked = false if !marked_records.entries.include?(record.id)
end end
else else
all_marked = (marked_records.length >= @page.pager.count) all_marked = (marked_records.length >= @records.total_count)
end end
tag_options = {:id => "#{controller_id}_mark_heading", :class => "mark_heading in_place_editor_field"} 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]}) tag_options['data-ie_url'] = url_for({:controller => params_for[:controller], :action => 'mark_all', :eid => params[:eid]})
Expand Down
Expand Up @@ -377,7 +377,7 @@ background-color: #005CB8;
color: #ccc; color: #ccc;
} }


.active-scaffold-footer .active-scaffold-pagination { .active-scaffold-footer .pagination {
float: right; float: right;
white-space: nowrap; white-space: nowrap;
margin-right: 5px; margin-right: 5px;
Expand Down

0 comments on commit 69f5478

Please sign in to comment.