Skip to content

Commit 4a3085a

Browse files
feat: list a module's contents in one ordered table (#166)
* feat: list a module's contents in one ordered table Links say Delete and games say Remove, both through ContentsController#destroy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: update tests --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e833a80 commit 4a3085a

13 files changed

Lines changed: 125 additions & 101 deletions

File tree

‎app/controllers/admin/content_modules_controller.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index
66
@active_program = @programs.find { |p| p.id.to_s == params[:program_id] } || @programs.first
77
@modules_by_level = @active_program
88
.content_modules.ordered
9-
.includes(:links)
9+
.includes(:contents)
1010
.group_by(&:level)
1111
end
1212

@@ -25,6 +25,7 @@ def create
2525
end
2626

2727
def edit
28+
@contents = @content_module.contents.includes(:contentable)
2829
end
2930

3031
def update
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Admin::ContentsController < AdminController
2+
before_action :set_content, only: %i[destroy]
3+
4+
def destroy
5+
@content.destroy!
6+
redirect_to edit_admin_content_module_path(@content.content_module), notice: "Content was successfully removed.", status: :see_other
7+
end
8+
9+
private
10+
def set_content
11+
@content = Content.find(params.expect(:id))
12+
end
13+
end

‎app/controllers/admin/links_controller.rb‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Admin::LinksController < AdminController
22
before_action :set_content_module, only: %i[new create]
3-
before_action :set_link, only: %i[edit update destroy]
3+
before_action :set_link, only: %i[edit update]
44

55
def new
66
@link = Link.new
@@ -29,12 +29,6 @@ def update
2929
end
3030
end
3131

32-
def destroy
33-
content_module = @link.content_module
34-
@link.destroy!
35-
redirect_to edit_admin_content_module_path(content_module), notice: "Link was successfully deleted.", status: :see_other
36-
end
37-
3832
private
3933
def set_content_module
4034
@content_module = ContentModule.find(params.expect(:content_module_id))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%# locals: (content_module:) %>
2+
<%= tag.tr id: dom_id(content_module) do %>
3+
<td><%= content_module.name %></td>
4+
<td><%= content_module.contents.size %></td>
5+
<td>
6+
<div class="flex gap-2">
7+
<%= link_to "Edit", edit_admin_content_module_path(content_module), class: "btn btn-sm btn-primary" %>
8+
<%= button_to "Delete", admin_content_module_path(content_module), method: :delete,
9+
data: { "turbo-confirm": "Delete this module?" },
10+
class: "btn btn-sm btn-error" %>
11+
</div>
12+
</td>
13+
<% end %>

‎app/views/admin/content_modules/edit.html.erb‎

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<li>Edit Module</li>
77
</ul>
88
</nav>
9+
<!--TODO: Add better styling for alerts/notices -->
10+
<p style="color: green"><%= notice %></p>
11+
<p style="color: red"><%= alert %></p>
912
<section class="py-10 flex justify-center">
1013
<div class="flex flex-col gap-6 w-full max-w-lg">
1114
<div class="card card-border bg-base-100 shadow-md">
@@ -17,11 +20,13 @@
1720

1821
<div class="card card-border bg-base-100 shadow-md">
1922
<div class="card-body">
20-
<div class="flex justify-between items-center mb-2">
21-
<h2 class="card-title text-lg">Links</h2>
22-
<%= link_to "Add link", new_admin_content_module_link_path(@content_module), class: "btn btn-sm btn-primary" %>
23+
<div class="flex justify-between items-center mb-2 gap-2 flex-wrap">
24+
<h2 class="card-title text-lg">Contents</h2>
25+
<div class="flex items-center gap-2">
26+
<%= link_to "Add link", new_admin_content_module_link_path(@content_module), class: "btn btn-sm btn-primary" %>
27+
</div>
2328
</div>
24-
<% if @content_module.links.any? %>
29+
<% if @contents.any? %>
2530
<table class="table">
2631
<thead>
2732
<tr>
@@ -31,59 +36,11 @@
3136
</tr>
3237
</thead>
3338
<tbody>
34-
<% @content_module.links.each do |link| %>
35-
<tr>
36-
<td><a href="<%= safe_external_url(link.url) %>" target="_blank" class="link inline-flex items-center gap-1"><%= link.title %> ↗</a></td>
37-
<td><%= link.link_type.humanize %></td>
38-
<td>
39-
<div class="flex gap-2">
40-
<%= link_to "Edit", edit_admin_link_path(link), class: "btn btn-xs btn-primary" %>
41-
<%= button_to "Delete", admin_link_path(link), method: :delete,
42-
data: { "turbo-confirm": "Delete this link?" },
43-
class: "btn btn-xs btn-error" %>
44-
</div>
45-
</td>
46-
</tr>
47-
<% end %>
39+
<%= render @contents %>
4840
</tbody>
4941
</table>
5042
<% else %>
51-
<p class="text-sm opacity-60">No links yet.</p>
52-
<% end %>
53-
</div>
54-
</div>
55-
56-
<div class="card card-border bg-base-100 shadow-md">
57-
<div class="card-body">
58-
<div class="flex justify-between items-center mb-2">
59-
<h2 class="card-title text-lg">Games</h2>
60-
</div>
61-
<% if @content_module.games.any? %>
62-
<table class="table">
63-
<thead>
64-
<tr>
65-
<th>Title</th>
66-
<th>Actions</th> <!-- TODO: Flex this to the end of the full width of the table -->
67-
</tr>
68-
</thead>
69-
<tbody>
70-
<% @content_module.games.each do |game| %>
71-
<tr>
72-
<td><%= game.title %></td>
73-
<td>
74-
<div class="flex gap-2">
75-
<%= 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 -->
76-
<%= button_to "Delete", admin_game_path(game), method: :delete,
77-
data: { "turbo-confirm": "Delete this game?" },
78-
class: "btn btn-xs btn-error" %>
79-
</div>
80-
</td>
81-
</tr>
82-
<% end %>
83-
</tbody>
84-
</table>
85-
<% else %>
86-
<p class="text-sm opacity-60">No games yet.</p>
43+
<p class="text-sm opacity-60">No contents yet.</p>
8744
<% end %>
8845
</div>
8946
</div>

‎app/views/admin/content_modules/index.html.erb‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,12 @@
2929
<thead>
3030
<tr>
3131
<th>Name</th>
32-
<th>Links</th>
32+
<th>Contents</th>
3333
<th>Actions</th>
3434
</tr>
3535
</thead>
3636
<tbody>
37-
<% modules.each do |mod| %>
38-
<tr>
39-
<td><%= mod.name %></td>
40-
<td><%= mod.links.size %></td>
41-
<td>
42-
<div class="flex gap-2">
43-
<%= link_to "Edit", edit_admin_content_module_path(mod), class: "btn btn-sm btn-primary" %>
44-
<%= button_to "Delete", admin_content_module_path(mod), method: :delete,
45-
data: { "turbo-confirm": "Delete this module?" },
46-
class: "btn btn-sm btn-error" %>
47-
</div>
48-
</td>
49-
</tr>
50-
<% end %>
37+
<%= render partial: "admin/content_modules/content_module", collection: modules %>
5138
</tbody>
5239
</table>
5340
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%# locals: (content:) %>
2+
<% contentable = content.contentable %>
3+
<%= tag.tr id: dom_id(content) do %>
4+
<%= render "admin/contents/contentables/#{content.contentable_name}", contentable: contentable %>
5+
<td>
6+
<div class="flex gap-2">
7+
<%= link_to "Edit", edit_polymorphic_path([:admin, contentable]), class: "btn btn-xs btn-primary" %>
8+
<% if contentable.owned_by_content? %>
9+
<%= button_to "Delete", admin_content_path(content), method: :delete,
10+
data: { "turbo-confirm": "Delete this link? It will be removed from the module and deleted." },
11+
class: "btn btn-xs btn-error" %>
12+
<% else %>
13+
<%= button_to "Remove", admin_content_path(content), method: :delete,
14+
data: { "turbo-confirm": "Remove this game from the module? The game itself is kept." },
15+
class: "btn btn-xs btn-error" %>
16+
<% end %>
17+
</div>
18+
</td>
19+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%# locals: (contentable:) %>
2+
<td><%= contentable.title %></td>
3+
<td>Game</td>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%# locals: (contentable:) %>
2+
<td><%= link_to "#{contentable.title} ↗", safe_external_url(contentable.url), target: "_blank", rel: "noopener noreferrer", class: "link inline-flex items-center gap-1" %></td>
3+
<td><%= contentable.link_type.humanize %></td>

‎config/routes.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
post :csv_import, to: "csv#import"
2424
end
2525
resources :content_modules, except: [ :show ] do
26-
resources :links, shallow: true, except: %i[index show]
26+
resources :links, shallow: true, except: %i[index show destroy]
27+
resources :contents, shallow: true, only: :destroy
2728
end
2829
resources :classroom_modules, only: %i[update]
2930
end

0 commit comments

Comments
 (0)