Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use confirmation modal when deleting non empty post types #418

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 8 additions & 10 deletions app/controllers/post_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PostTypesController < ApplicationController

def create
post_type_params = params.require(:post_type).permit(:name, :group_id)
post_type = PostType.create(post_type_params)
post_type = PostType.create(post_type_params)

group_url = group_path(params[:group_id])
return redirect_back fallback_location: group_url unless post_type.errors.any?
Expand All @@ -13,19 +13,17 @@ def create
end

def index
@post_types = PostType.where(group_id: @group.id).without_common
@post_types = PostType.where(group_id: @group.id).without_common.with_users
end

def destroy
post_type = PostType.find(params[:id])
can_be_deleted =
post_type.posts.empty? &&
!PostType::COMMON_TYPES.include?(post_type.id) &&
post_type.group.id == @group.id

return redirect_back fallback_location: group_url, alert: 'A poszt nem törölhető, mert használatban van!' unless can_be_deleted
post_type.destroy
redirect_back fallback_location: group_url

if DestroyPostType.call(@group, post_type)
redirect_back fallback_location: group_url, notice: 'Poszt törlése sikeres volt'
else
redirect_back fallback_location: group_url, alert: 'Alapvető posztokat nem lehet törölni'
end
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/post_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ class PostType < ApplicationRecord
LEADER_ASSISTANT_ID
].freeze
scope :without_common, -> { where.not(id: COMMON_TYPES) }
scope :with_users, -> {includes(posts:{membership: :user})}
end
12 changes: 12 additions & 0 deletions app/services/destroy_post_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class DestroyPostType
def self.call(group, post_type)
return false if PostType::COMMON_TYPES.include?(post_type.id) || post_type.group_id != group.id

ActiveRecord::Base.transaction do
post_type.posts.each(&:destroy) if post_type.posts.any?
post_type.destroy
end
end
end
28 changes: 28 additions & 0 deletions app/views/post_types/_confirm_modal.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div id="delete_modal_<%= post_type.id %>" class="uk-modal">
<div class="uk-modal-dialog uk-margin-auto-vertical">
<a class="uk-modal-close uk-close"></a>
<h1>Jelenlegi felhasználók (<%= post_type.name %>)</h1>

<%= button_to "Poszt törlese", group_post_type_path(@group.id, post_type),
action: :destroy, method: :delete, class: "uk-button uk-button-danger uk-align-center",
data: { confirm: 'Biztos hogy törlöd a posztot? Ez a művelet elveszi a posztot a jelenlegi tulajdonosaitól' } %>

<table class="uk-table uk-table-striped">
<thead>
<tr>
<th>Név</th>
<th>Státusz</th>
</tr>
</thead>
<tbody>
<% post_type.posts.sort_by { |p| p.membership.status }.each do |post| %>
<tr class="<%= 'uk-text-bold' if post.membership.status == 'active' %>">
<td><%= post.membership.user.full_name %></td>
<td><%= I18n.t post.membership.status, scope: 'statuses' %></td>
</tr>
<% end %>
</tbody>
</table>

</div>
</div>
30 changes: 21 additions & 9 deletions app/views/post_types/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@
<div id="content-main" class="uk-panel uk-panel-box">
<div class="uk-grid uk-margin-remove uk-width-medium-1-2">
<% @post_types.each do |post_type| %>
<div class="uk-width-1-1 uk-grid uk-margin-remove uk-padding-remove">
<div class="uk-width-1-2 uk-text-left uk-text-bold">
<%= post_type.name %>
</div>
<div class="uk-width-1-2">
<%= button_to "Poszt törlese", group_post_type_path(@group.id, post_type),
action: :destroy, method: :delete, class: "uk-button uk-button-danger uk-button-mini uk-width-1-2 uk-align-center" %>
<div class="uk-width-1-1 uk-grid uk-margin-remove uk-padding-remove">
<div class="uk-width-1-2 uk-text-left uk-text-bold">
<%= post_type.name %>
</div>
<div class="uk-width-1-2">
<% if post_type.posts.any? %>
<div class="button_to">
<button data-uk-modal="{target:'#delete_modal_<%= post_type.id %>'}" class="uk-button uk-button-danger uk-button-mini uk-width-1-2 uk-align-center">
Poszt törlese
</button>
</div>
<% else %>
<%= button_to "Poszt törlese", group_post_type_path(@group.id, post_type),
action: :destroy, method: :delete, class: "uk-button uk-button-danger uk-button-mini uk-width-1-2 uk-align-center" %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>

Expand All @@ -39,6 +47,10 @@

<div class="uk-margin-large-top uk-margin" id="back-button-container">
<%= button_to "Vissza", group_path(@group.id), method: :get,
class: "uk-button uk-button-primary uk-button-large uk-align-center" %>
class: "uk-button uk-button-primary uk-button-large uk-align-center" %>
</div>
</div>

<% @post_types.each do |post_type| %>
<%= render 'confirm_modal', post_type: post_type %>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/hu.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
hu:
statuses:
inactive: "Öregtag"
active: "Aktív"
archived: "Archivált"
date:
formats:
default: "%Y.%m.%d."
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/post_type_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@
end

context 'when the post type is in use' do
it 'does not delete the post type' do
it 'does delete the post type and associated posts' do
post = build(:post, post_type_id: post_type.id)
post_type.posts << post

delete "/groups/#{group.id}/post_types/#{post_type.id}"

expect(PostType.where(id: post_type.id)).to exist
expect(flash[:alert]).to be_present
expect(PostType.where(id: post_type.id)).not_to exist
expect(flash[:alert]).not_to be_present
end
end
end
Expand Down