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
2 changes: 1 addition & 1 deletion app/controllers/student_homes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
.order("programs.name")
@active_program = @classroom_programs.find { |cp| cp.id.to_s == params[:classroom_program_id] } || @classroom_programs.first
@published_modules = if @active_program
@active_program.classroom_modules.published.includes(content_module: :links)
@active_program.classroom_modules.published.includes(content_module: { contents: :contentable })
else
[]
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/student_homes/_content.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%# locals: (content:) %>
<li>
<%= render "student_homes/contentables/#{content.contentable_name}", contentable: content.contentable %>
</li>
2 changes: 2 additions & 0 deletions app/views/student_homes/contentables/_game.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%# locals: (contentable:) %>
<%= link_to contentable.title, play_game_path(contentable), class: "link inline-flex items-center gap-1" %>
3 changes: 3 additions & 0 deletions app/views/student_homes/contentables/_link.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%# locals: (contentable:) %>
<%= link_to "#{contentable.title} ↗", safe_external_url(contentable.url),
target: "_blank", rel: "noopener noreferrer", class: "link inline-flex items-center gap-1" %>
10 changes: 1 addition & 9 deletions app/views/student_homes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@
</summary>
<div class="collapse-content">
<ul class="flex flex-col gap-2 pt-2">
<% classroom_module.content_module.links.each do |link| %>
<li>
<%= tag.a link.title + " ↗",
href: safe_external_url(link.url),
target: "_blank",
rel: "noopener noreferrer",
class: "link inline-flex items-center gap-1" %>
</li>
<% end %>
<%= render partial: "student_homes/content", collection: classroom_module.content_module.contents %>
</ul>
</div>
<% end %>
Expand Down
86 changes: 57 additions & 29 deletions test/controllers/student_homes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

class StudentHomesControllerTest < ActionDispatch::IntegrationTest
setup do
# A fresh classroom has no enrollments, so each test builds the ones it needs.
@classroom = Classroom.create!(name: "Test Classroom", school: schools(:one))
@student = students(:one)
@student.update!(classroom: @classroom)
student_sign_in_as @student
end

Expand All @@ -12,71 +15,96 @@ class StudentHomesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to new_student_session_url
end

test "shows published module names" do
test "shows published module names only" do
enrollment = enroll(program: programs(:one))
publish_module(enrollment, name: "Intro")
publish_module(enrollment, name: "Scheduled", publish_on: 1.day.from_now)

get student_homes_url
assert_response :success
assert_select "summary", text: /Introduction to Health/
assert_select "summary", text: /Intro/
assert_select "summary", text: /Scheduled/, count: 0
end

test "shows links inside published modules" do
test "shows contents in position order" do
content_module = publish_module(enroll(program: programs(:one))).content_module
content_module.contents.create!(contentable: games(:one))
link_content = content_module.contents.create!(contentable: links(:one))

get student_homes_url
assert_response :success
link = content_modules(:one).links.first
assert_select "a[target='_blank']", text: /#{link.title}/ if link
assert_equal [ games(:one).title, links(:one).title ], listed_titles

link_content.move(:up)

get student_homes_url
assert_equal [ links(:one).title, games(:one).title ], listed_titles
end

test "most recently published module has the open attribute" do
# Add a second published module so there's a distinct "most recent"
second_module = ContentModule.create!(
program: programs(:one), level: "basic", name: "Second Module", position: 2
)
classroom_programs(:one).classroom_modules.create!(
content_module: second_module, publish_on: Date.current
)
enrollment = enroll(program: programs(:one))
publish_module(enrollment, name: "Older Module", publish_on: 1.week.ago.to_date)
publish_module(enrollment, name: "Newer Module", publish_on: Date.current)

get student_homes_url
assert_select "details[open] summary", text: /Second Module/
assert_select "details[open]", count: 1
assert_select "details[open] summary", text: /Newer Module/
end

test "all modules published on the same latest date have the open attribute" do
second_module = ContentModule.create!(
program: programs(:one), level: "basic", name: "Second Module", position: 2
)
classroom_modules(:one).update!(publish_on: Date.current)
classroom_programs(:one).classroom_modules.create!(
content_module: second_module, publish_on: Date.current
)
enrollment = enroll(program: programs(:one))
publish_module(enrollment, name: "First Module", publish_on: Date.current)
publish_module(enrollment, name: "Second Module", publish_on: Date.current)

get student_homes_url
assert_select "details[open]", count: 2
end

test "shows empty state when no modules are published" do
classroom_modules(:one).update!(publish_on: nil)
enrollment = enroll(program: programs(:one))
publish_module(enrollment, publish_on: nil)

get student_homes_url
assert_select "p", text: /No modules published yet/
end

test "does not show tab bar for single program enrollment" do
enroll(program: programs(:one))

get student_homes_url
assert_select "[role='tablist']", count: 0
end

test "shows tab bar when classroom has multiple program enrollments" do
classroom_programs(:one).classroom.classroom_programs.create!(
program: programs(:two), level: "moderate"
)
enroll(program: programs(:one))
enroll(program: programs(:two))

get student_homes_url
assert_select "[role='tablist']"
assert_select "a[role='tab']", minimum: 2
end

test "tab switching shows the selected program as active" do
enrollment = classroom_programs(:one)
enrollment.classroom.classroom_programs.create!(program: programs(:two), level: "moderate")
enroll(program: programs(:one))
second_enrollment = enroll(program: programs(:two))

get student_homes_url(classroom_program_id: second_enrollment.id)
assert_select "a[role='tab'][aria-selected='true'][href*='classroom_program_id=#{second_enrollment.id}']"
end

private

def enroll(program:, level: "basic")
@classroom.classroom_programs.create!(program: program, level: level)
end

# Creates a module in the enrollment's program and level, and schedules it for the classroom.
def publish_module(enrollment, name: "Module", publish_on: Date.current)
content_module = ContentModule.create!(program: enrollment.program, level: enrollment.level, name: name)
enrollment.classroom_modules.create!(content_module: content_module, publish_on: publish_on)
end

get student_homes_url(classroom_program_id: enrollment.id)
assert_select "a[role='tab'][aria-selected='true'][href*='classroom_program_id=#{enrollment.id}']"
# The " ↗" suffix marks external links, so strip it to compare plain titles.
def listed_titles
css_select("details li a").map { |a| a.text.delete_suffix(" ↗") }
end
end
Loading