-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share sorting clause code between controllers.
Fixes sorting events on user activity page.
- Loading branch information
Showing
3 changed files
with
18 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module WithSorting | ||
extend ActiveSupport::Concern | ||
|
||
def sort_direction(field_name) | ||
if params["sorting"].blank? | ||
'DESC' | ||
elsif params["sorting"][field_name].present? && params["sorting"][field_name].upcase == 'DESC' | ||
'DESC' | ||
else | ||
'ASC' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,14 @@ | ||
class EventsController < ApplicationController | ||
include WithSorting | ||
|
||
actions_without_auth :index | ||
|
||
def index | ||
events = Event.includes(:originating_user, :subject) | ||
.page(params[:page]) | ||
.per(params[:count]) | ||
.order("events.created_at #{sort_clause}") | ||
.order("events.created_at #{sort_direction('timestamp')}") | ||
|
||
render json: EventsPresenter.new(events) | ||
end | ||
|
||
|
||
private | ||
def sort_clause | ||
if params["sorting"].blank? | ||
'DESC' | ||
elsif params["sorting"]["timestamp"].present? && params["sorting"]["timestamp"].upcase == 'DESC' | ||
'DESC' | ||
else | ||
'ASC' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters