"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/helpers/sort_helper.rb" (6 Apr 2020, 4673 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "sort_helper.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Helpers to sort tables using clickable column headers.
    4 #
    5 # Author:  Stuart Rackham <srackham@methods.co.nz>, March 2005.
    6 #          Jean-Philippe Lang, 2009
    7 # License: This source code is released under the MIT license.
    8 #
    9 # - Consecutive clicks toggle the column's sort order.
   10 # - Sort state is maintained by a session hash entry.
   11 # - CSS classes identify sort column and state.
   12 # - Typically used in conjunction with the Pagination module.
   13 #
   14 # Example code snippets:
   15 #
   16 # Controller:
   17 #
   18 #   helper :sort
   19 #   include SortHelper
   20 #
   21 #   def list
   22 #     sort_init 'last_name'
   23 #     sort_update %w(first_name last_name)
   24 #     @items = Contact.find_all nil, sort_clause
   25 #   end
   26 #
   27 # Controller (using Pagination module):
   28 #
   29 #   helper :sort
   30 #   include SortHelper
   31 #
   32 #   def list
   33 #     sort_init 'last_name'
   34 #     sort_update %w(first_name last_name)
   35 #     @contact_pages, @items = paginate :contacts,
   36 #       :order_by => sort_clause,
   37 #       :per_page => 10
   38 #   end
   39 #
   40 # View (table header in list.rhtml):
   41 #
   42 #   <thead>
   43 #     <tr>
   44 #       <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
   45 #       <%= sort_header_tag('last_name', :caption => 'Name') %>
   46 #       <%= sort_header_tag('phone') %>
   47 #       <%= sort_header_tag('address', :width => 200) %>
   48 #     </tr>
   49 #   </thead>
   50 #
   51 # - Introduces instance variables: @sort_default, @sort_criteria
   52 # - Introduces param :sort
   53 #
   54 
   55 module SortHelper
   56   def sort_name
   57     controller_name + '_' + action_name + '_sort'
   58   end
   59 
   60   # Initializes the default sort.
   61   # Examples:
   62   #
   63   #   sort_init 'name'
   64   #   sort_init 'id', 'desc'
   65   #   sort_init ['name', ['id', 'desc']]
   66   #   sort_init [['name', 'desc'], ['id', 'desc']]
   67   #
   68   def sort_init(*args)
   69     case args.size
   70     when 1
   71       @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]]
   72     when 2
   73       @sort_default = [[args.first, args.last]]
   74     else
   75       raise ArgumentError
   76     end
   77   end
   78 
   79   # Updates the sort state. Call this in the controller prior to calling
   80   # sort_clause.
   81   # - criteria can be either an array or a hash of allowed keys
   82   #
   83   def sort_update(criteria, sort_name=nil)
   84     sort_name ||= self.sort_name
   85     @sort_criteria = Redmine::SortCriteria.new(params[:sort] || session[sort_name] || @sort_default)
   86     @sortable_columns = criteria
   87     session[sort_name] = @sort_criteria.to_param
   88   end
   89 
   90   # Clears the sort criteria session data
   91   #
   92   def sort_clear
   93     session[sort_name] = nil
   94   end
   95 
   96   # Returns an SQL sort clause corresponding to the current sort state.
   97   # Use this to sort the controller's table items collection.
   98   #
   99   def sort_clause
  100     @sort_criteria.sort_clause(@sortable_columns)
  101   end
  102 
  103   def sort_criteria
  104     @sort_criteria
  105   end
  106 
  107   # Returns a link which sorts by the named column.
  108   #
  109   # - column is the name of an attribute in the sorted record collection.
  110   # - the optional caption explicitly specifies the displayed link text.
  111   # - 2 CSS classes reflect the state of the link: sort and asc or desc
  112   #
  113   def sort_link(column, caption, default_order)
  114     css, order = nil, default_order
  115 
  116     if column.to_s == @sort_criteria.first_key
  117       if @sort_criteria.first_asc?
  118         css = 'sort asc icon icon-sorted-desc'
  119         order = 'desc'
  120       else
  121         css = 'sort desc icon icon-sorted-asc'
  122         order = 'asc'
  123       end
  124     end
  125     caption = column.to_s.humanize unless caption
  126 
  127     sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
  128     link_to(caption, {:params => request.query_parameters.merge(sort_options)}, :class => css)
  129   end
  130 
  131   # Returns a table header <th> tag with a sort link for the named column
  132   # attribute.
  133   #
  134   # Options:
  135   #   :caption     The displayed link name (defaults to titleized column name).
  136   #   :title       The tag's 'title' attribute (defaults to 'Sort by :caption').
  137   #
  138   # Other options hash entries generate additional table header tag attributes.
  139   #
  140   # Example:
  141   #
  142   #   <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
  143   #
  144   def sort_header_tag(column, options = {})
  145     caption = options.delete(:caption) || column.to_s.humanize
  146     default_order = options.delete(:default_order) || 'asc'
  147     options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]
  148     content_tag('th', sort_link(column, caption, default_order), options)
  149   end
  150 
  151   # Returns the css classes for the current sort order
  152   #
  153   # Example:
  154   #
  155   #   sort_css_classes
  156   #   # => "sort-by-created-on sort-desc"
  157   def sort_css_classes
  158     if @sort_criteria.first_key
  159       "sort-by-#{@sort_criteria.first_key.to_s.dasherize} sort-#{@sort_criteria.first_asc? ? 'asc' : 'desc'}"
  160     end
  161   end
  162 end