Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/controllers/studies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class StudiesController < ApplicationController
helper_method :sort_column, :sort_direction

def index
@studies = Study.all
@studies = Study.order(sort_column + " " + sort_direction)
end

def show
Expand Down Expand Up @@ -35,4 +36,12 @@ def study_params
params.require(:study).permit(:title, :principal_investigator, :open)
end

def sort_column
Study.column_names.include?(params[:sort]) ? params[:sort] : "title"
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end

end
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
module ApplicationHelper

def sort_by_column(column, title = nil)
title ||= column.titleize
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}
end

end
8 changes: 4 additions & 4 deletions app/views/studies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<table id="study_table">
<thead>
<tr>
<th>Study Number</th>
<th>Title</th>
<th>Principal Investigator</th>
<th>Status</th>
<th><%= sort_by_column "id", "Study Number" %></th>
<th><%= sort_by_column "title" %></th>
<th><%= sort_by_column "principal_investigator", "Principal Investigator" %></th>
<th><%= sort_by_column "open", "Status"%></th>
<th></th>
</tr>
</thead>
Expand Down