diff --git a/app/controllers/contents_controller.rb b/app/controllers/contents_controller.rb
index 45dd6a6..6371290 100644
--- a/app/controllers/contents_controller.rb
+++ b/app/controllers/contents_controller.rb
@@ -37,6 +37,12 @@ def update_position
head :no_content
end
+ def destroy
+ @content = Content.find(params[:id])
+ @content.destroy
+ redirect_to group_path(@content.group), notice: "Content deleted"
+ end
+
private
def content_params
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index 8a388f7..cb3dd2e 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -37,6 +37,12 @@ def update
end
end
+ def destroy
+ @group = Group.find(params[:id])
+ @group.destroy
+ redirect_to groups_path, notice: "Content group deleted"
+ end
+
private
def group_params
diff --git a/app/models/content.rb b/app/models/content.rb
index 117ecdb..b32eaa2 100644
--- a/app/models/content.rb
+++ b/app/models/content.rb
@@ -2,7 +2,7 @@ class Content < ApplicationRecord
belongs_to :group
positioned on: :group
- has_many :messages
+ has_many :messages, dependent: :nullify
validates_presence_of :body, :link
end
diff --git a/app/models/group.rb b/app/models/group.rb
index 3378c2e..984da97 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -1,5 +1,5 @@
class Group < ApplicationRecord
- has_many :contents
+ has_many :contents, dependent: :destroy
validates :name, :age_in_months, presence: true
end
diff --git a/app/models/message.rb b/app/models/message.rb
index eb1c901..819e240 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -4,4 +4,12 @@ class Message < ApplicationRecord
validates :body, presence: true
scope :with_content, -> { where.not(content: nil) }
+
+ def admin_status
+ if status == "delivered"
+ clicked_on ? "Clicked" : "Delivered"
+ else
+ status&.capitalize
+ end
+ end
end
diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb
index 74caec1..de898f2 100644
--- a/app/views/groups/index.html.erb
+++ b/app/views/groups/index.html.erb
@@ -20,7 +20,10 @@
<%= link_to group.name, group, class: "underline hover:no-underline" %> |
<%= group.age_in_months %> months |
<%= group.contents.size %> |
- <%= link_to "Edit", edit_group_path(group), class: "underline hover:no-underline" %> |
+
+ <%= link_to "Edit", edit_group_path(group), class: "underline hover:no-underline" %>
+ <%= link_to "Delete", group_path(group), class: "underline hover:no-underline", data: {turbo_method: :delete, turbo_confirm: 'Are you sure? This will also delete all the content in this group'} %>
+ |
<% end %>
diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb
index 2063258..3d625ff 100644
--- a/app/views/groups/show.html.erb
+++ b/app/views/groups/show.html.erb
@@ -25,7 +25,10 @@
<%= content.body %> |
<%= content.link %> |
- <%= link_to "Edit", edit_group_content_path(@group, content), class: "underline hover:no-underline" %> |
+
+ <%= link_to "Edit", edit_group_content_path(@group, content), class: "underline hover:no-underline" %>
+ <%= link_to "Delete", group_content_path(@group, content), class: "underline hover:no-underline", data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
+ |
<% end %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index b83d42b..b7bc736 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -12,6 +12,7 @@
Messages |
+ Status |
@@ -19,6 +20,7 @@
<% @user.messages.each do |message| %>
<%= message.body %> |
+ <%= message.admin_status %> |
<% end %>
<% else %>
diff --git a/test/system/contents_test.rb b/test/system/contents_test.rb
index 20eb63a..d822eaf 100644
--- a/test/system/contents_test.rb
+++ b/test/system/contents_test.rb
@@ -52,4 +52,23 @@ class ContentsTest < ApplicationSystemTestCase
assert_text "Content updated!"
assert_text "Updated Content"
end
+
+ test "deleting content" do
+ content = create(:content, body: "Content to delete", group: @group)
+ message = create(:message, user: create(:user), content: content)
+
+ sign_in
+ visit group_path(@group)
+
+ assert_text "Content to delete"
+
+ accept_confirm do
+ click_on "Delete", match: :first
+ end
+
+ assert_text "Content deleted"
+ refute_text "Content to delete"
+
+ assert_nil message.reload.content
+ end
end
diff --git a/test/system/groups_test.rb b/test/system/groups_test.rb
index 1b6b295..bf2c658 100644
--- a/test/system/groups_test.rb
+++ b/test/system/groups_test.rb
@@ -47,4 +47,21 @@ class GroupsTest < ApplicationSystemTestCase
assert_text "Content group updated"
assert_text "New group name"
end
+
+ test "deleting a group" do
+ group = create(:group, name: "Group to delete")
+ create(:message, user: create(:user), content: group.contents.first)
+
+ sign_in
+ visit groups_path
+
+ assert_text "Group to delete"
+
+ accept_confirm do
+ click_on "Delete", match: :first
+ end
+
+ assert_text "Content group deleted"
+ refute_text "Group to delete"
+ end
end