Skip to content

Commit 17dd97a

Browse files
committed
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.
1 parent eadbe12 commit 17dd97a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

lib/ransack/helpers/form_helper.rb

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ def initialize(search, attribute, args, params)
7575
@search = search
7676
@params = params
7777
@field = attribute.to_s
78-
sort_fields = extract_sort_fields_and_mutate_args!(args).compact
78+
@sort_fields = extract_sort_fields_and_mutate_args!(args).compact
7979
@current_dir = existing_sort_direction
8080
@label_text = extract_label_and_mutate_args!(args)
8181
@options = extract_options_and_mutate_args!(args)
8282
@hide_indicator = @options.delete :hide_indicator
8383
@default_order = @options.delete :default_order
84-
@sort_params = build_sort(sort_fields)
85-
@sort_params = @sort_params.first if @sort_params.size == 1
8684
end
8785

8886
def name
@@ -100,10 +98,10 @@ def url_options
10098

10199
def html_options(args)
102100
html_options = extract_options_and_mutate_args!(args)
103-
html_options.merge(class:
104-
[[Constants::SORT_LINK, @current_dir], html_options[:class]]
105-
.compact.join(Constants::SPACE)
106-
)
101+
html_options.merge(
102+
class: [[Constants::SORT_LINK, @current_dir], html_options[:class]]
103+
.compact.join(Constants::SPACE)
104+
)
107105
end
108106

109107
private
@@ -120,7 +118,7 @@ def extract_label_and_mutate_args!(args)
120118
if args.first.is_a? String
121119
args.shift
122120
else
123-
Translate.attribute(@field, :context => @search.context)
121+
Translate.attribute(@field, context: @search.context)
124122
end
125123
end
126124

@@ -133,16 +131,25 @@ def extract_options_and_mutate_args!(args)
133131
end
134132

135133
def search_and_sort_params
136-
search_params.merge(:s => @sort_params)
134+
search_params.merge(s: sort_params)
137135
end
138136

139137
def search_params
140138
@params[@search.context.search_key].presence || {}
141139
end
142140

143-
def build_sort(fields)
141+
def sort_params
142+
sort_array = recursive_sort_params_build(@sort_fields)
143+
if sort_array.size == 1
144+
sort_array.first
145+
else
146+
sort_array
147+
end
148+
end
149+
150+
def recursive_sort_params_build(fields)
144151
return [] if fields.empty?
145-
[parse_sort(fields[0])] + build_sort(fields.drop(1))
152+
[parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1)
146153
end
147154

148155
def parse_sort(field)
@@ -154,8 +161,7 @@ def parse_sort(field)
154161
end
155162

156163
def detect_previous_sort_direction_and_invert_it(attr_name)
157-
sort_dir = existing_sort_direction(attr_name)
158-
if sort_dir
164+
if sort_dir = existing_sort_direction(attr_name)
159165
direction_text(sort_dir)
160166
else
161167
default_sort_order(attr_name) || Constants::ASC
@@ -169,7 +175,11 @@ def existing_sort_direction(attr_name = @field)
169175
end
170176

171177
def default_sort_order(attr_name)
172-
Hash === @default_order ? @default_order[attr_name] : @default_order
178+
if Hash === @default_order
179+
@default_order[attr_name]
180+
else
181+
@default_order
182+
end
173183
end
174184

175185
def order_indicator

0 commit comments

Comments
 (0)