diff --git a/app/controllers/post_types_controller.rb b/app/controllers/post_types_controller.rb index 724e4954..d569a822 100644 --- a/app/controllers/post_types_controller.rb +++ b/app/controllers/post_types_controller.rb @@ -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? @@ -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 diff --git a/app/models/post_type.rb b/app/models/post_type.rb index a73c6a63..b46eb5d4 100644 --- a/app/models/post_type.rb +++ b/app/models/post_type.rb @@ -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 diff --git a/app/services/destroy_post_type.rb b/app/services/destroy_post_type.rb new file mode 100644 index 00000000..318b04cf --- /dev/null +++ b/app/services/destroy_post_type.rb @@ -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 diff --git a/app/views/post_types/_confirm_modal.html.erb b/app/views/post_types/_confirm_modal.html.erb new file mode 100644 index 00000000..5d636234 --- /dev/null +++ b/app/views/post_types/_confirm_modal.html.erb @@ -0,0 +1,28 @@ +
+
+ +

Jelenlegi felhasználók (<%= post_type.name %>)

+ + <%= 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' } %> + + + + + + + + + + <% post_type.posts.sort_by { |p| p.membership.status }.each do |post| %> + + + + + <% end %> + +
NévStátusz
<%= post.membership.user.full_name %><%= I18n.t post.membership.status, scope: 'statuses' %>
+ +
+
diff --git a/app/views/post_types/index.html.erb b/app/views/post_types/index.html.erb index d6b73fd5..09b99b11 100644 --- a/app/views/post_types/index.html.erb +++ b/app/views/post_types/index.html.erb @@ -7,15 +7,23 @@
<% @post_types.each do |post_type| %> -
-
- <%= post_type.name %> -
-
- <%= 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" %> +
+
+ <%= post_type.name %> +
+
+ <% if post_type.posts.any? %> +
+ +
+ <% 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 %> +
-
<% end %>
@@ -39,6 +47,10 @@
<%= 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" %>
+ +<% @post_types.each do |post_type| %> + <%= render 'confirm_modal', post_type: post_type %> +<% end %> diff --git a/config/locales/hu.yml b/config/locales/hu.yml index 2f4efd7f..cd111c7a 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -1,4 +1,8 @@ hu: + statuses: + inactive: "Öregtag" + active: "Aktív" + archived: "Archivált" date: formats: default: "%Y.%m.%d." diff --git a/spec/requests/post_type_controller_spec.rb b/spec/requests/post_type_controller_spec.rb index 026c0ba2..aada8b32 100644 --- a/spec/requests/post_type_controller_spec.rb +++ b/spec/requests/post_type_controller_spec.rb @@ -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