Commit 05017403 authored by João Victor Risso's avatar João Victor Risso

Implement basic search in SchoolClass controller

Signed-off-by: João Victor Risso's avatarJoão Victor Tozatti Risso <jvtr12@c3sl.ufpr.br>
parent 55a1652d
......@@ -4,7 +4,11 @@ class SchoolClassesController < ApplicationController
# GET /school_classes
# GET /school_classes.json
def index
@school_classes = SchoolClass.all.limit(30)
if params[:search]
# filter relevant parameters
@search_params = school_class_params.symbolize_keys
@avg_enrollments = apply_search_filters(@search_params)
end
end
# GET /school_classes/1
......@@ -69,6 +73,32 @@ class SchoolClassesController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def school_class_params
params.require(:school_class).permit(:census_year, :class_code, :num_enrollments, :location_id)
params.require(:school_class)
.permit(:search, :state_id, :city_id, :region_id, :census_year_start, :census_year_end,
:administrative_level_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
......@@ -5,4 +5,8 @@ class City < ActiveRecord::Base
validates_presence_of :city_code,
:name
validates_uniqueness_of :city_code
def self.ordered
self.order("name ASC")
end
end
......@@ -3,4 +3,8 @@ class Region < ActiveRecord::Base
has_many :cities
# validations
validates_presence_of :name
def self.ordered
self.order("name ASC")
end
end
......@@ -18,4 +18,16 @@ class SchoolClass < ActiveRecord::Base
validates_numericality_of :census_year,
only_integer: true,
greater_than: 0
def self.start_year
minimum(:census_year)
end
def self.end_year
maximum(:census_year)
end
def search(params)
end
end
......@@ -5,4 +5,8 @@ class State < ActiveRecord::Base
# validations
validates_presence_of :name,
:region
def self.ordered
self.order("name ASC")
end
end
<p id="notice"><%= notice %></p>
<%= @search_params unless @search_params.nil? %>
<h1><%= SchoolClass.model_name.human.pluralize %></h1>
<fieldset>
<legend>Recorte Espacial</legend>
</fieldset>
<fieldset>
<legend>Recorte Temporal</legend>
</fieldset>
<fieldset>
<legend>Configurações de Visualização</legend>
</fieldset>
<table>
<thead>
<tr>
<th><%= SchoolClass.human_attribute_name(:census_year) %></th>
<th><%= SchoolClass.human_attribute_name(:num_enrollments) %></th>
<th><%= SchoolClass.human_attribute_name(:city_id) %></th>
<th><%= SchoolClass.human_attribute_name(:location_id) %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @school_classes.each do |school_class| %>
<tr>
<td><%= school_class.census_year %></td>
<td><%= school_class.num_enrollments %></td>
<td><%= school_class.city.name %></td>
<td><%= school_class.location.name %></td>
<td><%= link_to t(:show), school_class %></td>
<td><%= link_to t(:edit), edit_school_class_path(school_class) %></td>
<td><%= link_to t(:delete), school_class, method: :delete, data: { confirm: t(:delete_confirm) } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= form_tag(school_classes_path, :method => "get", id: "search-form") do %>
<fieldset>
<legend><%= t :location_filter %></legend>
<p</p>
<div class="field">
<%= State.model_name.human %>:<br />
<%= select :school_class, :state_id, State.all.ordered.collect { |p| [ p.name, p.id ] }, prompt: "Nenhum estado selecionado" %>
</div>
<div class="field">
<%= City.model_name.human %>:<br />
<%= select :school_class, :city_id, City.all.ordered.collect { |p| [ p.name, p.id ] }, prompt: "Nenhuma cidade selecionada" %>
</div>
<div class="field">
<%= Region.model_name.human %>:<br />
<%= select :school_class, :region_id, Region.all.ordered.collect { |p| [ p.name, p.id ] }, prompt: "Nenhuma região selecionada" %>
</div>
</fieldset>
<fieldset>
<legend><%= t :time_filter %></legend>
Selecione o intervalo de tempo:<br />
<div class="field">
Início: <br />
<%= select :school_class, :census_year_start, Range.new(SchoolClass.start_year, SchoolClass.end_year).to_a, default: SchoolClass.end_year %>
</div>
<div class="field">
Fim: <br />
<%= select :school_class, :census_year_end, Range.new(SchoolClass.start_year, SchoolClass.end_year).to_a, default: SchoolClass.end_year %>
</div>
</fieldset>
<fieldset>
<legend>Variáveis</legend>
<div class="field">
<%= AdministrativeLevel.model_name.human %>:<br />
<%= select :school_class, :administrative_level_id, AdministrativeLevel.all.collect { |p| [ p.name, p.id ] }, prompt: "Nenhuma dependência administrativa selecionada" %>
</div>
</fieldset>
<fieldset>
<legend>Opções</legend>
<%= submit_tag t(:search), name: "search" %>
</fieldset>
<% end %>
<%= @avg_enrollments unless @avg_enrollments.nil? %><br>
<%= link_to "#{t(:new)} #{SchoolClass.model_name.human}", new_school_class_path %>
......@@ -4,6 +4,9 @@ pt_BR:
models:
school_class: "Turma"
location: "Local"
state: "Estado"
city: "Cidade"
region: "Região"
attributes:
city:
name: "Nome"
......@@ -230,10 +233,12 @@ pt_BR:
short: "%d de %B, %H:%M"
pm: ''
search: "Filtrar"
new: "Adicionar"
show: "Mostrar"
edit: "Editar"
delete: "Apagar"
back: "Voltar"
delete_confirm: "Tem certeza que deseja apagar?"
location_filter: "Recorte Espacial"
time_filter: "Recorte Temporal"
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