Move SchoolClassesController#apply_filter_parameters to SchoolClass#search

parent 05017403
......@@ -7,7 +7,7 @@ class SchoolClassesController < ApplicationController
if params[:search]
# filter relevant parameters
@search_params = school_class_params.symbolize_keys
@avg_enrollments = apply_search_filters(@search_params)
@result = SchoolClass.search(@search_params)
end
end
......@@ -75,30 +75,7 @@ class SchoolClassesController < ApplicationController
def school_class_params
params.require(:school_class)
.permit(:search, :state_id, :city_id, :region_id, :census_year_start, :census_year_end,
:administrative_level_id)
:administrative_level_id, :variable, :location_id, :school_class_type_id)
.keep_if { |k,v| !v.nil? and !v.empty? }
end
def apply_search_filters(filtered_params)
school_classes = SchoolClass
filtered_params.each do |param, value|
if param.eql? :state_id
school_classes = school_classes.where([ "state_id = :state_id", { state_id: value.to_i} ])
elsif param.eql? :city_id
school_classes = school_classes.where([ "city_id = :city_id", { city_id: value.to_i } ])
#elsif param.eql? :region_id
# school_classes = school_classes.where([ "region_id = :region_id", { region_id: value.to_i } ])
elsif param.eql? :census_year_start
school_classes = school_classes.where([ "census_year >= :census_year", { census_year: value.to_i } ])
elsif param.eql? :census_year_end
school_classes = school_classes.where([ "census_year <= :census_year", { census_year: value.to_i } ])
end
end
if filtered_params.empty?
school_classes.all.average(:num_enrollments)
else
school_classes.average(:num_enrollments)
end
end
end
......@@ -27,7 +27,32 @@ class SchoolClass < ActiveRecord::Base
maximum(:census_year)
end
def search(params)
def self.search(filtered_params)
params = filtered_params
school_classes = self
if filtered_params.size > 1
school_classes = school_classes.where(state_id: params[:state_id]) if params[:state_id]
school_classes = school_classes.where(city_id: params[:city_id]) if params[:city_id]
school_classes = school_classes.where(school_type_id: params[:school_type_id]) if params[:school_type_id]
if params[:census_year_start]
school_classes = school_classes.where([ "census_year >= :census_year",
{ census_year: params[:census_year_start] } ])
end
if params[:census_year_end]
school_classes = school_classes.where([ "census_year <= :census_year",
{ census_year: params[:census_year_end] } ])
end
else
school_classes = school_classes.all
end
case params[:variable]
when "enrollments"
school_classes = school_classes.sum(:num_enrollments)
when "teachers"
school_classes = school_classes.sum(:num_enrollments)
when "schools"
school_classes = school_classes.sum(:num_enrollments)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment