##// END OF EJS Templates
set default category_id instead of the object (#11665)...
set default category_id instead of the object (#11665) Rails 2.3 still has issues with synchronizing the association_id and association attributes of an object. That means, if you set the association with an object first and then just set the id afterwards, the object wins and the setting of the id gets lost. This is not an issue in Rails >= 3.1 anymore. Contributed by Holger Just. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/1.4-stable@10226 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r8368:b1504ceb434c
r10043:14dcefaa97f9
Show More
sort_helper.rb
234 lines | 6.1 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
Added encoding comment to helpers (#9792)....
r8090 # encoding: utf-8
#
Jean-Philippe Lang
Initial commit...
r2 # Helpers to sort tables using clickable column headers.
#
# Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # Jean-Philippe Lang, 2009
Jean-Philippe Lang
Initial commit...
r2 # 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.
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # - CSS classes identify sort column and state.
Jean-Philippe Lang
Initial commit...
r2 # - Typically used in conjunction with the Pagination module.
#
# Example code snippets:
#
# Controller:
#
# helper :sort
# include SortHelper
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Initial commit...
r2 # def list
# sort_init 'last_name'
Jean-Philippe Lang
Fixes SortHelper examples (#2950)....
r2508 # sort_update %w(first_name last_name)
Jean-Philippe Lang
Initial commit...
r2 # @items = Contact.find_all nil, sort_clause
# end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Initial commit...
r2 # Controller (using Pagination module):
#
# helper :sort
# include SortHelper
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Initial commit...
r2 # def list
# sort_init 'last_name'
Jean-Philippe Lang
Fixes SortHelper examples (#2950)....
r2508 # sort_update %w(first_name last_name)
Jean-Philippe Lang
Initial commit...
r2 # @contact_pages, @items = paginate :contacts,
# :order_by => sort_clause,
# :per_page => 10
# end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Initial commit...
r2 # View (table header in list.rhtml):
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Initial commit...
r2 # <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>
#
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # - Introduces instance variables: @sort_default, @sort_criteria
# - Introduces param :sort
Jean-Philippe Lang
Initial commit...
r2 #
Jean-Philippe Lang
SortHelper refactoring:...
r2503
Jean-Philippe Lang
Initial commit...
r2 module SortHelper
Jean-Philippe Lang
SortHelper refactoring:...
r2503 class SortCriteria
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def initialize
@criteria = []
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def available_criteria=(criteria)
unless criteria.is_a?(Hash)
criteria = criteria.inject({}) {|h,k| h[k] = k; h}
end
@available_criteria = criteria
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def from_param(param)
@criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
normalize!
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 def criteria=(arg)
@criteria = arg
normalize!
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def to_param
@criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def to_sql
sql = @criteria.collect do |k,o|
if s = @available_criteria[k]
Jean-Philippe Lang
Issue list improvements for subtasking (#5196):...
r3504 (o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ')
Jean-Philippe Lang
SortHelper refactoring:...
r2503 end
end.compact.join(', ')
sql.blank? ? nil : sql
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def add!(key, asc)
@criteria.delete_if {|k,o| k == key}
@criteria = [[key, asc]] + @criteria
normalize!
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def add(*args)
r = self.class.new.from_param(to_param)
r.add!(*args)
r
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def first_key
@criteria.first && @criteria.first.first
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def first_asc?
@criteria.first && @criteria.first.last
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 def empty?
@criteria.empty?
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 private
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 def normalize!
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 @criteria ||= []
@criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]}
Jean-Philippe Lang
SortHelper refactoring:...
r2503 @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria
@criteria.slice!(3)
self
end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
Issue list improvements for subtasking (#5196):...
r3504 # 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
Jean-Philippe Lang
SortHelper refactoring:...
r2503 end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 def sort_name
controller_name + '_' + action_name + '_sort'
end
Jean-Philippe Lang
Initial commit...
r2
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 # Initializes the default sort.
# Examples:
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225 #
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 # sort_init 'name'
# sort_init 'id', 'desc'
# sort_init ['name', ['id', 'desc']]
# sort_init [['name', 'desc'], ['id', 'desc']]
Jean-Philippe Lang
Initial commit...
r2 #
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 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
Jean-Philippe Lang
Initial commit...
r2 end
# Updates the sort state. Call this in the controller prior to calling
# sort_clause.
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # - criteria can be either an array or a hash of allowed keys
#
Jean-Philippe Lang
Adds previous/next links to issue (#2850)....
r8368 def sort_update(criteria, sort_name=nil)
sort_name ||= self.sort_name
Jean-Philippe Lang
SortHelper refactoring:...
r2503 @sort_criteria = SortCriteria.new
@sort_criteria.available_criteria = criteria
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 @sort_criteria.from_param(params[:sort] || session[sort_name])
@sort_criteria.criteria = @sort_default if @sort_criteria.empty?
Jean-Philippe Lang
SortHelper refactoring:...
r2503 session[sort_name] = @sort_criteria.to_param
Jean-Philippe Lang
Initial commit...
r2 end
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
Ability to save "sort order" in custom queries (#2899)....
r2504 # Clears the sort criteria session data
#
def sort_clear
session[sort_name] = nil
end
Jean-Philippe Lang
Initial commit...
r2
# Returns an SQL sort clause corresponding to the current sort state.
# Use this to sort the controller's table items collection.
#
def sort_clause()
Jean-Philippe Lang
SortHelper refactoring:...
r2503 @sort_criteria.to_sql
Jean-Philippe Lang
Initial commit...
r2 end
# Returns a link which sorts by the named column.
#
# - column is the name of an attribute in the sorted record collection.
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # - the optional caption explicitly specifies the displayed link text.
# - 2 CSS classes reflect the state of the link: sort and asc or desc
Jean-Philippe Lang
Initial commit...
r2 #
Jean-Philippe Lang
More appropriate default sort order on sortable columns....
r1107 def sort_link(column, caption, default_order)
Jean-Philippe Lang
SortHelper refactoring:...
r2503 css, order = nil, default_order
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 if column.to_s == @sort_criteria.first_key
if @sort_criteria.first_asc?
css = 'sort asc'
Jean-Philippe Lang
Initial commit...
r2 order = 'desc'
else
Jean-Philippe Lang
SortHelper refactoring:...
r2503 css = 'sort desc'
Jean-Philippe Lang
Initial commit...
r2 order = 'asc'
end
end
Jean-Philippe Lang
SortHelper refactoring:...
r2503 caption = column.to_s.humanize unless caption
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
Jean-Philippe Lang
Keep issue filter params on sort headers....
r5183 url_options = params.merge(sort_options)
Toshi MARUYAMA
remove trailing white-spaces from app/helpers/sort_helper.rb....
r6225
Jean-Philippe Lang
SortHelper refactoring:...
r2503 # Add project_id to url_options
Eric Davis
Fixes Issue sorting in a project, broken by #2317...
r2322 url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
Jean-Philippe Lang
SortHelper refactoring:...
r2503
Jean-Philippe Lang
Additional escaping....
r6207 link_to_content_update(h(caption), url_options, :class => css)
Jean-Philippe Lang
Initial commit...
r2 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 = {})
Jean-Philippe Lang
SortHelper refactoring:...
r2503 caption = options.delete(:caption) || column.to_s.humanize
Jean-Philippe Lang
More appropriate default sort order on sortable columns....
r1107 default_order = options.delete(:default_order) || 'asc'
Jean-Philippe Lang
SortHelper refactoring:...
r2503 options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]
Jean-Philippe Lang
More appropriate default sort order on sortable columns....
r1107 content_tag('th', sort_link(column, caption, default_order), options)
Jean-Philippe Lang
Initial commit...
r2 end
end
Jean-Philippe Lang
SortHelper refactoring:...
r2503