remove trailing white-spaces from app/helpers/sort_helper.rb.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6345 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
7a892322aa
commit
a51829eb22
@ -15,18 +15,18 @@
|
|||||||
#
|
#
|
||||||
# helper :sort
|
# helper :sort
|
||||||
# include SortHelper
|
# include SortHelper
|
||||||
#
|
#
|
||||||
# def list
|
# def list
|
||||||
# sort_init 'last_name'
|
# sort_init 'last_name'
|
||||||
# sort_update %w(first_name last_name)
|
# sort_update %w(first_name last_name)
|
||||||
# @items = Contact.find_all nil, sort_clause
|
# @items = Contact.find_all nil, sort_clause
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Controller (using Pagination module):
|
# Controller (using Pagination module):
|
||||||
#
|
#
|
||||||
# helper :sort
|
# helper :sort
|
||||||
# include SortHelper
|
# include SortHelper
|
||||||
#
|
#
|
||||||
# def list
|
# def list
|
||||||
# sort_init 'last_name'
|
# sort_init 'last_name'
|
||||||
# sort_update %w(first_name last_name)
|
# sort_update %w(first_name last_name)
|
||||||
@ -34,9 +34,9 @@
|
|||||||
# :order_by => sort_clause,
|
# :order_by => sort_clause,
|
||||||
# :per_page => 10
|
# :per_page => 10
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# View (table header in list.rhtml):
|
# View (table header in list.rhtml):
|
||||||
#
|
#
|
||||||
# <thead>
|
# <thead>
|
||||||
# <tr>
|
# <tr>
|
||||||
# <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
|
# <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
|
||||||
@ -52,32 +52,32 @@
|
|||||||
|
|
||||||
module SortHelper
|
module SortHelper
|
||||||
class SortCriteria
|
class SortCriteria
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@criteria = []
|
@criteria = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def available_criteria=(criteria)
|
def available_criteria=(criteria)
|
||||||
unless criteria.is_a?(Hash)
|
unless criteria.is_a?(Hash)
|
||||||
criteria = criteria.inject({}) {|h,k| h[k] = k; h}
|
criteria = criteria.inject({}) {|h,k| h[k] = k; h}
|
||||||
end
|
end
|
||||||
@available_criteria = criteria
|
@available_criteria = criteria
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_param(param)
|
def from_param(param)
|
||||||
@criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
|
@criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
|
||||||
normalize!
|
normalize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def criteria=(arg)
|
def criteria=(arg)
|
||||||
@criteria = arg
|
@criteria = arg
|
||||||
normalize!
|
normalize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_param
|
def to_param
|
||||||
@criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
|
@criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_sql
|
def to_sql
|
||||||
sql = @criteria.collect do |k,o|
|
sql = @criteria.collect do |k,o|
|
||||||
if s = @available_criteria[k]
|
if s = @available_criteria[k]
|
||||||
@ -86,33 +86,33 @@ module SortHelper
|
|||||||
end.compact.join(', ')
|
end.compact.join(', ')
|
||||||
sql.blank? ? nil : sql
|
sql.blank? ? nil : sql
|
||||||
end
|
end
|
||||||
|
|
||||||
def add!(key, asc)
|
def add!(key, asc)
|
||||||
@criteria.delete_if {|k,o| k == key}
|
@criteria.delete_if {|k,o| k == key}
|
||||||
@criteria = [[key, asc]] + @criteria
|
@criteria = [[key, asc]] + @criteria
|
||||||
normalize!
|
normalize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(*args)
|
def add(*args)
|
||||||
r = self.class.new.from_param(to_param)
|
r = self.class.new.from_param(to_param)
|
||||||
r.add!(*args)
|
r.add!(*args)
|
||||||
r
|
r
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_key
|
def first_key
|
||||||
@criteria.first && @criteria.first.first
|
@criteria.first && @criteria.first.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_asc?
|
def first_asc?
|
||||||
@criteria.first && @criteria.first.last
|
@criteria.first && @criteria.first.last
|
||||||
end
|
end
|
||||||
|
|
||||||
def empty?
|
def empty?
|
||||||
@criteria.empty?
|
@criteria.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def normalize!
|
def normalize!
|
||||||
@criteria ||= []
|
@criteria ||= []
|
||||||
@criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]}
|
@criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]}
|
||||||
@ -120,7 +120,7 @@ module SortHelper
|
|||||||
@criteria.slice!(3)
|
@criteria.slice!(3)
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Appends DESC to the sort criterion unless it has a fixed order
|
# Appends DESC to the sort criterion unless it has a fixed order
|
||||||
def append_desc(criterion)
|
def append_desc(criterion)
|
||||||
if criterion =~ / (asc|desc)$/i
|
if criterion =~ / (asc|desc)$/i
|
||||||
@ -130,14 +130,14 @@ module SortHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_name
|
def sort_name
|
||||||
controller_name + '_' + action_name + '_sort'
|
controller_name + '_' + action_name + '_sort'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Initializes the default sort.
|
# Initializes the default sort.
|
||||||
# Examples:
|
# Examples:
|
||||||
#
|
#
|
||||||
# sort_init 'name'
|
# sort_init 'name'
|
||||||
# sort_init 'id', 'desc'
|
# sort_init 'id', 'desc'
|
||||||
# sort_init ['name', ['id', 'desc']]
|
# sort_init ['name', ['id', 'desc']]
|
||||||
@ -165,7 +165,7 @@ module SortHelper
|
|||||||
@sort_criteria.criteria = @sort_default if @sort_criteria.empty?
|
@sort_criteria.criteria = @sort_default if @sort_criteria.empty?
|
||||||
session[sort_name] = @sort_criteria.to_param
|
session[sort_name] = @sort_criteria.to_param
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clears the sort criteria session data
|
# Clears the sort criteria session data
|
||||||
#
|
#
|
||||||
def sort_clear
|
def sort_clear
|
||||||
@ -187,7 +187,7 @@ module SortHelper
|
|||||||
#
|
#
|
||||||
def sort_link(column, caption, default_order)
|
def sort_link(column, caption, default_order)
|
||||||
css, order = nil, default_order
|
css, order = nil, default_order
|
||||||
|
|
||||||
if column.to_s == @sort_criteria.first_key
|
if column.to_s == @sort_criteria.first_key
|
||||||
if @sort_criteria.first_asc?
|
if @sort_criteria.first_asc?
|
||||||
css = 'sort asc'
|
css = 'sort asc'
|
||||||
@ -198,10 +198,10 @@ module SortHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
caption = column.to_s.humanize unless caption
|
caption = column.to_s.humanize unless caption
|
||||||
|
|
||||||
sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
|
sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
|
||||||
url_options = params.merge(sort_options)
|
url_options = params.merge(sort_options)
|
||||||
|
|
||||||
# Add project_id to url_options
|
# Add project_id to url_options
|
||||||
url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
|
url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user