Skip to content

Instantly share code, notes, and snippets.

@ajm188
Created July 7, 2015 03:01
Show Gist options
  • Save ajm188/ce80e46463a3adf6dc7a to your computer and use it in GitHub Desktop.
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
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