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
3 changes: 2 additions & 1 deletion app/controllers/admin/content_modules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
@active_program = @programs.find { |p| p.id.to_s == params[:program_id] } || @programs.first
@modules_by_level = @active_program
.content_modules.ordered
.includes(:links)
.includes(:contents)
.group_by(&:level)
end

Expand All @@ -25,6 +25,7 @@ def create
end

def edit
@contents = @content_module.contents.includes(:contentable)
end

def update
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/admin/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Admin::ContentsController < AdminController
before_action :set_content, only: %i[destroy]

def destroy
@content.destroy!
redirect_to edit_admin_content_module_path(@content.content_module), notice: "Content was successfully removed.", status: :see_other
end

private
def set_content
@content = Content.find(params.expect(:id))
end
end
8 changes: 1 addition & 7 deletions app/controllers/admin/links_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Admin::LinksController < AdminController
before_action :set_content_module, only: %i[new create]
before_action :set_link, only: %i[edit update destroy]
before_action :set_link, only: %i[edit update]

def new
@link = Link.new
Expand Down Expand Up @@ -29,12 +29,6 @@ def update
end
end

def destroy
content_module = @link.content_module
@link.destroy!
redirect_to edit_admin_content_module_path(content_module), notice: "Link was successfully deleted.", status: :see_other
end

private
def set_content_module
@content_module = ContentModule.find(params.expect(:content_module_id))
Expand Down
13 changes: 13 additions & 0 deletions app/views/admin/content_modules/_content_module.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%# locals: (content_module:) %>
<%= tag.tr id: dom_id(content_module) do %>
<td><%= content_module.name %></td>
<td><%= content_module.contents.size %></td>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_admin_content_module_path(content_module), class: "btn btn-sm btn-primary" %>
<%= button_to "Delete", admin_content_module_path(content_module), method: :delete,
data: { "turbo-confirm": "Delete this module?" },
class: "btn btn-sm btn-error" %>
</div>
</td>
<% end %>
65 changes: 11 additions & 54 deletions app/views/admin/content_modules/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<li>Edit Module</li>
</ul>
</nav>
<!--TODO: Add better styling for alerts/notices -->
<p style="color: green"><%= notice %></p>
Comment thread
sean-dickinson marked this conversation as resolved.
Resolved
<p style="color: red"><%= alert %></p>
<section class="py-10 flex justify-center">
<div class="flex flex-col gap-6 w-full max-w-lg">
<div class="card card-border bg-base-100 shadow-md">
Expand All @@ -17,11 +20,13 @@

<div class="card card-border bg-base-100 shadow-md">
<div class="card-body">
<div class="flex justify-between items-center mb-2">
<h2 class="card-title text-lg">Links</h2>
<%= link_to "Add link", new_admin_content_module_link_path(@content_module), class: "btn btn-sm btn-primary" %>
<div class="flex justify-between items-center mb-2 gap-2 flex-wrap">
<h2 class="card-title text-lg">Contents</h2>
<div class="flex items-center gap-2">
<%= link_to "Add link", new_admin_content_module_link_path(@content_module), class: "btn btn-sm btn-primary" %>
</div>
</div>
<% if @content_module.links.any? %>
<% if @contents.any? %>
<table class="table">
<thead>
<tr>
Expand All @@ -31,59 +36,11 @@
</tr>
</thead>
<tbody>
<% @content_module.links.each do |link| %>
<tr>
<td><a href="<%= safe_external_url(link.url) %>" target="_blank" class="link inline-flex items-center gap-1"><%= link.title %> ↗</a></td>
<td><%= link.link_type.humanize %></td>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_admin_link_path(link), class: "btn btn-xs btn-primary" %>
<%= button_to "Delete", admin_link_path(link), method: :delete,
data: { "turbo-confirm": "Delete this link?" },
class: "btn btn-xs btn-error" %>
</div>
</td>
</tr>
<% end %>
<%= render @contents %>
</tbody>
</table>
<% else %>
<p class="text-sm opacity-60">No links yet.</p>
<% end %>
</div>
</div>

<div class="card card-border bg-base-100 shadow-md">
<div class="card-body">
<div class="flex justify-between items-center mb-2">
<h2 class="card-title text-lg">Games</h2>
</div>
<% if @content_module.games.any? %>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Actions</th> <!-- TODO: Flex this to the end of the full width of the table -->
</tr>
</thead>
<tbody>
<% @content_module.games.each do |game| %>
<tr>
<td><%= game.title %></td>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_admin_game_path(game), class: "btn btn-xs btn-primary" %> <!-- TODO: Flex this to the end of the full width of the table -->
<%= button_to "Delete", admin_game_path(game), method: :delete,
data: { "turbo-confirm": "Delete this game?" },
class: "btn btn-xs btn-error" %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p class="text-sm opacity-60">No games yet.</p>
<p class="text-sm opacity-60">No contents yet.</p>
<% end %>
</div>
</div>
Expand Down
17 changes: 2 additions & 15 deletions app/views/admin/content_modules/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,12 @@
<thead>
<tr>
<th>Name</th>
<th>Links</th>
<th>Contents</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% modules.each do |mod| %>
<tr>
<td><%= mod.name %></td>
<td><%= mod.links.size %></td>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_admin_content_module_path(mod), class: "btn btn-sm btn-primary" %>
<%= button_to "Delete", admin_content_module_path(mod), method: :delete,
data: { "turbo-confirm": "Delete this module?" },
class: "btn btn-sm btn-error" %>
</div>
</td>
</tr>
<% end %>
<%= render partial: "admin/content_modules/content_module", collection: modules %>
</tbody>
</table>
</div>
Expand Down
19 changes: 19 additions & 0 deletions app/views/admin/contents/_content.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%# locals: (content:) %>
<% contentable = content.contentable %>
<%= tag.tr id: dom_id(content) do %>
<%= render "admin/contents/contentables/#{content.contentable_name}", contentable: contentable %>
<td>
<div class="flex gap-2">
<%= link_to "Edit", edit_polymorphic_path([:admin, contentable]), class: "btn btn-xs btn-primary" %>
<% if contentable.owned_by_content? %>
<%= button_to "Delete", admin_content_path(content), method: :delete,
data: { "turbo-confirm": "Delete this link? It will be removed from the module and deleted." },
class: "btn btn-xs btn-error" %>
<% else %>
<%= button_to "Remove", admin_content_path(content), method: :delete,
data: { "turbo-confirm": "Remove this game from the module? The game itself is kept." },
class: "btn btn-xs btn-error" %>
<% end %>
</div>
</td>
<% end %>
3 changes: 3 additions & 0 deletions app/views/admin/contents/contentables/_game.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%# locals: (contentable:) %>
<td><%= contentable.title %></td>
<td>Game</td>
3 changes: 3 additions & 0 deletions app/views/admin/contents/contentables/_link.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%# locals: (contentable:) %>
<td><%= link_to "#{contentable.title} ↗", safe_external_url(contentable.url), target: "_blank", rel: "noopener noreferrer", class: "link inline-flex items-center gap-1" %></td>
<td><%= contentable.link_type.humanize %></td>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
post :csv_import, to: "csv#import"
end
resources :content_modules, except: [ :show ] do
resources :links, shallow: true, except: %i[index show]
resources :links, shallow: true, except: %i[index show destroy]
resources :contents, shallow: true, only: :destroy
end
resources :classroom_modules, only: %i[update]
end
Expand Down
31 changes: 15 additions & 16 deletions test/controllers/admin/content_modules_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ class Admin::ContentModulesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to admin_content_modules_url
end

test "create appends the module and ignores a submitted position" do
ContentModule.create!(program: programs(:one), level: "moderate", name: "Existing")

post admin_content_modules_url, params: {
content_module: { name: "New Module", program_id: programs(:one).id, level: "moderate", position: 99 }
}

assert_equal 2, ContentModule.find_by!(name: "New Module").position
end

test "should not create with missing name" do
assert_no_difference "ContentModule.count" do
post admin_content_modules_url, params: {
Expand All @@ -57,15 +47,24 @@ class Admin::ContentModulesControllerTest < ActionDispatch::IntegrationTest

test "edit lists links ordered by position" do
content_module = ContentModule.create!(program: programs(:one), level: "basic", name: "Ordered Module")
last, first = [
content_module.contents.create!(contentable: Link.new(title: "Last Link", url: "https://example.com/2", link_type: "survey",), position: 2),
content_module.contents.create!(contentable: Link.new(title: "First Link", url: "https://example.com/1", link_type: "survey"), position: 1)
]
last = content_module.contents.create!(contentable: Link.new(title: "Last Link", url: "https://example.com/2", link_type: "survey"))
first = content_module.contents.create!(contentable: Link.new(title: "First Link", url: "https://example.com/1", link_type: "survey"))
first.move(:up)

get edit_admin_content_module_url(content_module)

hrefs = css_select("table tbody tr td:first-child a").map { |link| link["href"] }
assert_equal [ last.contentable.url, first.contentable.url ], hrefs
row_ids = css_select("tbody tr").map { |row| row["id"] }
assert_equal [ dom_id(first), dom_id(last) ], row_ids
end

test "create appends the module and ignores a submitted position" do
ContentModule.create!(program: programs(:one), level: "moderate", name: "Existing")

post admin_content_modules_url, params: {
content_module: { name: "New Module", program_id: programs(:one).id, level: "moderate", position: 99 }
}

assert_equal 2, ContentModule.find_by!(name: "New Module").position
end

test "should update content module" do
Expand Down
41 changes: 41 additions & 0 deletions test/controllers/admin/contents_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "test_helper"

class Admin::ContentsControllerTest < ActionDispatch::IntegrationTest
setup do
@content_module = ContentModule.create!(program: programs(:one), level: "basic", name: "Module With Contents")
sign_in_as users(:one)
end

test "destroy removes a game from the module but keeps the game" do
content = @content_module.contents.create!(contentable: games(:one))

assert_difference "Content.count", -1 do
assert_no_difference "Game.count" do
delete admin_content_url(content)
end
end

assert_redirected_to edit_admin_content_module_url(@content_module)
end

test "destroy deletes the link behind a link content" do
content = @content_module.contents.create!(
contentable: Link.new(title: "Survey", url: "https://example.com/survey", link_type: "survey")
)

assert_difference [ "Content.count", "Link.count" ], -1 do
delete admin_content_url(content)
end

assert_redirected_to edit_admin_content_module_url(@content_module)
end

test "destroy requires admin sign in" do
sign_out

assert_no_difference "Content.count" do
delete admin_content_url(contents(:one))
end
assert_redirected_to new_admin_session_url
end
end
7 changes: 0 additions & 7 deletions test/controllers/admin/links_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,4 @@ class Admin::LinksControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to edit_admin_content_module_url(@mod)
assert_equal "Updated Title", @link.reload.title
end

test "should destroy link" do
assert_difference "Link.count", -1 do
delete admin_link_url(@link)
end
assert_redirected_to edit_admin_content_module_url(@mod)
end
end
Loading