Skip to content

Commit

Permalink
Merge pull request #13 from nestauk/destroy-content-content-groups
Browse files Browse the repository at this point in the history
Can delete groups and content
  • Loading branch information
cdccollins authored Sep 25, 2024
2 parents cd7c69a + ae7ca90 commit 78b3f05
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Group < ApplicationRecord
has_many :contents
has_many :contents, dependent: :destroy

validates :name, :age_in_months, presence: true
end
8 changes: 8 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion app/views/groups/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
<td class="p-4"><%= link_to group.name, group, class: "underline hover:no-underline" %></td>
<td class="p-4"><%= group.age_in_months %> months</td>
<td class="p-4"><%= group.contents.size %></td>
<td class="p-4"><%= link_to "Edit", edit_group_path(group), class: "underline hover:no-underline" %></td>
<td class="p-4">
<%= 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'} %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
5 changes: 4 additions & 1 deletion app/views/groups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
</td>
<td class="p-4"><%= content.body %></td>
<td class="p-4"><%= content.link %></td>
<td class="p-4"><%= link_to "Edit", edit_group_content_path(@group, content), class: "underline hover:no-underline" %></td>
<td class="p-4">
<%= 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?'} %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 2 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
<thead>
<tr class="bg-purple-300">
<th class="px-4 py-4 text-left">Messages</th>
<th class="px-4 py-4 text-left">Status</th>
</tr>
</thead>
<tbody>
<% if @user.messages.any? %>
<% @user.messages.each do |message| %>
<tr class="border-b-2">
<td class="px-4 py-4 bg-white"><%= message.body %></td>
<td class="px-4 py-4 bg-white"><%= message.admin_status %></td>
</tr>
<% end %>
<% else %>
Expand Down
19 changes: 19 additions & 0 deletions test/system/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions test/system/groups_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 78b3f05

Please sign in to comment.