@@ -75,14 +75,12 @@ def initialize(search, attribute, args, params)
75
75
@search = search
76
76
@params = params
77
77
@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
79
79
@current_dir = existing_sort_direction
80
80
@label_text = extract_label_and_mutate_args! ( args )
81
81
@options = extract_options_and_mutate_args! ( args )
82
82
@hide_indicator = @options . delete :hide_indicator
83
83
@default_order = @options . delete :default_order
84
- @sort_params = build_sort ( sort_fields )
85
- @sort_params = @sort_params . first if @sort_params . size == 1
86
84
end
87
85
88
86
def name
@@ -100,10 +98,10 @@ def url_options
100
98
101
99
def html_options ( args )
102
100
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
+ )
107
105
end
108
106
109
107
private
@@ -120,7 +118,7 @@ def extract_label_and_mutate_args!(args)
120
118
if args . first . is_a? String
121
119
args . shift
122
120
else
123
- Translate . attribute ( @field , : context => @search . context )
121
+ Translate . attribute ( @field , context : @search . context )
124
122
end
125
123
end
126
124
@@ -133,16 +131,25 @@ def extract_options_and_mutate_args!(args)
133
131
end
134
132
135
133
def search_and_sort_params
136
- search_params . merge ( :s => @ sort_params)
134
+ search_params . merge ( s : sort_params )
137
135
end
138
136
139
137
def search_params
140
138
@params [ @search . context . search_key ] . presence || { }
141
139
end
142
140
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 )
144
151
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 )
146
153
end
147
154
148
155
def parse_sort ( field )
@@ -154,8 +161,7 @@ def parse_sort(field)
154
161
end
155
162
156
163
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 )
159
165
direction_text ( sort_dir )
160
166
else
161
167
default_sort_order ( attr_name ) || Constants ::ASC
@@ -169,7 +175,11 @@ def existing_sort_direction(attr_name = @field)
169
175
end
170
176
171
177
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
173
183
end
174
184
175
185
def order_indicator
0 commit comments