Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/controllers/classroom_rosters_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ClassroomRostersController < ApplicationController
allow_unauthenticated_access

def show
@classroom = Classroom.includes(:students).find_by!(uuid: params.expect(:uuid))
end
end
30 changes: 30 additions & 0 deletions app/controllers/classrooms_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class ClassroomsController < ApplicationController
before_action :set_classroom, only: %i[ edit update ]

# GET /classrooms/1/edit
def edit
end
# PATCH/PUT /classrooms/1 or /classrooms/1.json
def update
respond_to do |format|
if @classroom.update(classroom_params)
format.html { redirect_to school_students_url(@classroom.school), notice: "Classroom was successfully updated.", status: :see_other }
format.json { render :show, status: :ok, location: @classroom }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @classroom.errors, status: :unprocessable_entity }
end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_classroom
@classroom = Classroom.find(params.expect(:id))
end

# Only allow a list of trusted parameters through.
def classroom_params
params.expect(classroom: [ :name, :teacher ])
end
end
5 changes: 3 additions & 2 deletions app/controllers/students_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class StudentsController < ApplicationController

# GET /students or /students.json
def index
@students = @school.students.all
@students = @school.students.includes(:classroom).all
end

# GET /students/1 or /students/1.json
Expand All @@ -18,6 +18,7 @@ def new

# GET /students/1/edit
def edit
@classrooms = @student.school.classrooms.all
end

# POST /students or /students.json
Expand Down Expand Up @@ -70,6 +71,6 @@ def set_school

# Only allow a list of trusted parameters through.
def student_params
params.expect(student: [ :first_name, :last_name, :email, :grade_level, :gender ])
params.expect(student: [ :first_name, :last_name, :email, :grade_level, :gender, :classroom_id ])
end
end
2 changes: 0 additions & 2 deletions app/helpers/schools_helper.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/classroom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Classroom < ApplicationRecord
belongs_to :school
has_many :students
end
1 change: 1 addition & 0 deletions app/models/school.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class School < ApplicationRecord
has_many :students, dependent: :destroy
has_many :classrooms, dependent: :destroy
validates :name, presence: true
end
3 changes: 3 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class Student < ApplicationRecord
belongs_to :school
belongs_to :classroom

enum :gender, %i[male female other].index_by(&:itself)

validates :first_name, :last_name, :grade_level, presence: true

def full_name = [ first_name, last_name ].join(" ")
end
14 changes: 14 additions & 0 deletions app/views/classroom_rosters/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<header class="flex items-center justify-between mb-8">
<h1 class="text-4xl"><%= @classroom.name %></h1>
</header>
<ul class="list bg-base-100 rounded-box shadow-md">
<% @classroom.students.sort_by(&:full_name).each do |student| %>
<%= link_to "#" do %>
<li class="list-row link link-hover">
<span class="text-2xl">
<%= student.full_name %>
</span>
</li>
<% end %>
<% end %>
</ul>
14 changes: 14 additions & 0 deletions app/views/classrooms/_classroom.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div id="<%= dom_id classroom %>" class="w-full sm:w-auto my-5 space-y-5">
<div>
<strong class="block font-medium mb-1">Name:</strong>
<%= classroom.name %>
</div>
<div>
<strong class="block font-medium mb-1">School:</strong>
<%= classroom.school_id %>
</div>
<div>
<strong class="block font-medium mb-1">Teacher:</strong>
<%= classroom.teacher %>
</div>
</div>
26 changes: 26 additions & 0 deletions app/views/classrooms/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= form_with(model: classroom) do |form| %>
<div class="flex flex-col gap-2">
<% if classroom.errors.any? %>
<div style="color: red">
<h2><%= pluralize(classroom.errors.count, "error") %> prohibited this classroom from being saved:</h2>

<ul>
<% classroom.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form.label :name, class: "label" %>
<%= form.text_field :name, class: "input w-full" %>

<%= form.label :teacher, class: "label" %>
<%= form.text_field :teacher, class: "input w-full" %>

<%= form.label :Link, class: "label" %>
<%= link_to classroom_roster_url(classroom.uuid), classroom_roster_path(classroom.uuid), class: 'link' %>

<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/classrooms/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<% content_for :title, "Editing classroom" %>
<nav class="breadcrumbs text-sm">
<ul>
<li><%= link_to "All Schools", schools_path %></li>
<li><%= link_to @classroom.school.name, school_path(@classroom.school) %></li>
<li>Edit Classroom</li>
</ul>
</nav>
<section class="py-10 flex items-center justify-center">
<div class="card card-border bg-base-100 shadow-md w-96">
<div class="card-body">
<h1 class="card-title">Editing Classroom</h1>
<%= render "form", classroom: @classroom %>
</div>
</div>
</section>
29 changes: 29 additions & 0 deletions app/views/classrooms/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% content_for :title, "Classrooms" %>

<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-md inline-block" id="notice"><%= notice %></p>
<% end %>

<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Classrooms</h1>
<%= link_to "New classroom", new_classroom_path, class: "rounded-md px-3.5 py-2.5 bg-blue-600 hover:bg-blue-500 text-white block font-medium" %>
</div>

<div id="classrooms" class="min-w-full divide-y divide-gray-200 space-y-5">
<% if @classrooms.any? %>
<% @classrooms.each do |classroom| %>
<div class="flex flex-col sm:flex-row justify-between items-center pb-5 sm:pb-0">
<%= render classroom %>
<div class="w-full sm:w-auto flex flex-col sm:flex-row space-x-2 space-y-2">
<%= link_to "Show", classroom, class: "w-full sm:w-auto text-center rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium" %>
<%= link_to "Edit", edit_classroom_path(classroom), class: "w-full sm:w-auto text-center rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium" %>
<%= button_to "Destroy", classroom, method: :delete, class: "w-full sm:w-auto rounded-md px-3.5 py-2.5 text-white bg-red-600 hover:bg-red-500 font-medium cursor-pointer", data: { turbo_confirm: "Are you sure?" } %>
</div>
</div>
<% end %>
<% else %>
<p class="text-center my-10">No classrooms found.</p>
<% end %>
</div>
</div>
2 changes: 2 additions & 0 deletions app/views/students/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<%= form.label :gender, class: "label" %>
<%= form.text_field :gender, class: "input w-full" %>

<%= form.collection_select :classroom_id, classrooms, :id, :name, { include_blank: "Select a classroom" }, class: "select w-full" %>

<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/students/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="card card-border bg-base-100 shadow-md w-96">
<div class="card-body">
<h1 class="card-title">Editing Student</h1>
<%= render "form", student: @student %>
<%= render "form", student: @student, classrooms: @classrooms %>
</div>
</div>
</section>
2 changes: 2 additions & 0 deletions app/views/students/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<th>Email</th>
<th>Grade level</th>
<th>Gender</th>
<th>Classroom</th>
<th>Actions</th>
</tr>
</thead>
Expand All @@ -34,6 +35,7 @@
<td><%= student.email %></td>
<td><%= student.grade_level %></td>
<td><%= student.gender %></td>
<td><%= link_to student.classroom.name, edit_classroom_path(student.classroom), class: "btn btn-link" %></td>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_student_path(student), class: "btn btn-primary" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/students/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="card card-border bg-base-100 shadow-md w-96">
<div class="card-body">
<h1 class="card-title">New Student</h1>
<%= render "form", student: @student %>
<%= render "form", student: @student, classrooms: @school.classrooms %>
</div>
</div>
</section>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Rails.application.routes.draw do
get "classroom_rosters/show"
resource :session
resources :passwords, param: :token
resources :classroom_rosters, only: %i[show], param: :uuid
scope :admin do
resources :schools do
resources :students, shallow: true
resources :classrooms, shallow: true, only: %i[edit update]
end
end
root to: "schools#index"
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20260428233313_create_classrooms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateClassrooms < ActiveRecord::Migration[8.1]
def change
create_table :classrooms do |t|
t.string :name
t.belongs_to :school, null: false, foreign_key: true
t.string :teacher
t.string :uuid, null: false, index: { unique: true }

t.timestamps
end

change_table :students do |t|
t.belongs_to :classroom, null: false, foreign_key: true
end
end
end
17 changes: 16 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def build_student_attrs(overrides = {})
# randomly sample 3 grade levels and create 10 students for each grade level
grades = (1..12).to_a.sample(3)
grades.each do |grade|
students = 10.times.map { build_student_attrs(grade_level: grade) }
classrooms = 2.times.map do |i|
school.classrooms.create!(name: "Classroom #{ i + 1 }", teacher: maybe { Faker::Name.name }, uuid: SecureRandom.urlsafe_base64(32))
end
students = 10.times.map { |i| build_student_attrs(grade_level: grade, classroom_id: classrooms[i % 2].id) }
school.students.create!(students)
end
9 changes: 9 additions & 0 deletions test/controllers/classroom_rosters_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

class ClassroomRostersControllerTest < ActionDispatch::IntegrationTest
test "should get show without signing in" do
classroom = classrooms(:one)
get classroom_roster_url(classroom.uuid)
assert_response :success
end
end
19 changes: 19 additions & 0 deletions test/controllers/classrooms_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

class ClassroomsControllerTest < ActionDispatch::IntegrationTest
setup do
@classroom = classrooms(:one)
sign_in_as users(:admin)
end

test "should get edit" do
get edit_classroom_url(@classroom)
assert_response :success
end

test "should update classroom" do
sign_in_as users(:admin)
patch classroom_url(@classroom), params: { classroom: { name: @classroom.name, teacher: @classroom.teacher } }
assert_redirected_to school_students_url(@classroom.school)
end
end
11 changes: 10 additions & 1 deletion test/controllers/students_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class StudentsControllerTest < ActionDispatch::IntegrationTest

test "should create student" do
assert_difference("Student.count") do
post school_students_url(@school), params: { student: { email: @student.email, first_name: @student.first_name, gender: @student.gender, grade_level: @student.grade_level, last_name: @student.last_name, school_id: @student.school_id } }
post school_students_url(@school), params: { student: { email: @student.email, first_name: @student.first_name, gender: @student.gender, grade_level: @student.grade_level, last_name: @student.last_name, classroom_id: @student.classroom_id } }
end

assert_redirected_to student_url(Student.last)
Expand All @@ -40,6 +40,15 @@ class StudentsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to student_url(@student)
end

test "can update a student's classroom" do
classroom = @student.classroom.dup
classroom.update! uuid: SecureRandom.urlsafe_base64(32), name: "New Classroom"
assert_changes -> { @student.reload.classroom_id } do
patch student_url(@student), params: { student: { classroom_id: classroom.id } }
assert_redirected_to student_url(@student)
end
end

test "should destroy student" do
assert_difference("Student.count", -1) do
delete student_url(@student)
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/classrooms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: Classroom 1
school: one
teacher: Teacher 1
uuid: abcd-1234-efgh-5678

two:
name: Classroom 2
school: two
teacher:
uuid: wxyz-9876-ijkl-5432
Loading