Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add default_sort_value to configuration #4

Merged
merged 2 commits into from Mar 1, 2012
Jump to file or symbol
Failed to load files and symbols.
+12 −2
Split
@@ -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]