Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add default_sort_value to configuration #4

Merged
merged 2 commits into from

2 participants

@dimfeld

Here's a minor enhancement that I found useful:

When the sort parameter is not given, the controller may
specify a default in the configuration object's default_sort_value
member. This is mostly done to allow the sorting links to render
the default sort link with the proper CSS class and indicator when
a page is first loaded.

dimfeld added some commits
@dimfeld dimfeld Add a configurable default sort value.
When the sort parameter is not given, the controller may
specify a default in the configuration object's default_sort_value
member. This is mostly done to allow the sorting links to render
the selected sort with the proper CSS class and indicator when
a page is first loaded.
505431a
@dimfeld dimfeld Remove debugging statement a95cd72
@dadooda dadooda merged commit 65af202 into from
@dadooda
Owner

Daniel

Thanks for your commitment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2012
  1. @dimfeld

    Add a configurable default sort value.

    dimfeld authored
    When the sort parameter is not given, the controller may
    specify a default in the configuration object's default_sort_value
    member. This is mostly done to allow the sorting links to render
    the selected sort with the proper CSS class and indicator when
    a page is first loaded.
  2. @dimfeld

    Remove debugging statement

    dimfeld authored
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 2 deletions.
  1. +12 −2 lib/handles/sortable_columns.rb
View
14 lib/handles/sortable_columns.rb
@@ -55,6 +55,11 @@ class Config
# sort
attr_accessor :sort_param
+ # Default sort direction, if params[sort_param] is not given.
+ #
+ # default_sort_value
+ attr_accessor :default_sort_value
+
# Sort indicator text. If any of values are empty, indicator is not displayed. Default:
#
# {:asc => " ↓ ", :desc => " ↑ "}
@@ -72,6 +77,7 @@ def initialize(attrs = {})
:indicator_text => {:asc => " ↓ ", :desc => " ↑ "},
:page_param => "page",
:sort_param => "sort",
+ :default_sort_value => nil
}
defaults.merge(attrs).each {|k, v| send("#{k}=", v)}
@@ -169,6 +175,7 @@ def sortable_column(title, options = {}) #:doc:
o = {}
conf = {}
conf[k = :sort_param] = sortable_columns_config[k]
+ conf[k = :default_sort_value] = sortable_columns_config[k]
conf[k = :page_param] = sortable_columns_config[k]
conf[k = :indicator_text] = sortable_columns_config[k]
conf[k = :indicator_class] = sortable_columns_config[k]
@@ -183,7 +190,8 @@ def sortable_column(title, options = {}) #:doc:
raise "Unknown option(s): #{options.inspect}" if not options.empty?
# Parse sort param.
- pp = parse_sortable_column_sort_param(params[conf[:sort_param]])
+ sort = params[conf[:sort_param]] || conf[:default_sort_value]
+ pp = parse_sortable_column_sort_param(sort)
css_class = []
if (s = o[:link_class]).present?
@@ -251,9 +259,11 @@ def sortable_column(title, options = {}) #:doc:
def sortable_column_order(&block)
conf = {}
conf[k = :sort_param] = sortable_columns_config[k]
+ conf[k = :default_sort_value] = sortable_columns_config[k]
# Parse sort param.
- pp = parse_sortable_column_sort_param(params[conf[:sort_param]])
+ sort = params[conf[:sort_param]] || conf[:default_sort_value]
+ pp = parse_sortable_column_sort_param(sort)
order = if block
column, direction = pp[:column], pp[:direction]
Something went wrong with that request. Please try again.