From a58fcd31239da089c98f68db705a2dc68acf7de0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 6 Apr 2025 13:26:10 +0200 Subject: [PATCH] Nicer API --- app/models/bubble/taggable.rb | 14 +++++--------- app/views/bubbles/tags/_listbox_option.html.erb | 5 +---- .../bubbles/taggings_controller_test.rb | 4 ++-- test/models/bubble_test.rb | 10 +++++----- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb index 9edf66708..91e039ccb 100644 --- a/app/models/bubble/taggable.rb +++ b/app/models/bubble/taggable.rb @@ -10,24 +10,20 @@ module Bubble::Taggable def toggle_tag(title) transaction do - tag = find_or_create_tag_with_title(title) - tagged_with?(title) ? untagging(tag) : tagging(tag) + tag = find_or_create_tag_by_title(title) + tagged_with?(tag) ? untagging(tag) : tagging(tag) end end - def tagged_with?(title) - tags.exists?(title: title) + def tagged_with?(tag) + tags.include?(tag) end private - def find_or_create_tag_with_title(title) + def find_or_create_tag_by_title(title) bucket.account.tags.find_or_create_by!(title: title) end - def find_tag_by_title(title) - bucket.account.tags.find_by(title: title) - end - def tagging(tag) taggings.create tag: tag end diff --git a/app/views/bubbles/tags/_listbox_option.html.erb b/app/views/bubbles/tags/_listbox_option.html.erb index cea797bf3..712b94022 100644 --- a/app/views/bubbles/tags/_listbox_option.html.erb +++ b/app/views/bubbles/tags/_listbox_option.html.erb @@ -1,7 +1,4 @@
<%= tag.hashtag %> - - <% if bubble.tagged_with?(tag) %> - <%= icon_tag "check" %> - <% end %> + <%= icon_tag "check" if bubble.tagged_with?(tag) %>
diff --git a/test/controllers/bubbles/taggings_controller_test.rb b/test/controllers/bubbles/taggings_controller_test.rb index 2f52c4e1b..2f12b18b4 100644 --- a/test/controllers/bubbles/taggings_controller_test.rb +++ b/test/controllers/bubbles/taggings_controller_test.rb @@ -11,14 +11,14 @@ class Bubbles::TaggingsControllerTest < ActionDispatch::IntegrationTest end test "toggle tag on" do - assert_changes "bubbles(:logo).tagged_with?(tags(:mobile).title)", from: false, to: true do + assert_changes "bubbles(:logo).tagged_with?(tags(:mobile))", 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 test "toggle tag off" do - assert_changes "bubbles(:logo).tagged_with?(tags(:web).title)", from: true, to: false do + assert_changes "bubbles(:logo).tagged_with?(tags(:web))", from: true, to: false do post bubble_taggings_url(bubbles(:logo)), params: { tag_title: tags(:web).title }, as: :turbo_stream end assert_response :success diff --git a/test/models/bubble_test.rb b/test/models/bubble_test.rb index 27a42e846..63f6a0753 100644 --- a/test/models/bubble_test.rb +++ b/test/models/bubble_test.rb @@ -45,22 +45,22 @@ class BubbleTest < ActiveSupport::TestCase end test "tagged states" do - assert bubbles(:logo).tagged_with?(tags(:web).title) - assert_not bubbles(:logo).tagged_with?(tags(:mobile).title) + assert bubbles(:logo).tagged_with?(tags(:web)) + assert_not bubbles(:logo).tagged_with?(tags(:mobile)) end test "tag toggling" do - assert bubbles(:logo).tagged_with?(tags(:web).title) + assert bubbles(:logo).tagged_with?(tags(:web)) assert_difference "bubbles(:logo).taggings.count", -1 do bubbles(:logo).toggle_tag tags(:web).title end - assert_not bubbles(:logo).tagged_with?(tags(:web).title) + assert_not bubbles(:logo).tagged_with?(tags(:web)) assert_difference "bubbles(:logo).taggings.count", +1 do bubbles(:logo).toggle_tag tags(:web).title end - assert bubbles(:logo).tagged_with?(tags(:web).title) + assert bubbles(:logo).tagged_with?(tags(:web)) assert_difference %w[ bubbles(:logo).taggings.count accounts("37s").tags.count ], +1 do bubbles(:logo).toggle_tag "prioritized"