Permalink
Browse files

Merge pull request #4 from dimfeld/master

Add default_sort_value to configuration
  • Loading branch information...
2 parents 86eb606 + a95cd72 commit 65af202470c79bdc55ca05e91c1e39526ebea11b Alex Fortuna committed Mar 1, 2012
Showing with 12 additions and 2 deletions.
  1. +12 −2 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]

0 comments on commit 65af202

Please sign in to comment.