Skip to content

Commit

Permalink
Fixed TimeTable View Filtering Bug (#37)
Browse files Browse the repository at this point in the history
* fixed timeTable View Filtering Bug

* fixed header

* added rspec tests and ran rubocop
  • Loading branch information
pavitgopal authored Oct 24, 2024
1 parent 0e047d2 commit 93271b8
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 63 deletions.
10 changes: 10 additions & 0 deletions app/controllers/time_slots_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# frozen_string_literal: true

class TimeSlotsController < ApplicationController
def filter
@schedule = Schedule.find(params[:schedule_id])

@time_slots = TimeSlot.all
@time_slots = @time_slots.where(day: params[:day]) if params[:day].present?
@time_slots = @time_slots.where(slot_type: params[:slot_type]) if params[:slot_type].present?

redirect_to schedule_time_slots_path(@schedule, day: params[:day], slot_type: params[:slot_type])
end

def index
@time_slots = TimeSlot.all
@time_slots = @time_slots.where(day: params[:day]) if params[:day].present?
Expand Down
25 changes: 13 additions & 12 deletions app/views/time_slots/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<%= render_navbar("time-slots", @schedule) %>
<%= form_with url: time_slots_path, method: :get, local: true do %>
<div class="timeTable-filters">
<div>
<%= label_tag :day, 'Filter by Day' %>
<%= select_tag :day, options_for_select(['MWF', 'MW', 'TR', 'F'], params[:day]), include_blank: 'All Days', class: 'timeTable-select' %>
</div>
<div>
<%= label_tag :slot_type, 'Filter by Type' %>
<%= select_tag :slot_type, options_for_select(['LEC', 'LAB'], params[:slot_type]), include_blank: 'All Types', class: 'timeTable-select' %>
</div>
<%= submit_tag 'Filter', class: 'timeTable-btn' %>
<%= form_with url: schedule_time_slots_path(@schedule), method: :post, local: true do %>
<div class="timeTable-filters">
<div>
<%= label_tag :day, 'Filter by Day' %>
<%= select_tag :day, options_for_select(['MWF', 'MW', 'TR', 'F'], params[:day]), include_blank: 'All Days', class: 'timeTable-select' %>
</div>
<% end %>
<div>
<%= label_tag :slot_type, 'Filter by Type' %>
<%= select_tag :slot_type, options_for_select(['LEC', 'LAB'], params[:slot_type]), include_blank: 'All Types', class: 'timeTable-select' %>
</div>
<%= submit_tag 'Filter', class: 'timeTable-btn' %>
</div>
<% end %>


<div class="timeTable-table-wrapper">
<table class="table timeTable-table">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

resources :instructors, only: [:index]
post :upload_instructors, on: :member
post 'time_slots', to: 'time_slots#filter', as: 'filter_time_slots'
get '/time_slots', to: 'time_slots#index'
end

Expand Down
104 changes: 53 additions & 51 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -10,65 +12,65 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_10_18_191419) do
create_table "instructors", force: :cascade do |t|
t.integer "id_number"
t.string "last_name"
t.string "first_name"
t.string "middle_name"
t.string "email"
t.boolean "before_9"
t.boolean "after_3"
t.text "beaware_of"
t.integer "schedule_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["schedule_id"], name: "index_instructors_on_schedule_id"
ActiveRecord::Schema[7.2].define(version: 20_241_018_191_419) do
create_table 'instructors', force: :cascade do |t|
t.integer 'id_number'
t.string 'last_name'
t.string 'first_name'
t.string 'middle_name'
t.string 'email'
t.boolean 'before_9'
t.boolean 'after_3'
t.text 'beaware_of'
t.integer 'schedule_id'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['schedule_id'], name: 'index_instructors_on_schedule_id'
end

create_table "rooms", force: :cascade do |t|
t.integer "campus"
t.boolean "is_lecture_hall"
t.boolean "is_learning_studio"
t.boolean "is_lab"
t.string "building_code"
t.string "room_number"
t.integer "capacity"
t.boolean "is_active"
t.string "comments"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "schedule_id", default: -1, null: false
t.index ["schedule_id"], name: "index_rooms_on_schedule_id"
create_table 'rooms', force: :cascade do |t|
t.integer 'campus'
t.boolean 'is_lecture_hall'
t.boolean 'is_learning_studio'
t.boolean 'is_lab'
t.string 'building_code'
t.string 'room_number'
t.integer 'capacity'
t.boolean 'is_active'
t.string 'comments'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.integer 'schedule_id', default: -1, null: false
t.index ['schedule_id'], name: 'index_rooms_on_schedule_id'
end

create_table "schedules", force: :cascade do |t|
t.string "schedule_name"
t.string "semester_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'schedules', force: :cascade do |t|
t.string 'schedule_name'
t.string 'semester_name'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

create_table "time_slots", force: :cascade do |t|
t.string "day"
t.string "start_time"
t.string "end_time"
t.string "slot_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'time_slots', force: :cascade do |t|
t.string 'day'
t.string 'start_time'
t.string 'end_time'
t.string 'slot_type'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

create_table "users", force: :cascade do |t|
t.string "email"
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "uid"
t.string "provider"
t.index ["email"], name: "index_users_on_email", unique: true
create_table 'users', force: :cascade do |t|
t.string 'email'
t.string 'first_name'
t.string 'last_name'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.string 'uid'
t.string 'provider'
t.index ['email'], name: 'index_users_on_email', unique: true
end

add_foreign_key "instructors", "schedules"
add_foreign_key "rooms", "schedules"
add_foreign_key 'instructors', 'schedules'
add_foreign_key 'rooms', 'schedules'
end
33 changes: 33 additions & 0 deletions spec/controllers/time_slot_controllers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
session[:user_id] = @user.id
end

let!(:schedule) { create(:schedule) }
let!(:lec_slot) { TimeSlot.create!(day: 'MWF', start_time: '08:00', end_time: '08:50', slot_type: 'LEC') }
let!(:lab_slot) { TimeSlot.create!(day: 'TR', start_time: '09:35', end_time: '10:50', slot_type: 'LAB') }

Expand Down Expand Up @@ -39,4 +40,36 @@
expect(assigns(:time_slots)).to eq([lab_slot])
end
end

describe 'GET filter' do
it 'redirects to time_slots_path with no filters applied' do
get :filter, params: { schedule_id: schedule.id }
expect(assigns(:time_slots)).to match_array([lec_slot, lab_slot])
expect(response).to redirect_to schedule_time_slots_path(schedule)
end

it 'filters time slots by day' do
get :filter, params: { schedule_id: schedule.id, day: 'MWF' }
expect(assigns(:time_slots)).to eq([lec_slot])
expect(response).to redirect_to schedule_time_slots_path(schedule, day: 'MWF')
end

it 'filters time slots by slot_type' do
get :filter, params: { schedule_id: schedule.id, slot_type: 'LAB' }
expect(assigns(:time_slots)).to eq([lab_slot])
expect(response).to redirect_to schedule_time_slots_path(schedule, slot_type: 'LAB')
end

it 'filters time slots by day and slot_type' do
get :filter, params: { schedule_id: schedule.id, day: 'TR', slot_type: 'LAB' }
expect(assigns(:time_slots)).to eq([lab_slot])
expect(response).to redirect_to schedule_time_slots_path(schedule, day: 'TR', slot_type: 'LAB')
end

it 'returns an empty array when no time slots match the filters' do
get :filter, params: { schedule_id: schedule.id, day: 'Saturday' }
expect(assigns(:time_slots)).to be_empty
expect(response).to redirect_to schedule_time_slots_path(schedule, day: 'Saturday')
end
end
end

0 comments on commit 93271b8

Please sign in to comment.