diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb index c296e0ff5..7fa4dee07 100644 --- a/app/models/bubble/taggable.rb +++ b/app/models/bubble/taggable.rb @@ -9,7 +9,7 @@ module Bubble::Taggable end def toggle_tag_with(title) - tag = find_or_create_tag_by_title(title) + tag = bucket.account.tags.find_or_create_by!(title: title) transaction do if tagged_with?(tag) @@ -23,9 +23,4 @@ module Bubble::Taggable def tagged_with?(tag) tags.include? tag end - - private - def find_or_create_tag_by_title(title) - bucket.account.tags.find_by("lower(title) = ?", title.downcase) || bucket.account.tags.create!(title: title) - end end diff --git a/app/models/tag.rb b/app/models/tag.rb index 742dfcfea..376e21bc0 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -7,6 +7,7 @@ class Tag < ApplicationRecord has_many :bubbles, through: :taggings validates :title, format: { without: /\A#/ } + normalizes :title, with: -> { it.downcase } scope :alphabetically, -> { order("lower(title)") } diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml index 76fedd9a6..d6dbcf51c 100644 --- a/test/fixtures/tags.yml +++ b/test/fixtures/tags.yml @@ -1,7 +1,7 @@ web: account: 37s - title: Web + title: web mobile: account: 37s - title: Mobile + title: mobile diff --git a/test/models/filter_test.rb b/test/models/filter_test.rb index 16b78eddd..1cf117dc4 100644 --- a/test/models/filter_test.rb +++ b/test/models/filter_test.rb @@ -104,10 +104,10 @@ class FilterTest < ActiveSupport::TestCase end test "summary" do - assert_equal "Most discussed, tagged #Mobile, and assigned to JZ ", filters(:jz_assignments).summary + assert_equal "Most discussed, tagged #mobile, and assigned to JZ ", filters(:jz_assignments).summary filters(:jz_assignments).update!(stages: workflow_stages(:qa_triage, :qa_in_progress)) - assert_equal "Most discussed, tagged #Mobile, assigned to JZ, and staged in Triage or In progress ", filters(:jz_assignments).summary + assert_equal "Most discussed, tagged #mobile, assigned to JZ, and staged in Triage or In progress ", filters(:jz_assignments).summary filters(:jz_assignments).update!(stages: [], assignees: [], tags: [], buckets: [ buckets(:writebook) ]) assert_equal "Most discussed in Writebook", filters(:jz_assignments).summary diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb index 554056941..7fe171e3f 100644 --- a/test/models/tag_test.rb +++ b/test/models/tag_test.rb @@ -1,15 +1,21 @@ require "test_helper" class TagTest < ActiveSupport::TestCase - test "creating or deleting a tag touches the account, so tags dialog fragment cache is invalidated" do - account = accounts("37s") + setup do + @account = accounts("37s") + end - assert_changes -> { account.reload.updated_at } do - account.tags.create!(title: "ReleaseBlocker") + test "creating or deleting a tag touches the account, so tags dialog fragment cache is invalidated" do + assert_changes -> { @account.reload.updated_at } do + @account.tags.create!(title: "ReleaseBlocker") end - assert_changes -> { account.reload.updated_at } do - account.tags.find_by(title: "ReleaseBlocker").destroy + assert_changes -> { @account.reload.updated_at } do + @account.tags.find_by(title: "ReleaseBlocker").destroy end end + + test "downcase title" do + assert_equal "a tag", @account.tags.create!(title: "A TAG").title + end end