Skip to content

Commit e93eb4d

Browse files
authored
Merge pull request #2012 from codidact/0valt/2003/filter-apply-sorting
2 parents 3d890ef + ddac455 commit e93eb4d

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

app/controllers/categories_controller.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,15 @@ def verify_view_access
158158
end
159159

160160
def set_list_posts
161-
sort_params = { activity: { last_activity: :desc }, age: { created_at: :desc }, score: { score: :desc },
161+
@default_sort_type = :activity
162+
@sort_type = params[:sort]&.to_sym || @default_sort_type
163+
164+
sort_params = { activity: { last_activity: :desc },
165+
age: { created_at: :desc },
166+
score: { score: :desc },
162167
lottery: [Arel.sql('(RAND() - ? * DATEDIFF(CURRENT_TIMESTAMP, posts.created_at)) DESC'),
163168
SiteSetting['LotteryAgeDeprecationSpeed']] }
164-
sort_param = sort_params[params[:sort]&.to_sym] || { last_activity: :desc }
169+
165170
@posts = @category.posts
166171
.undeleted
167172
.where(post_type_id: @category.display_post_types)
@@ -200,7 +205,8 @@ def set_list_posts
200205

201206
@posts = helpers.qualifiers_to_sql(filter_qualifiers, @posts, current_user)
202207
@filtered = filter_qualifiers.any?
203-
@posts = @posts.paginate(page: params[:page], per_page: 50).order(sort_param)
208+
@posts = @posts.paginate(page: params[:page], per_page: 50)
209+
.order(sort_params[@sort_type])
204210
end
205211

206212
def clear_categories_cache

app/views/search/_filters.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<% allow_delete ||= false %>
22
<% allow_apply = true if allow_apply.nil? %>
33
<% @active_filter ||= {} %>
4+
<% @default_sort_type ||= '' %>
45

56
<div class="form-group">
6-
<input name="sort" type="hidden" value="<%= params[:sort] || '' %>" />
7+
<input name="sort" type="hidden" value="<%= params[:sort] || @default_sort_type %>" />
78

89
<div class="search-filters-filter">
910
<div>

0 commit comments

Comments
 (0)