Created
July 7, 2015 03:01
-
-
Save ajm188/ce80e46463a3adf6dc7a to your computer and use it in GitHub Desktop.
example of controller code that has been bloated by needing to check many query parameters
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
class CoursesController < ApplicationController | |
def index | |
@semesters = Semester.all | |
@courses = Course.all | |
if params[:search].present? | |
@courses = @courses.search(params[:search]) | |
end | |
if params[:semester].present? | |
@courses = | |
@courses.joins(:course_instances) | |
.where(course_instances: { semester_id: params[:semester].to_i }) | |
end | |
if params[:professor].present? | |
professor = Professor.arel_table | |
@courses = | |
@courses.joins(course_instances: :professor) | |
.where(professor[:name].matches("%#{params[:professor]}%")) | |
end | |
course_ids = @courses.pluck('courses.id').uniq | |
@courses = Course.where('id IN (?)', course_ids).order_by_short_name | |
@courses = @courses.page(params[:page]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment