Skip to content

Commit

Permalink
Minor refactor based on PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
stage-rl committed Sep 26, 2023
1 parent 7e48396 commit b544716
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="flex justify-center items-center relative gap-1 px-2 py-1 rounded-md bg-gray-50 border border-gray-300">
<p class=" text-sm text-left text-gray-600"><%= @tag.name %></p>
<% 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 %>
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" class=" w-3.5 h-3.5 relative" preserveAspectRatio="xMidYMid meet">
<path d="M3.5 3.5L10.5 10.5M3.5 10.5L10.5 3.5L3.5 10.5Z" stroke="#6B7280" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
Expand Down
17 changes: 10 additions & 7 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit b544716

Please sign in to comment.