Skip to content

Commit

Permalink
Merge pull request #385 from griffithlab/fix-user-event-sorting
Browse files Browse the repository at this point in the history
Share sorting clause code between controllers.
  • Loading branch information
acoffman authored Dec 22, 2017
2 parents 808ef16 + 7623cb7 commit 76ac014
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
13 changes: 13 additions & 0 deletions app/controllers/concerns/with_sorting.rb
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
16 changes: 3 additions & 13 deletions app/controllers/events_controller.rb
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
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class UsersController < ApplicationController
include WithSoftDeletion
include WithSorting

actions_without_auth :events, :show, :index, :username_suggestions, :username_status

Expand All @@ -17,7 +18,7 @@ def events
.includes(:subject)
.page(params[:page])
.per(params[:count])
.order('created_at DESC')
.order("events.created_at #{sort_direction('timestamp')}")

render json: EventsPresenter.new(events)
end
Expand Down

0 comments on commit 76ac014

Please sign in to comment.