Refactor FormHelper#sort_link
- Simplify the #initialize method by moving the `sort_params` logic into its own method. - Avoid hidden conditionals and ternaries to keep conditionals as explicit (and painful) as possible. - Use Ruby 1.9+ hash syntax.
This commit is contained in:
parent
eadbe1242e
commit
17dd97af1c
|
@ -75,14 +75,12 @@ module Ransack
|
||||||
@search = search
|
@search = search
|
||||||
@params = params
|
@params = params
|
||||||
@field = attribute.to_s
|
@field = attribute.to_s
|
||||||
sort_fields = extract_sort_fields_and_mutate_args!(args).compact
|
@sort_fields = extract_sort_fields_and_mutate_args!(args).compact
|
||||||
@current_dir = existing_sort_direction
|
@current_dir = existing_sort_direction
|
||||||
@label_text = extract_label_and_mutate_args!(args)
|
@label_text = extract_label_and_mutate_args!(args)
|
||||||
@options = extract_options_and_mutate_args!(args)
|
@options = extract_options_and_mutate_args!(args)
|
||||||
@hide_indicator = @options.delete :hide_indicator
|
@hide_indicator = @options.delete :hide_indicator
|
||||||
@default_order = @options.delete :default_order
|
@default_order = @options.delete :default_order
|
||||||
@sort_params = build_sort(sort_fields)
|
|
||||||
@sort_params = @sort_params.first if @sort_params.size == 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -100,10 +98,10 @@ module Ransack
|
||||||
|
|
||||||
def html_options(args)
|
def html_options(args)
|
||||||
html_options = extract_options_and_mutate_args!(args)
|
html_options = extract_options_and_mutate_args!(args)
|
||||||
html_options.merge(class:
|
html_options.merge(
|
||||||
[[Constants::SORT_LINK, @current_dir], html_options[:class]]
|
class: [[Constants::SORT_LINK, @current_dir], html_options[:class]]
|
||||||
.compact.join(Constants::SPACE)
|
.compact.join(Constants::SPACE)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -120,7 +118,7 @@ module Ransack
|
||||||
if args.first.is_a? String
|
if args.first.is_a? String
|
||||||
args.shift
|
args.shift
|
||||||
else
|
else
|
||||||
Translate.attribute(@field, :context => @search.context)
|
Translate.attribute(@field, context: @search.context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -133,16 +131,25 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_and_sort_params
|
def search_and_sort_params
|
||||||
search_params.merge(:s => @sort_params)
|
search_params.merge(s: sort_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_params
|
def search_params
|
||||||
@params[@search.context.search_key].presence || {}
|
@params[@search.context.search_key].presence || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_sort(fields)
|
def sort_params
|
||||||
|
sort_array = recursive_sort_params_build(@sort_fields)
|
||||||
|
if sort_array.size == 1
|
||||||
|
sort_array.first
|
||||||
|
else
|
||||||
|
sort_array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def recursive_sort_params_build(fields)
|
||||||
return [] if fields.empty?
|
return [] if fields.empty?
|
||||||
[parse_sort(fields[0])] + build_sort(fields.drop(1))
|
[parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_sort(field)
|
def parse_sort(field)
|
||||||
|
@ -154,8 +161,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_previous_sort_direction_and_invert_it(attr_name)
|
def detect_previous_sort_direction_and_invert_it(attr_name)
|
||||||
sort_dir = existing_sort_direction(attr_name)
|
if sort_dir = existing_sort_direction(attr_name)
|
||||||
if sort_dir
|
|
||||||
direction_text(sort_dir)
|
direction_text(sort_dir)
|
||||||
else
|
else
|
||||||
default_sort_order(attr_name) || Constants::ASC
|
default_sort_order(attr_name) || Constants::ASC
|
||||||
|
@ -169,7 +175,11 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_sort_order(attr_name)
|
def default_sort_order(attr_name)
|
||||||
Hash === @default_order ? @default_order[attr_name] : @default_order
|
if Hash === @default_order
|
||||||
|
@default_order[attr_name]
|
||||||
|
else
|
||||||
|
@default_order
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def order_indicator
|
def order_indicator
|
||||||
|
|
Loading…
Reference in New Issue