annotate .svn/pristine/35/35a0a9f6553f729051c01dc102ed98a7a0151c3f.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents 261b3d9a4903
children
rev   line source
Chris@1464 1 # encoding: utf-8
Chris@1464 2 #
Chris@1464 3 # Helpers to sort tables using clickable column headers.
Chris@1464 4 #
Chris@1464 5 # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
Chris@1464 6 # Jean-Philippe Lang, 2009
Chris@1464 7 # License: This source code is released under the MIT license.
Chris@1464 8 #
Chris@1464 9 # - Consecutive clicks toggle the column's sort order.
Chris@1464 10 # - Sort state is maintained by a session hash entry.
Chris@1464 11 # - CSS classes identify sort column and state.
Chris@1464 12 # - Typically used in conjunction with the Pagination module.
Chris@1464 13 #
Chris@1464 14 # Example code snippets:
Chris@1464 15 #
Chris@1464 16 # Controller:
Chris@1464 17 #
Chris@1464 18 # helper :sort
Chris@1464 19 # include SortHelper
Chris@1464 20 #
Chris@1464 21 # def list
Chris@1464 22 # sort_init 'last_name'
Chris@1464 23 # sort_update %w(first_name last_name)
Chris@1464 24 # @items = Contact.find_all nil, sort_clause
Chris@1464 25 # end
Chris@1464 26 #
Chris@1464 27 # Controller (using Pagination module):
Chris@1464 28 #
Chris@1464 29 # helper :sort
Chris@1464 30 # include SortHelper
Chris@1464 31 #
Chris@1464 32 # def list
Chris@1464 33 # sort_init 'last_name'
Chris@1464 34 # sort_update %w(first_name last_name)
Chris@1464 35 # @contact_pages, @items = paginate :contacts,
Chris@1464 36 # :order_by => sort_clause,
Chris@1464 37 # :per_page => 10
Chris@1464 38 # end
Chris@1464 39 #
Chris@1464 40 # View (table header in list.rhtml):
Chris@1464 41 #
Chris@1464 42 # <thead>
Chris@1464 43 # <tr>
Chris@1464 44 # <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
Chris@1464 45 # <%= sort_header_tag('last_name', :caption => 'Name') %>
Chris@1464 46 # <%= sort_header_tag('phone') %>
Chris@1464 47 # <%= sort_header_tag('address', :width => 200) %>
Chris@1464 48 # </tr>
Chris@1464 49 # </thead>
Chris@1464 50 #
Chris@1464 51 # - Introduces instance variables: @sort_default, @sort_criteria
Chris@1464 52 # - Introduces param :sort
Chris@1464 53 #
Chris@1464 54
Chris@1464 55 module SortHelper
Chris@1464 56 class SortCriteria
Chris@1464 57
Chris@1464 58 def initialize
Chris@1464 59 @criteria = []
Chris@1464 60 end
Chris@1464 61
Chris@1464 62 def available_criteria=(criteria)
Chris@1464 63 unless criteria.is_a?(Hash)
Chris@1464 64 criteria = criteria.inject({}) {|h,k| h[k] = k; h}
Chris@1464 65 end
Chris@1464 66 @available_criteria = criteria
Chris@1464 67 end
Chris@1464 68
Chris@1464 69 def from_param(param)
Chris@1464 70 @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
Chris@1464 71 normalize!
Chris@1464 72 end
Chris@1464 73
Chris@1464 74 def criteria=(arg)
Chris@1464 75 @criteria = arg
Chris@1464 76 normalize!
Chris@1464 77 end
Chris@1464 78
Chris@1464 79 def to_param
Chris@1464 80 @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
Chris@1464 81 end
Chris@1464 82
Chris@1464 83 # Returns an array of SQL fragments used to sort the list
Chris@1464 84 def to_sql
Chris@1464 85 sql = @criteria.collect do |k,o|
Chris@1464 86 if s = @available_criteria[k]
Chris@1464 87 (o ? s.to_a : s.to_a.collect {|c| append_desc(c)})
Chris@1464 88 end
Chris@1464 89 end.flatten.compact
Chris@1464 90 sql.blank? ? nil : sql
Chris@1464 91 end
Chris@1464 92
Chris@1464 93 def to_a
Chris@1464 94 @criteria.dup
Chris@1464 95 end
Chris@1464 96
Chris@1464 97 def add!(key, asc)
Chris@1464 98 @criteria.delete_if {|k,o| k == key}
Chris@1464 99 @criteria = [[key, asc]] + @criteria
Chris@1464 100 normalize!
Chris@1464 101 end
Chris@1464 102
Chris@1464 103 def add(*args)
Chris@1464 104 r = self.class.new.from_param(to_param)
Chris@1464 105 r.add!(*args)
Chris@1464 106 r
Chris@1464 107 end
Chris@1464 108
Chris@1464 109 def first_key
Chris@1464 110 @criteria.first && @criteria.first.first
Chris@1464 111 end
Chris@1464 112
Chris@1464 113 def first_asc?
Chris@1464 114 @criteria.first && @criteria.first.last
Chris@1464 115 end
Chris@1464 116
Chris@1464 117 def empty?
Chris@1464 118 @criteria.empty?
Chris@1464 119 end
Chris@1464 120
Chris@1464 121 private
Chris@1464 122
Chris@1464 123 def normalize!
Chris@1464 124 @criteria ||= []
Chris@1464 125 @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]}
Chris@1464 126 @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria
Chris@1464 127 @criteria.slice!(3)
Chris@1464 128 self
Chris@1464 129 end
Chris@1464 130
Chris@1464 131 # Appends DESC to the sort criterion unless it has a fixed order
Chris@1464 132 def append_desc(criterion)
Chris@1464 133 if criterion =~ / (asc|desc)$/i
Chris@1464 134 criterion
Chris@1464 135 else
Chris@1464 136 "#{criterion} DESC"
Chris@1464 137 end
Chris@1464 138 end
Chris@1464 139 end
Chris@1464 140
Chris@1464 141 def sort_name
Chris@1464 142 controller_name + '_' + action_name + '_sort'
Chris@1464 143 end
Chris@1464 144
Chris@1464 145 # Initializes the default sort.
Chris@1464 146 # Examples:
Chris@1464 147 #
Chris@1464 148 # sort_init 'name'
Chris@1464 149 # sort_init 'id', 'desc'
Chris@1464 150 # sort_init ['name', ['id', 'desc']]
Chris@1464 151 # sort_init [['name', 'desc'], ['id', 'desc']]
Chris@1464 152 #
Chris@1464 153 def sort_init(*args)
Chris@1464 154 case args.size
Chris@1464 155 when 1
Chris@1464 156 @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]]
Chris@1464 157 when 2
Chris@1464 158 @sort_default = [[args.first, args.last]]
Chris@1464 159 else
Chris@1464 160 raise ArgumentError
Chris@1464 161 end
Chris@1464 162 end
Chris@1464 163
Chris@1464 164 # Updates the sort state. Call this in the controller prior to calling
Chris@1464 165 # sort_clause.
Chris@1464 166 # - criteria can be either an array or a hash of allowed keys
Chris@1464 167 #
Chris@1464 168 def sort_update(criteria, sort_name=nil)
Chris@1464 169 sort_name ||= self.sort_name
Chris@1464 170 @sort_criteria = SortCriteria.new
Chris@1464 171 @sort_criteria.available_criteria = criteria
Chris@1464 172 @sort_criteria.from_param(params[:sort] || session[sort_name])
Chris@1464 173 @sort_criteria.criteria = @sort_default if @sort_criteria.empty?
Chris@1464 174 session[sort_name] = @sort_criteria.to_param
Chris@1464 175 end
Chris@1464 176
Chris@1464 177 # Clears the sort criteria session data
Chris@1464 178 #
Chris@1464 179 def sort_clear
Chris@1464 180 session[sort_name] = nil
Chris@1464 181 end
Chris@1464 182
Chris@1464 183 # Returns an SQL sort clause corresponding to the current sort state.
Chris@1464 184 # Use this to sort the controller's table items collection.
Chris@1464 185 #
Chris@1464 186 def sort_clause()
Chris@1464 187 @sort_criteria.to_sql
Chris@1464 188 end
Chris@1464 189
Chris@1464 190 def sort_criteria
Chris@1464 191 @sort_criteria
Chris@1464 192 end
Chris@1464 193
Chris@1464 194 # Returns a link which sorts by the named column.
Chris@1464 195 #
Chris@1464 196 # - column is the name of an attribute in the sorted record collection.
Chris@1464 197 # - the optional caption explicitly specifies the displayed link text.
Chris@1464 198 # - 2 CSS classes reflect the state of the link: sort and asc or desc
Chris@1464 199 #
Chris@1464 200 def sort_link(column, caption, default_order)
Chris@1464 201 css, order = nil, default_order
Chris@1464 202
Chris@1464 203 if column.to_s == @sort_criteria.first_key
Chris@1464 204 if @sort_criteria.first_asc?
Chris@1464 205 css = 'sort asc'
Chris@1464 206 order = 'desc'
Chris@1464 207 else
Chris@1464 208 css = 'sort desc'
Chris@1464 209 order = 'asc'
Chris@1464 210 end
Chris@1464 211 end
Chris@1464 212 caption = column.to_s.humanize unless caption
Chris@1464 213
Chris@1464 214 sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
Chris@1464 215 url_options = params.merge(sort_options)
Chris@1464 216
Chris@1464 217 # Add project_id to url_options
Chris@1464 218 url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
Chris@1464 219
Chris@1464 220 link_to_content_update(h(caption), url_options, :class => css)
Chris@1464 221 end
Chris@1464 222
Chris@1464 223 # Returns a table header <th> tag with a sort link for the named column
Chris@1464 224 # attribute.
Chris@1464 225 #
Chris@1464 226 # Options:
Chris@1464 227 # :caption The displayed link name (defaults to titleized column name).
Chris@1464 228 # :title The tag's 'title' attribute (defaults to 'Sort by :caption').
Chris@1464 229 #
Chris@1464 230 # Other options hash entries generate additional table header tag attributes.
Chris@1464 231 #
Chris@1464 232 # Example:
Chris@1464 233 #
Chris@1464 234 # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
Chris@1464 235 #
Chris@1464 236 def sort_header_tag(column, options = {})
Chris@1464 237 caption = options.delete(:caption) || column.to_s.humanize
Chris@1464 238 default_order = options.delete(:default_order) || 'asc'
Chris@1464 239 options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]
Chris@1464 240 content_tag('th', sort_link(column, caption, default_order), options)
Chris@1464 241 end
Chris@1464 242 end
Chris@1464 243