From 1515a18a9ee016c9ac8452446d56c3dee4b7e5e1 Mon Sep 17 00:00:00 2001 From: shea Date: Thu, 14 Apr 2016 12:13:54 -0500 Subject: [PATCH 1/4] title column now sortable --- app/controllers/studies_controller.rb | 11 ++++++++++- app/helpers/application_helper.rb | 7 +++++++ app/views/studies/index.html.erb | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/studies_controller.rb b/app/controllers/studies_controller.rb index ff32701..fba1ef7 100644 --- a/app/controllers/studies_controller.rb +++ b/app/controllers/studies_controller.rb @@ -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 @@ -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] : "name" + end + + def sort_direction + %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" + end + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..340215b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/studies/index.html.erb b/app/views/studies/index.html.erb index c02be99..04daa3b 100644 --- a/app/views/studies/index.html.erb +++ b/app/views/studies/index.html.erb @@ -4,7 +4,7 @@ Study Number - Title + <%= sort_by_column "title" %> Principal Investigator Status From 3a5ae0cb6dd57fb9dc8ca2735a00543cfbaed221 Mon Sep 17 00:00:00 2001 From: shea Date: Thu, 14 Apr 2016 12:17:49 -0500 Subject: [PATCH 2/4] added sort column functionality to rest of table --- app/views/studies/index.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/studies/index.html.erb b/app/views/studies/index.html.erb index 04daa3b..6bf65d2 100644 --- a/app/views/studies/index.html.erb +++ b/app/views/studies/index.html.erb @@ -3,10 +3,10 @@ - + - - + + From ec5c5ff89933c6b4725f9c32adf2b0bfe0ef29bb Mon Sep 17 00:00:00 2001 From: shea Date: Thu, 14 Apr 2016 12:23:50 -0500 Subject: [PATCH 3/4] fixed a minor bug with default params --- app/controllers/studies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/studies_controller.rb b/app/controllers/studies_controller.rb index fba1ef7..3944606 100644 --- a/app/controllers/studies_controller.rb +++ b/app/controllers/studies_controller.rb @@ -37,7 +37,7 @@ def study_params end def sort_column - Study.column_names.include?(params[:sort]) ? params[:sort] : "name" + Study.column_names.include?(params[:sort]) ? params[:sort] : "titlete" end def sort_direction From 6d93167b640a90e5e3e38624049bb95c355ab3a3 Mon Sep 17 00:00:00 2001 From: shea Date: Thu, 14 Apr 2016 12:25:07 -0500 Subject: [PATCH 4/4] fixed typo caused from switching desktops too quickly --- app/controllers/studies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/studies_controller.rb b/app/controllers/studies_controller.rb index 3944606..f0e716a 100644 --- a/app/controllers/studies_controller.rb +++ b/app/controllers/studies_controller.rb @@ -37,7 +37,7 @@ def study_params end def sort_column - Study.column_names.include?(params[:sort]) ? params[:sort] : "titlete" + Study.column_names.include?(params[:sort]) ? params[:sort] : "title" end def sort_direction
Study Number<%= sort_by_column "id", "Study Number" %> <%= sort_by_column "title" %>Principal InvestigatorStatus<%= sort_by_column "principal_investigator", "Principal Investigator" %><%= sort_by_column "open", "Status"%>