From b544716f5ab0f2927932d2e920e55892bcd4306b Mon Sep 17 00:00:00 2001 From: stage-rl Date: Tue, 26 Sep 2023 15:04:09 +0200 Subject: [PATCH] Minor refactor based on PR review --- .../attached_tag_label_component.html.erb | 2 +- app/controllers/messages_controller.rb | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/components/common/attached_tag_label_component.html.erb b/app/components/common/attached_tag_label_component.html.erb index f0c628a39..8baf58ec7 100644 --- a/app/components/common/attached_tag_label_component.html.erb +++ b/app/components/common/attached_tag_label_component.html.erb @@ -2,7 +2,7 @@

<%= @tag.name %>

<% if @object_tag.deletable %> - <%= button_to url_for(@object_tag), method: :delete, form: { data: { turbo_confirm: "Naozaj odstrániť štítok #{tag.name} zo správy?" } } do %> + <%= button_to url_for(@object_tag), method: :delete, form: { data: { turbo_confirm: "Naozaj odstrániť štítok #{@tag.name} zo správy?" } } do %> diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 171abc75c..e66d6092c 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -29,9 +29,8 @@ def set_message_tags_with_deletable_flag @message_tags_with_deletable_flag = @message .messages_tags - .joins(:tag) .includes(:tag) - .select("messages_tags.*, tags.*, case when exists (#{permitted_tag_query.to_sql} and tags.id = messages_tags.tag_id) then true else false end as deletable") + .select("messages_tags.*, #{deletable_subquery('tags.id = messages_tags.tag_id').to_sql} as deletable") .order("tags.name") end @@ -40,14 +39,18 @@ def set_thread_tags_with_deletable_flag @message .thread .message_threads_tags - .joins(:tag) .includes(:tag) - .select("message_threads_tags.*, tags.*, case when exists (#{permitted_tag_query.to_sql} and tags.id = message_threads_tags.tag_id) then true else false end as deletable") - .order("tags.name") + .select("message_threads_tags.*, #{deletable_subquery('tags.id = message_threads_tags.tag_id').to_sql} as deletable") + .order('tags.name') end - def permitted_tag_query - Tag.joins(:groups, { groups: :group_memberships }).where(group_memberships: { user_id: Current.user.id }) + def deletable_subquery(where_clause) + Tag + .joins(:groups, { groups: :group_memberships }) + .where('group_memberships.user_id = ?', Current.user.id) + .where(where_clause) + .arel + .exists end def permit_reply_params