2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2006-06-28 22:11:03 +04:00
|
|
|
# Helpers to sort tables using clickable column headers.
|
|
|
|
#
|
|
|
|
# Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
|
2009-03-11 00:11:36 +03:00
|
|
|
# Jean-Philippe Lang, 2009
|
2006-06-28 22:11:03 +04:00
|
|
|
# License: This source code is released under the MIT license.
|
|
|
|
#
|
|
|
|
# - Consecutive clicks toggle the column's sort order.
|
|
|
|
# - Sort state is maintained by a session hash entry.
|
2009-03-11 00:11:36 +03:00
|
|
|
# - CSS classes identify sort column and state.
|
2006-06-28 22:11:03 +04:00
|
|
|
# - Typically used in conjunction with the Pagination module.
|
|
|
|
#
|
|
|
|
# Example code snippets:
|
|
|
|
#
|
|
|
|
# Controller:
|
|
|
|
#
|
|
|
|
# helper :sort
|
|
|
|
# include SortHelper
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2006-06-28 22:11:03 +04:00
|
|
|
# def list
|
|
|
|
# sort_init 'last_name'
|
2009-03-12 21:24:32 +03:00
|
|
|
# sort_update %w(first_name last_name)
|
2006-06-28 22:11:03 +04:00
|
|
|
# @items = Contact.find_all nil, sort_clause
|
|
|
|
# end
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2006-06-28 22:11:03 +04:00
|
|
|
# Controller (using Pagination module):
|
|
|
|
#
|
|
|
|
# helper :sort
|
|
|
|
# include SortHelper
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2006-06-28 22:11:03 +04:00
|
|
|
# def list
|
|
|
|
# sort_init 'last_name'
|
2009-03-12 21:24:32 +03:00
|
|
|
# sort_update %w(first_name last_name)
|
2006-06-28 22:11:03 +04:00
|
|
|
# @contact_pages, @items = paginate :contacts,
|
|
|
|
# :order_by => sort_clause,
|
|
|
|
# :per_page => 10
|
|
|
|
# end
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2006-06-28 22:11:03 +04:00
|
|
|
# View (table header in list.rhtml):
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2006-06-28 22:11:03 +04:00
|
|
|
# <thead>
|
|
|
|
# <tr>
|
|
|
|
# <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
|
|
|
|
# <%= sort_header_tag('last_name', :caption => 'Name') %>
|
|
|
|
# <%= sort_header_tag('phone') %>
|
|
|
|
# <%= sort_header_tag('address', :width => 200) %>
|
|
|
|
# </tr>
|
|
|
|
# </thead>
|
|
|
|
#
|
2009-03-11 00:11:36 +03:00
|
|
|
# - Introduces instance variables: @sort_default, @sort_criteria
|
|
|
|
# - Introduces param :sort
|
2006-06-28 22:11:03 +04:00
|
|
|
#
|
2009-03-11 00:11:36 +03:00
|
|
|
|
2006-06-28 22:11:03 +04:00
|
|
|
module SortHelper
|
2009-03-11 00:11:36 +03:00
|
|
|
class SortCriteria
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def initialize
|
|
|
|
@criteria = []
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def available_criteria=(criteria)
|
|
|
|
unless criteria.is_a?(Hash)
|
|
|
|
criteria = criteria.inject({}) {|h,k| h[k] = k; h}
|
|
|
|
end
|
|
|
|
@available_criteria = criteria
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def from_param(param)
|
|
|
|
@criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
|
|
|
|
normalize!
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-12 21:06:54 +03:00
|
|
|
def criteria=(arg)
|
|
|
|
@criteria = arg
|
|
|
|
normalize!
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def to_param
|
|
|
|
@criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def to_sql
|
|
|
|
sql = @criteria.collect do |k,o|
|
|
|
|
if s = @available_criteria[k]
|
2010-03-27 19:55:20 +03:00
|
|
|
(o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ')
|
2009-03-11 00:11:36 +03:00
|
|
|
end
|
|
|
|
end.compact.join(', ')
|
|
|
|
sql.blank? ? nil : sql
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def add!(key, asc)
|
|
|
|
@criteria.delete_if {|k,o| k == key}
|
|
|
|
@criteria = [[key, asc]] + @criteria
|
|
|
|
normalize!
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def add(*args)
|
|
|
|
r = self.class.new.from_param(to_param)
|
|
|
|
r.add!(*args)
|
|
|
|
r
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def first_key
|
|
|
|
@criteria.first && @criteria.first.first
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def first_asc?
|
|
|
|
@criteria.first && @criteria.first.last
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-12 21:06:54 +03:00
|
|
|
def empty?
|
|
|
|
@criteria.empty?
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
private
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
def normalize!
|
2009-03-12 21:06:54 +03:00
|
|
|
@criteria ||= []
|
|
|
|
@criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]}
|
2009-03-11 00:11:36 +03:00
|
|
|
@criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria
|
|
|
|
@criteria.slice!(3)
|
|
|
|
self
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-03-27 19:55:20 +03:00
|
|
|
# Appends DESC to the sort criterion unless it has a fixed order
|
|
|
|
def append_desc(criterion)
|
|
|
|
if criterion =~ / (asc|desc)$/i
|
|
|
|
criterion
|
|
|
|
else
|
|
|
|
"#{criterion} DESC"
|
|
|
|
end
|
|
|
|
end
|
2009-03-11 00:11:36 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-12 21:06:54 +03:00
|
|
|
def sort_name
|
|
|
|
controller_name + '_' + action_name + '_sort'
|
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
|
2009-03-12 21:06:54 +03:00
|
|
|
# Initializes the default sort.
|
|
|
|
# Examples:
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2009-03-12 21:06:54 +03:00
|
|
|
# sort_init 'name'
|
|
|
|
# sort_init 'id', 'desc'
|
|
|
|
# sort_init ['name', ['id', 'desc']]
|
|
|
|
# sort_init [['name', 'desc'], ['id', 'desc']]
|
2006-06-28 22:11:03 +04:00
|
|
|
#
|
2009-03-12 21:06:54 +03:00
|
|
|
def sort_init(*args)
|
|
|
|
case args.size
|
|
|
|
when 1
|
|
|
|
@sort_default = args.first.is_a?(Array) ? args.first : [[args.first]]
|
|
|
|
when 2
|
|
|
|
@sort_default = [[args.first, args.last]]
|
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Updates the sort state. Call this in the controller prior to calling
|
|
|
|
# sort_clause.
|
2009-03-11 00:11:36 +03:00
|
|
|
# - criteria can be either an array or a hash of allowed keys
|
|
|
|
#
|
|
|
|
def sort_update(criteria)
|
|
|
|
@sort_criteria = SortCriteria.new
|
|
|
|
@sort_criteria.available_criteria = criteria
|
2009-03-12 21:06:54 +03:00
|
|
|
@sort_criteria.from_param(params[:sort] || session[sort_name])
|
|
|
|
@sort_criteria.criteria = @sort_default if @sort_criteria.empty?
|
2009-03-11 00:11:36 +03:00
|
|
|
session[sort_name] = @sort_criteria.to_param
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-12 21:06:54 +03:00
|
|
|
# Clears the sort criteria session data
|
|
|
|
#
|
|
|
|
def sort_clear
|
|
|
|
session[sort_name] = nil
|
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
|
|
|
|
# Returns an SQL sort clause corresponding to the current sort state.
|
|
|
|
# Use this to sort the controller's table items collection.
|
|
|
|
#
|
|
|
|
def sort_clause()
|
2009-03-11 00:11:36 +03:00
|
|
|
@sort_criteria.to_sql
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a link which sorts by the named column.
|
|
|
|
#
|
|
|
|
# - column is the name of an attribute in the sorted record collection.
|
2009-03-11 00:11:36 +03:00
|
|
|
# - the optional caption explicitly specifies the displayed link text.
|
|
|
|
# - 2 CSS classes reflect the state of the link: sort and asc or desc
|
2006-06-28 22:11:03 +04:00
|
|
|
#
|
2008-02-05 20:28:02 +03:00
|
|
|
def sort_link(column, caption, default_order)
|
2009-03-11 00:11:36 +03:00
|
|
|
css, order = nil, default_order
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
if column.to_s == @sort_criteria.first_key
|
|
|
|
if @sort_criteria.first_asc?
|
|
|
|
css = 'sort asc'
|
2006-06-28 22:11:03 +04:00
|
|
|
order = 'desc'
|
|
|
|
else
|
2009-03-11 00:11:36 +03:00
|
|
|
css = 'sort desc'
|
2006-06-28 22:11:03 +04:00
|
|
|
order = 'asc'
|
|
|
|
end
|
|
|
|
end
|
2009-03-11 00:11:36 +03:00
|
|
|
caption = column.to_s.humanize unless caption
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
|
2011-04-03 18:09:22 +04:00
|
|
|
url_options = params.merge(sort_options)
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-03-11 00:11:36 +03:00
|
|
|
# Add project_id to url_options
|
2009-01-27 23:42:19 +03:00
|
|
|
url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
|
2009-03-11 00:11:36 +03:00
|
|
|
|
2011-07-30 23:51:06 +04:00
|
|
|
link_to_content_update(h(caption), url_options, :class => css)
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a table header <th> tag with a sort link for the named column
|
|
|
|
# attribute.
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
# :caption The displayed link name (defaults to titleized column name).
|
|
|
|
# :title The tag's 'title' attribute (defaults to 'Sort by :caption').
|
|
|
|
#
|
|
|
|
# Other options hash entries generate additional table header tag attributes.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
|
|
|
|
#
|
|
|
|
def sort_header_tag(column, options = {})
|
2009-03-11 00:11:36 +03:00
|
|
|
caption = options.delete(:caption) || column.to_s.humanize
|
2008-02-05 20:28:02 +03:00
|
|
|
default_order = options.delete(:default_order) || 'asc'
|
2009-03-11 00:11:36 +03:00
|
|
|
options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]
|
2008-02-05 20:28:02 +03:00
|
|
|
content_tag('th', sort_link(column, caption, default_order), options)
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
|
|
|
end
|
2009-03-11 00:11:36 +03:00
|
|
|
|