diff --git a/app/controllers/time_slots_controller.rb b/app/controllers/time_slots_controller.rb index bcb3564..5f19249 100644 --- a/app/controllers/time_slots_controller.rb +++ b/app/controllers/time_slots_controller.rb @@ -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? diff --git a/app/views/time_slots/index.html.erb b/app/views/time_slots/index.html.erb index daafebf..35f19cb 100644 --- a/app/views/time_slots/index.html.erb +++ b/app/views/time_slots/index.html.erb @@ -1,18 +1,19 @@ <%= render_navbar("time-slots", @schedule) %> -<%= form_with url: time_slots_path, method: :get, local: true do %> -
-
- <%= 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' %> -
-
- <%= 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' %> -
- <%= submit_tag 'Filter', class: 'timeTable-btn' %> +<%= form_with url: schedule_time_slots_path(@schedule), method: :post, local: true do %> +
+
+ <%= 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' %>
- <% end %> +
+ <%= 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' %> +
+ <%= submit_tag 'Filter', class: 'timeTable-btn' %> +
+<% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 9c6bf50..b0789da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 735c833..44e9d84 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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. @@ -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 diff --git a/spec/controllers/time_slot_controllers_spec.rb b/spec/controllers/time_slot_controllers_spec.rb index f8fcabd..eb8448b 100644 --- a/spec/controllers/time_slot_controllers_spec.rb +++ b/spec/controllers/time_slot_controllers_spec.rb @@ -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') } @@ -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