Skip to content

Commit

Permalink
Refactor FormHelper#sort_link
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jonatack committed Jul 26, 2015
1 parent eadbe12 commit 17dd97a
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ def initialize(search, attribute, args, params)
@search = search
@params = params
@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
@label_text = extract_label_and_mutate_args!(args)
@options = extract_options_and_mutate_args!(args)
@hide_indicator = @options.delete :hide_indicator
@default_order = @options.delete :default_order
@sort_params = build_sort(sort_fields)
@sort_params = @sort_params.first if @sort_params.size == 1
end

def name
Expand All @@ -100,10 +98,10 @@ def url_options

def html_options(args)
html_options = extract_options_and_mutate_args!(args)
html_options.merge(class:
[[Constants::SORT_LINK, @current_dir], html_options[:class]]
.compact.join(Constants::SPACE)
)
html_options.merge(
class: [[Constants::SORT_LINK, @current_dir], html_options[:class]]
.compact.join(Constants::SPACE)
)
end

private
Expand All @@ -120,7 +118,7 @@ def extract_label_and_mutate_args!(args)
if args.first.is_a? String
args.shift
else
Translate.attribute(@field, :context => @search.context)
Translate.attribute(@field, context: @search.context)
end
end

Expand All @@ -133,16 +131,25 @@ def extract_options_and_mutate_args!(args)
end

def search_and_sort_params
search_params.merge(:s => @sort_params)
search_params.merge(s: sort_params)
end

def search_params
@params[@search.context.search_key].presence || {}
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?
[parse_sort(fields[0])] + build_sort(fields.drop(1))
[parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1)
end

def parse_sort(field)
Expand All @@ -154,8 +161,7 @@ def parse_sort(field)
end

def detect_previous_sort_direction_and_invert_it(attr_name)
sort_dir = existing_sort_direction(attr_name)
if sort_dir
if sort_dir = existing_sort_direction(attr_name)
direction_text(sort_dir)
else
default_sort_order(attr_name) || Constants::ASC
Expand All @@ -169,7 +175,11 @@ def existing_sort_direction(attr_name = @field)
end

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

def order_indicator
Expand Down

0 comments on commit 17dd97a

Please sign in to comment.