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 @@ +
Név | +Státusz | +
---|---|
<%= post.membership.user.full_name %> | +<%= I18n.t post.membership.status, scope: 'statuses' %> | +