diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb index 14a762407..9edf66708 100644 --- a/app/models/bubble/taggable.rb +++ b/app/models/bubble/taggable.rb @@ -11,12 +11,12 @@ module Bubble::Taggable def toggle_tag(title) transaction do tag = find_or_create_tag_with_title(title) - tagged_with?(tag) ? untag_with(tag) : tag_with(tag) + tagged_with?(title) ? untagging(tag) : tagging(tag) end end - def tagged_with?(tag) - tags.include? tag + def tagged_with?(title) + tags.exists?(title: title) end private @@ -24,11 +24,15 @@ module Bubble::Taggable bucket.account.tags.find_or_create_by!(title: title) end - def tag_with(tag) + def find_tag_by_title(title) + bucket.account.tags.find_by(title: title) + end + + def tagging(tag) taggings.create tag: tag end - def untag_with(tag) + def untagging(tag) taggings.destroy_by tag: tag end end diff --git a/test/controllers/bubbles/taggings_controller_test.rb b/test/controllers/bubbles/taggings_controller_test.rb index 34a666fa3..2f52c4e1b 100644 --- a/test/controllers/bubbles/taggings_controller_test.rb +++ b/test/controllers/bubbles/taggings_controller_test.rb @@ -10,16 +10,16 @@ class Bubbles::TaggingsControllerTest < ActionDispatch::IntegrationTest assert_response :success end - test "create" do - assert_changes "bubbles(:logo).tagged_with?(tags(:mobile))", from: false, to: true do - post bubble_taggings_url(bubbles(:logo)), params: { tag_id: tags(:mobile).id }, as: :turbo_stream + test "toggle tag on" do + assert_changes "bubbles(:logo).tagged_with?(tags(:mobile).title)", from: false, to: true do + post bubble_taggings_url(bubbles(:logo)), params: { tag_title: tags(:mobile).title }, as: :turbo_stream end assert_response :success + end - assert_changes "bubbles(:logo).tagged_with?(tags(:web))", from: false, to: true do - assert_changes "bubbles(:logo).tagged_with?(tags(:mobile))", from: true, to: false do - post bubble_taggings_url(bubbles(:logo)), params: { tag_id: tags(:web).id }, as: :turbo_stream - end + test "toggle tag off" do + assert_changes "bubbles(:logo).tagged_with?(tags(:web).title)", from: true, to: false do + post bubble_taggings_url(bubbles(:logo)), params: { tag_title: tags(:web).title }, as: :turbo_stream end assert_response :success end diff --git a/test/models/bubble_test.rb b/test/models/bubble_test.rb index 40473a3c4..27a42e846 100644 --- a/test/models/bubble_test.rb +++ b/test/models/bubble_test.rb @@ -45,8 +45,8 @@ class BubbleTest < ActiveSupport::TestCase end test "tagged states" do - assert bubbles(:logo).tagged_with?(tags(:web)) - assert_not bubbles(:logo).tagged_with?(tags(:mobile)) + assert bubbles(:logo).tagged_with?(tags(:web).title) + assert_not bubbles(:logo).tagged_with?(tags(:mobile).title) end test "tag toggling" do