Nicer API

This commit is contained in:
David Heinemeier Hansson
2025-04-06 13:26:10 +02:00
parent cdd78b41ee
commit a58fcd3123
4 changed files with 13 additions and 20 deletions
+5 -9
View File
@@ -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
@@ -1,7 +1,4 @@
<div class="flex gap-half justify-space-between align-center" style="--btn-icon-size: 1rem">
<%= tag.hashtag %>
<% if bubble.tagged_with?(tag) %>
<%= icon_tag "check" %>
<% end %>
<%= icon_tag "check" if bubble.tagged_with?(tag) %>
</div>
@@ -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
+5 -5
View File
@@ -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"