diff --git a/app/components/admin/boxes/box_form_component.html.erb b/app/components/admin/boxes/box_form_component.html.erb index bb268c06f..5a055f230 100644 --- a/app/components/admin/boxes/box_form_component.html.erb +++ b/app/components/admin/boxes/box_form_component.html.erb @@ -29,7 +29,7 @@
- <%= form.select :color, @color_select_options, {}, class: "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" %> + <%= form.select :color, helpers.color_select_options, {}, class: "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" %>
diff --git a/app/components/admin/boxes/box_form_component.rb b/app/components/admin/boxes/box_form_component.rb index 284a5f09b..56dca61cb 100644 --- a/app/components/admin/boxes/box_form_component.rb +++ b/app/components/admin/boxes/box_form_component.rb @@ -1,7 +1,8 @@ class Admin::Boxes::BoxFormComponent < ViewComponent::Base + include ColorizedHelper + def initialize(box:, action:) @box = box @action = action - @color_select_options = Box.colors.keys.map { |color| [color, { class: "bg-#{color}-100 text-#{color}-600" }] } end end diff --git a/app/components/admin/permissions/groups_list_row_component.html.erb b/app/components/admin/permissions/groups_list_row_component.html.erb index 9ac1df95f..1af915997 100644 --- a/app/components/admin/permissions/groups_list_row_component.html.erb +++ b/app/components/admin/permissions/groups_list_row_component.html.erb @@ -13,7 +13,9 @@
- <%= render Admin::Permissions::TagComponent.with_collection(@group.tags) %> + <% @group.tags.each do |tag| %> + <%= render Common::TagComponent.new(tag) %> + <% end %>
diff --git a/app/components/admin/permissions/tag_component.html.erb b/app/components/admin/permissions/tag_component.html.erb deleted file mode 100644 index 2fa363288..000000000 --- a/app/components/admin/permissions/tag_component.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -
-

<%= @tag.name %>

- - - -
diff --git a/app/components/admin/permissions/tag_component.rb b/app/components/admin/permissions/tag_component.rb deleted file mode 100644 index b0981a643..000000000 --- a/app/components/admin/permissions/tag_component.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Admin::Permissions::TagComponent < ViewComponent::Base - def initialize(tag:) - @tag = tag - end -end diff --git a/app/components/admin/tags/tag_form_component.html.erb b/app/components/admin/tags/tag_form_component.html.erb index ad27d9ff7..b0761ddd1 100644 --- a/app/components/admin/tags/tag_form_component.html.erb +++ b/app/components/admin/tags/tag_form_component.html.erb @@ -17,6 +17,16 @@ <%= form.text_field :name, placeholder: "Názov štítku", class: "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" %> +
+
+ <%= form.select :color, helpers.color_select_options, {}, class: "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" %> +
+
+
+
+ <%= form.select :icon, helpers.icon_select_options, {}, class: "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" %> +
+
<%= link_to admin_tenant_tags_path(Current.tenant), class: "flex justify-center items-center flex-grow relative overflow-hidden gap-2.5 px-3.5 py-2.5 rounded-md bg-white border border-gray-300", data: { turbo_frame: "_top" } do %> diff --git a/app/components/admin/tags/tag_form_component.rb b/app/components/admin/tags/tag_form_component.rb index cab8e35e2..901f4b82c 100644 --- a/app/components/admin/tags/tag_form_component.rb +++ b/app/components/admin/tags/tag_form_component.rb @@ -1,4 +1,5 @@ class Admin::Tags::TagFormComponent < ViewComponent::Base + include ColorizedHelper, IconizedHelper def initialize(tag:) @tag = tag end diff --git a/app/components/admin/tags/tags_list_component.html.erb b/app/components/admin/tags/tags_list_component.html.erb index 3888b56e6..97ca27705 100644 --- a/app/components/admin/tags/tags_list_component.html.erb +++ b/app/components/admin/tags/tags_list_component.html.erb @@ -17,8 +17,8 @@ <%= render Icons::InfoComponent.big_gray %>
-
Nataviť prístup na základe štítkov
-
Jednotlivý používatelia alebo skupiny používateľov môžu mať rozdielny prístup k správam
+
Nastaviť prístup na základe štítkov
+
Jednotliví používatelia alebo skupiny používateľov môžu mať rozdielny prístup k správam
<%= link_to admin_tenant_tag_groups_path, class: "px-3.5 py-2.5 bg-white rounded-md border border-gray-300 justify-center items-center gap-2.5 flex hover:bg-gray-100" do %>

Nastaviť prístup

diff --git a/app/components/admin/tags/tags_list_row_component.html.erb b/app/components/admin/tags/tags_list_row_component.html.erb index 9566ad19a..a9d1ca8ef 100644 --- a/app/components/admin/tags/tags_list_row_component.html.erb +++ b/app/components/admin/tags/tags_list_row_component.html.erb @@ -1,12 +1,7 @@
- <%= tag.turbo_frame id: "tag-name-#{@tag.id}" do %> - <%= render Common::InlineRenameComponent.new(name: @tag.name, model: @tag, url: admin_tenant_tag_path(@tag.tenant, @tag)) %> - <% end %> - <% if @tag.external_name.present? %> - <%= @tag.external_name %> - <% end %> + <%= render Common::TagComponent.new(@tag) %>
<%= render Admin::Tags::VisibilityToggleComponent.new(@tag) %> diff --git a/app/components/common/box_label_component.html.erb b/app/components/common/box_label_component.html.erb index d2d9f622d..ce1bc0aaf 100644 --- a/app/components/common/box_label_component.html.erb +++ b/app/components/common/box_label_component.html.erb @@ -1,36 +1,5 @@ -<%# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %> -<%# If color values are changed, adjust the color list below !!!!!!!!! %> -<%# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %>
<%= @box.short_name || @box.name[0] %>
- -<%# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %> -<%# Necessary to generate proper CSS for dynamically generated colors %> -<%# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %> -<%# !!!!!! Change values here, if values in template change !!!!!! %> -<%# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %> -<%# class="bg-slate-100 border border-slate-400 text-slate-600" %> -<%# class="bg-gray-100 border border-gray-400 text-gray-600" %> -<%# class="bg-zinc-100 border border-zinc-400 text-zinc-600" %> -<%# class="bg-neutral-100 border border-neutral-400 text-neutral-600" %> -<%# class="bg-stone-100 border border-stone-400 text-stone-600" %> -<%# class="bg-red-100 border border-red-400 text-red-600" %> -<%# class="bg-orange-100 border border-orange-400 text-orange-600" %> -<%# class="bg-amber-100 border border-amber-400 text-amber-600" %> -<%# class="bg-yellow-100 border border-yellow-400 text-yellow-600" %> -<%# class="bg-lime-100 border border-lime-400 text-lime-600" %> -<%# class="bg-green-100 border border-green-400 text-green-600" %> -<%# class="bg-emerald-100 border border-emerald-400 text-emerald-600" %> -<%# class="bg-teal-100 border border-teal-400 text-teal-600" %> -<%# class="bg-cyan-100 border border-cyan-400 text-cyan-600" %> -<%# class="bg-sky-100 border border-sky-400 text-sky-600" %> -<%# class="bg-blue-100 border border-blue-400 text-blue-600" %> -<%# class="bg-indigo-100 border border-indigo-400 text-indigo-600" %> -<%# class="bg-violet-100 border border-violet-400 text-violet-600" %> -<%# class="bg-purple-100 border border-purple-400 text-purple-600" %> -<%# class="bg-fuchsia-100 border border-fuchsia-400 text-fuchsia-600" %> -<%# class="bg-pink-100 border border-pink-400 text-pink-600" %> -<%# class="bg-rose-100 border border-rose-400 text-rose-600" %> diff --git a/app/components/common/icon_component.html.erb b/app/components/common/icon_component.html.erb new file mode 100644 index 000000000..55f91f273 --- /dev/null +++ b/app/components/common/icon_component.html.erb @@ -0,0 +1,3 @@ + + + diff --git a/app/components/common/icon_component.rb b/app/components/common/icon_component.rb new file mode 100644 index 000000000..0b9cda39c --- /dev/null +++ b/app/components/common/icon_component.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Common + class IconComponent < ViewComponent::Base + ICONS = { + "key" => "M15.75 5.25a3 3 0 013 3m3 0a6 6 0 01-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1121.75 8.25z", + "fingerprint" => "M7.864 4.243A7.5 7.5 0 0119.5 10.5c0 2.92-.556 5.709-1.568 8.268M5.742 6.364A7.465 7.465 0 004.5 10.5a7.464 7.464 0 01-1.15 3.993m1.989 3.559A11.209 11.209 0 008.25 10.5a3.75 3.75 0 117.5 0c0 .527-.021 1.049-.064 1.565M12 10.5a14.94 14.94 0 01-3.6 9.75m6.633-4.596a18.666 18.666 0 01-2.485 5.33", + "pencil" => "M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125", + "check" => "M4.5 12.75l6 6 9-13.5" + }.freeze + + def initialize(icon, classes: "") + @icon = icon + @svg = ICONS[icon] + @classes = classes + end + end +end diff --git a/app/components/common/tag_component.html.erb b/app/components/common/tag_component.html.erb index 9dc801a3a..6a37710d4 100644 --- a/app/components/common/tag_component.html.erb +++ b/app/components/common/tag_component.html.erb @@ -1,3 +1,6 @@ - + + <% if @icon.present? %> + <%= render Common::IconComponent.new(@icon, classes: "inline") %> + <% end %> <%= @label %> \ No newline at end of file diff --git a/app/components/common/tag_component.rb b/app/components/common/tag_component.rb index 299d0fc87..46aec88ce 100644 --- a/app/components/common/tag_component.rb +++ b/app/components/common/tag_component.rb @@ -1,8 +1,10 @@ module Common class TagComponent < ViewComponent::Base - def initialize(label, classes="") - @label = label + def initialize(tag, classes="", color: nil) + @label = tag.name || tag.external_name @classes = classes + @color = tag.color || "gray" + @icon = tag.icon.presence || (tag.gives_access? ? "key" : nil) end end end diff --git a/app/components/message_thread_header_component.html.erb b/app/components/message_thread_header_component.html.erb index 1bfeab965..c5709e816 100644 --- a/app/components/message_thread_header_component.html.erb +++ b/app/components/message_thread_header_component.html.erb @@ -9,7 +9,7 @@
<% @thread_tags.each do |thread_tag|%> - <%= render MessageThreads::TagComponent.new(thread_tag) %> + <%= render Common::TagComponent.new(thread_tag.tag) %> <% end %> <%= link_to edit_message_thread_tags_path(@message_thread), data: { turbo_frame: "modal" }, title: "Upraviť štítky", class: "rounded-md bg-blue-100 p-1.5 hover:bg-blue-200" do %> <%= render Icons::TagComponent.new(css_classes: "h-4 w-4 text-blue-600", stroke_width: 2) %> diff --git a/app/components/message_threads/tag_component.html.erb b/app/components/message_threads/tag_component.html.erb deleted file mode 100644 index 64182e336..000000000 --- a/app/components/message_threads/tag_component.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -
- <%= @tag.name %> -
diff --git a/app/components/message_threads/tag_component.rb b/app/components/message_threads/tag_component.rb deleted file mode 100644 index 1d6f7b744..000000000 --- a/app/components/message_threads/tag_component.rb +++ /dev/null @@ -1,8 +0,0 @@ -module MessageThreads - class TagComponent < ViewComponent::Base - def initialize(message_thread_tag) - @message_thread_tag = message_thread_tag - @tag = @message_thread_tag.tag - end - end -end diff --git a/app/components/message_threads_table_row_component.html.erb b/app/components/message_threads_table_row_component.html.erb index 733b40d81..38ee36e3c 100644 --- a/app/components/message_threads_table_row_component.html.erb +++ b/app/components/message_threads_table_row_component.html.erb @@ -17,7 +17,7 @@ <% if @visible_tags.present? %>
<% @visible_tags.each do |tag| %> - <%= render Common::TagComponent.new(tag.name) %> + <%= render Common::TagComponent.new(tag) %> <% end %>
<% end %> diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index bf352429b..b6d81b732 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -61,6 +61,6 @@ def set_tag end def simple_tag_params - params.require(:simple_tag).permit(:name, :visible) + params.require(:simple_tag).permit(:name, :visible, :color, :icon) end end diff --git a/app/helpers/colorized_helper.rb b/app/helpers/colorized_helper.rb new file mode 100644 index 000000000..0f9b49105 --- /dev/null +++ b/app/helpers/colorized_helper.rb @@ -0,0 +1,29 @@ +module ColorizedHelper + def color_select_options + [""] + Colorized.colors.map { |color| [color, { class: "bg-#{color}-100 text-#{color}-600" }] } + end +end + +# NOTE: Tailwind processor catches these +# class="bg-slate-50 border-slate-400 text-slate-600 ring-slate-500/10" +# class="bg-gray-50 border-gray-400 text-gray-600 ring-gray-500/10" +# class="bg-zinc-50 border-zinc-400 text-zinc-600 ring-zinc-500/10" +# class="bg-neutral-50 border-neutral-400 text-neutral-600 ring-neutral-500/10" +# class="bg-stone-50 border-stone-400 text-stone-600 ring-stone-500/10" +# class="bg-red-50 border-red-400 text-red-600 ring-red-500/10" +# class="bg-orange-50 border-orange-400 text-orange-600 ring-orange-500/10" +# class="bg-amber-50 border-amber-400 text-amber-600 ring-amber-500/10" +# class="bg-yellow-50 border-yellow-400 text-yellow-600 ring-yellow-500/10" +# class="bg-lime-50 border-lime-400 text-lime-600 ring-lime-500/10" +# class="bg-green-50 border-green-400 text-green-600 ring-green-500/10" +# class="bg-emerald-50 border-emerald-400 text-emerald-600 ring-emerald-500/10" +# class="bg-teal-50 border-teal-400 text-teal-600 ring-teal-500/10" +# class="bg-cyan-50 border-cyan-400 text-cyan-600 ring-cyan-500/10" +# class="bg-sky-50 border-sky-400 text-sky-600 ring-sky-500/10" +# class="bg-blue-50 border-blue-400 text-blue-600 ring-blue-500/10" +# class="bg-indigo-50 border-indigo-400 text-indigo-600 ring-indigo-500/10" +# class="bg-violet-50 border-violet-400 text-violet-600 ring-violet-500/10" +# class="bg-purple-50 border-purple-400 text-purple-600 ring-purple-500/10" +# class="bg-fuchsia-50 border-fuchsia-400 text-fuchsia-600 ring-fuchsia-500/10" +# class="bg-pink-50 border-pink-400 text-pink-600 ring-pink-500/10" +# class="bg-rose-50 border-rose-400 text-rose-600 ring-rose-500/10" diff --git a/app/helpers/iconized_helper.rb b/app/helpers/iconized_helper.rb new file mode 100644 index 000000000..529202e7f --- /dev/null +++ b/app/helpers/iconized_helper.rb @@ -0,0 +1,5 @@ +module IconizedHelper + def icon_select_options + [""] + Iconized.icons + end +end diff --git a/app/models/box.rb b/app/models/box.rb index 2b41b62e7..1a95cbe2b 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -15,6 +15,8 @@ # tenant_id :bigint not null # class Box < ApplicationRecord + include Colorized + belongs_to :tenant belongs_to :api_connection @@ -30,32 +32,6 @@ class Box < ApplicationRecord before_create { self.color = Box.colors.keys[name.hash % Box.colors.size] if color.blank? } - enum :color, - { - slate: 'slate', - gray: 'gray', - zinc: 'zinc', - neutral: 'neutral', - stone: 'stone', - red: 'red', - orange: 'orange', - amber: 'amber', - yellow: 'yellow', - lime: 'lime', - green: 'green', - emerald: 'emerald', - teal: 'teal', - cyan: 'cyan', - sky: 'sky', - blue: 'blue', - indigo: 'indigo', - violet: 'violet', - purple: 'purple', - fuchsia: 'fuchsia', - pink: 'pink', - rose: 'rose' - } - validate :validate_box_with_api_connection private diff --git a/app/models/concerns/colorized.rb b/app/models/concerns/colorized.rb new file mode 100644 index 000000000..2f0baf238 --- /dev/null +++ b/app/models/concerns/colorized.rb @@ -0,0 +1,59 @@ +module Colorized + extend ActiveSupport::Concern + + included do + enum :color, + { + slate: 'slate', + gray: 'gray', + zinc: 'zinc', + neutral: 'neutral', + stone: 'stone', + red: 'red', + orange: 'orange', + amber: 'amber', + yellow: 'yellow', + lime: 'lime', + green: 'green', + emerald: 'emerald', + teal: 'teal', + cyan: 'cyan', + sky: 'sky', + blue: 'blue', + indigo: 'indigo', + violet: 'violet', + purple: 'purple', + fuchsia: 'fuchsia', + pink: 'pink', + rose: 'rose' + } + end + + def self.colors + [ + :slate, + :gray, + :zinc, + :neutral, + :stone, + :red, + :orange, + :amber, + :yellow, + :lime, + :green, + :emerald, + :teal, + :cyan, + :sky, + :blue, + :indigo, + :violet, + :purple, + :fuchsia, + :pink, + :rose + ] + end +end + diff --git a/app/models/concerns/iconized.rb b/app/models/concerns/iconized.rb new file mode 100644 index 000000000..3a3503a42 --- /dev/null +++ b/app/models/concerns/iconized.rb @@ -0,0 +1,12 @@ +module Iconized + extend ActiveSupport::Concern + + def self.icons + Common::IconComponent::ICONS.keys + end + + included do + validates_inclusion_of :icon, in: Iconized.icons, allow_blank: true + end +end + diff --git a/app/models/draft_tag.rb b/app/models/draft_tag.rb index 16e0839b0..d4f47b6b7 100644 --- a/app/models/draft_tag.rb +++ b/app/models/draft_tag.rb @@ -2,15 +2,21 @@ # # Table name: tags # -# id :bigint not null, primary key -# external_name :string -# name :string not null -# type :string not null -# visible :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :bigint -# tenant_id :bigint not null +# id :bigint not null, primary key +# color :enum +# external_name :string +# icon :string +# name :string not null +# tag_groups_count :integer default(0), not null +# type :string not null +# visible :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint +# tenant_id :bigint not null # class DraftTag < Tag + def destroyable? + false + end end diff --git a/app/models/everything_tag.rb b/app/models/everything_tag.rb new file mode 100644 index 000000000..59e998f35 --- /dev/null +++ b/app/models/everything_tag.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: tags +# +# id :bigint not null, primary key +# color :enum +# external_name :string +# icon :string +# name :string not null +# tag_groups_count :integer default(0), not null +# type :string not null +# visible :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint +# tenant_id :bigint not null +# +class EverythingTag < Tag + def destroyable? + false + end +end diff --git a/app/models/message_thread.rb b/app/models/message_thread.rb index 26cc236ef..e51f8eee7 100644 --- a/app/models/message_thread.rb +++ b/app/models/message_thread.rb @@ -30,6 +30,7 @@ def find_or_create_by_uuid!(uuid:) attr_accessor :search_highlight + after_create_commit ->(thread) { thread.tags << thread.tenant.everything_tag } after_create_commit ->(thread) { EventBus.publish(:message_thread_created, thread) } after_update_commit ->(thread) { EventBus.publish(:message_thread_changed, thread) } diff --git a/app/models/simple_tag.rb b/app/models/simple_tag.rb index 3b116d413..793824314 100644 --- a/app/models/simple_tag.rb +++ b/app/models/simple_tag.rb @@ -2,15 +2,18 @@ # # Table name: tags # -# id :bigint not null, primary key -# external_name :string -# name :string not null -# type :string not null -# visible :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :bigint -# tenant_id :bigint not null +# id :bigint not null, primary key +# color :enum +# external_name :string +# icon :string +# name :string not null +# tag_groups_count :integer default(0), not null +# type :string not null +# visible :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint +# tenant_id :bigint not null # class SimpleTag < Tag def destroyable? diff --git a/app/models/tag.rb b/app/models/tag.rb index 19b7a048f..edb589e7b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,18 +2,22 @@ # # Table name: tags # -# id :bigint not null, primary key -# external_name :string -# name :string not null -# type :string not null -# visible :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :bigint -# tenant_id :bigint not null +# id :bigint not null, primary key +# color :enum +# external_name :string +# icon :string +# name :string not null +# tag_groups_count :integer default(0), not null +# type :string not null +# visible :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint +# tenant_id :bigint not null # class Tag < ApplicationRecord include AuditableEvents + include Colorized, Iconized belongs_to :tenant belongs_to :owner, class_name: 'User', optional: true @@ -31,14 +35,17 @@ class Tag < ApplicationRecord scope :simple, -> { where(type: SimpleTag.to_s) } scope :visible, -> { where(visible: true) } - after_create_commit ->(tag) { tag.mark_readable_by_groups([tag.tenant.admin_group]) } after_update_commit ->(tag) { EventBus.publish(:tag_renamed, tag) if previous_changes.key?("name") } def mark_readable_by_groups(groups) self.groups += groups end + def gives_access? + tag_groups_count.positive? + end + def destroyable? - false + raise NotImplementedError end end diff --git a/app/models/tag_group.rb b/app/models/tag_group.rb index 66ea09b14..92e3a448f 100644 --- a/app/models/tag_group.rb +++ b/app/models/tag_group.rb @@ -12,7 +12,7 @@ class TagGroup < ApplicationRecord include AuditableEvents belongs_to :group - belongs_to :tag + belongs_to :tag, counter_cache: true # used for joins only has_many :group_memberships, primary_key: :group_id, foreign_key: :group_id diff --git a/app/models/tenant.rb b/app/models/tenant.rb index ee8103033..ccfa367f6 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -18,6 +18,7 @@ class Tenant < ApplicationRecord has_many :custom_groups has_one :draft_tag + has_one :everything_tag has_many :boxes, dependent: :destroy has_many :automation_rules, class_name: "Automation::Rule", dependent: :destroy @@ -56,12 +57,20 @@ def disable_feature(feature) save! end + def make_admins_see_everything! + everything_tag.groups << admin_group + end + private def create_default_objects + create_draft_tag!(name: "Rozpracované", visible: true) + create_everything_tag!(name: "Všetky správy", visible: false) + create_all_group!(name: "all") create_admin_group!(name: "admins") create_signer_group!(name: "signers") - create_draft_tag!(name: "Rozpracované", visible: true) + + make_admins_see_everything! end end diff --git a/app/models/upvs/delivery_notification_tag.rb b/app/models/upvs/delivery_notification_tag.rb index 2bb069707..efef2da63 100644 --- a/app/models/upvs/delivery_notification_tag.rb +++ b/app/models/upvs/delivery_notification_tag.rb @@ -2,15 +2,18 @@ # # Table name: tags # -# id :bigint not null, primary key -# external_name :string -# name :string not null -# type :string not null -# visible :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :bigint -# tenant_id :bigint not null +# id :bigint not null, primary key +# color :enum +# external_name :string +# icon :string +# name :string not null +# tag_groups_count :integer default(0), not null +# type :string not null +# visible :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# owner_id :bigint +# tenant_id :bigint not null # class Upvs::DeliveryNotificationTag < ::Tag def self.find_or_create_for_tenant!(tenant) diff --git a/db/migrate/20231205190743_add_icon_to_tags.rb b/db/migrate/20231205190743_add_icon_to_tags.rb new file mode 100644 index 000000000..588309564 --- /dev/null +++ b/db/migrate/20231205190743_add_icon_to_tags.rb @@ -0,0 +1,5 @@ +class AddIconToTags < ActiveRecord::Migration[7.0] + def change + add_column :tags, :icon, :string + end +end diff --git a/db/migrate/20231205190907_add_group_memberships_counter_to_tags.rb b/db/migrate/20231205190907_add_group_memberships_counter_to_tags.rb new file mode 100644 index 000000000..0aa58d1aa --- /dev/null +++ b/db/migrate/20231205190907_add_group_memberships_counter_to_tags.rb @@ -0,0 +1,8 @@ +class AddGroupMembershipsCounterToTags < ActiveRecord::Migration[7.0] + def change + add_column :tags, :tag_groups_count, :integer, null: false, default: 0 + Tag.find_each do |tag| + Tag.reset_counters(tag.id, :tag_groups) + end + end +end diff --git a/db/migrate/20231205192436_add_color_to_tags.rb b/db/migrate/20231205192436_add_color_to_tags.rb new file mode 100644 index 000000000..4c417fbc5 --- /dev/null +++ b/db/migrate/20231205192436_add_color_to_tags.rb @@ -0,0 +1,7 @@ +class AddColorToTags < ActiveRecord::Migration[7.0] + def change + change_table :tags do |t| + t.enum :color, enum_type: 'color' + end + end +end diff --git a/db/migrate/20231206090833_migrate_to_everything_tag.rb b/db/migrate/20231206090833_migrate_to_everything_tag.rb new file mode 100644 index 000000000..7a61cf292 --- /dev/null +++ b/db/migrate/20231206090833_migrate_to_everything_tag.rb @@ -0,0 +1,19 @@ +class MigrateToEverythingTag < ActiveRecord::Migration[7.0] + def up + Tenant.find_each do |tenant| + tenant.admin_group.tag_groups.destroy_all + + tenant.create_everything_tag!(name: "Všetky správy", visible: false) unless tenant.everything_tag + tenant.make_admins_see_everything! + tenant.boxes.find_each do |box| + box.message_threads.find_each do |thread| + thread.tags << tenant.everything_tag + end + end + end + end + + def down + # noop + end +end diff --git a/db/schema.rb b/db/schema.rb index ab2396bf0..1981dde3c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_12_01_113708) do +ActiveRecord::Schema[7.0].define(version: 2023_12_06_090833) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -432,6 +432,9 @@ t.bigint "owner_id" t.string "external_name" t.string "type", null: false + t.enum "color", enum_type: "color" + t.integer "tag_groups_count", default: 0, null: false + t.string "icon" t.index "tenant_id, lower((name)::text)", name: "index_tags_on_tenant_id_and_lowercase_name", unique: true t.index ["owner_id"], name: "index_tags_on_owner_id" t.index ["tenant_id"], name: "index_tags_on_tenant_id" diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml index 6c998cf58..121278182 100644 --- a/test/fixtures/tags.yml +++ b/test/fixtures/tags.yml @@ -38,6 +38,12 @@ ssd_drafts: visible: true tenant: ssd +ssd_everything: + name: Everything + type: EverythingTag + visible: false + tenant: ssd + ssd_other: name: Other type: SimpleTag @@ -73,3 +79,9 @@ solver_drafts: type: DraftTag visible: true tenant: solver + +solver_everything: + name: Everything + type: EverythingTag + visible: false + tenant: solver \ No newline at end of file diff --git a/test/models/everything_tag_test.rb b/test/models/everything_tag_test.rb new file mode 100644 index 000000000..5ec2dca4f --- /dev/null +++ b/test/models/everything_tag_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +class EverythingTagTest < ActiveSupport::TestCase + test "adds everything tag to every thread" do + box = boxes(:ssd_main) + thread = box.message_threads.create!( + title: 'Test', + original_title: 'Test', + delivered_at: Time.current, + last_message_delivered_at: Time.current + ) + + assert_includes thread.tags, box.tenant.everything_tag + end +end diff --git a/test/models/govbox/message_test.rb b/test/models/govbox/message_test.rb index f95d08c69..9c9efb62a 100644 --- a/test/models/govbox/message_test.rb +++ b/test/models/govbox/message_test.rb @@ -21,12 +21,12 @@ class Govbox::MessageTest < ActiveSupport::TestCase assert_equal message.objects.first.message_object_datum.blob, "MyContent" - assert_equal message.tags.count, 1 - assert_equal message.tags.first.name, "slovensko.sk:#{govbox_message.folder.name}" - assert_equal message.tags.first.external_name, "slovensko.sk:#{govbox_message.folder.name}" - assert_equal message.tags.first.visible, false - assert_equal message.thread.tags.count, 1 - assert_equal message.tags.first, message.thread.tags.first + assert_equal 1, message.tags.count + assert_equal "slovensko.sk:#{govbox_message.folder.name}", message.tags.first.name + assert_equal "slovensko.sk:#{govbox_message.folder.name}", message.tags.first.external_name + assert_not message.tags.first.visible + assert_equal 1, message.thread.tags.simple.count + assert_equal message.tags.first, message.thread.tags.simple.first end test "should include general agenda subject in message title" do @@ -48,10 +48,10 @@ class Govbox::MessageTest < ActiveSupport::TestCase message = Message.last - assert_equal message.tags.count, 1 - assert_equal message.tags.first, tag - assert_equal message.thread.tags.count, 1 - assert_equal message.thread.tags.first, tag + assert_equal 1, message.tags.count + assert_equal tag, message.tags.first + assert_equal 1, message.thread.tags.simple.count + assert_equal tag, message.thread.tags.simple.first end test "should not duplicate message thread tags" do @@ -66,15 +66,14 @@ class Govbox::MessageTest < ActiveSupport::TestCase Govbox::Message.create_message_with_thread!(govbox_message2) message2 = Message.last - assert_equal message1.tags.count, 1 - assert_equal message1.tags.first, tag - assert_equal message1.thread.tags.count, 1 - assert_equal message1.thread.tags.first, tag + assert_equal tag, message1.tags.first + assert_equal 1, message1.thread.tags.simple.count + assert_equal tag, message1.thread.tags.simple.first - assert_equal message2.tags.count, 1 - assert_equal message2.tags.first, tag - assert_equal message2.thread.tags.count, 1 - assert_equal message2.thread.tags.first, tag + assert_equal 1, message2.tags.simple.count + assert_equal tag, message2.tags.simple.first + assert_equal 1, message2.thread.tags.simple.count + assert_equal tag, message2.thread.tags.simple.first end test "should not use delivery notification title for message thread title" do diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb index d702f58b0..b4abcd82f 100644 --- a/test/models/tag_test.rb +++ b/test/models/tag_test.rb @@ -1,10 +1,4 @@ require "test_helper" class TagTest < ActiveSupport::TestCase - test "should mark tag readable by admin groups" do - tenant = tenants(:ssd) - tag = SimpleTag.create(name: 'New tag', tenant: tenant) - - assert tag.groups == [tenant.admin_group] - end end