Encapsulate tagging as a taggable domain method

Tag scope control should not float around in the controller
This commit is contained in:
David Heinemeier Hansson
2024-10-19 20:14:59 -07:00
parent c00dcd8c66
commit b1f5498914
3 changed files with 31 additions and 10 deletions
+2 -10
View File
@@ -11,21 +11,13 @@ class TagsController < ApplicationController
def new
end
# FIXME: Should move this to a taggings controller to separate tag administration from applying
def create
@bubble.tags << Current.account.tags.find_or_create_by!(tag_params)
@bubble.tag(params.require(:tag).expect(:title))
redirect_to @bubble
end
def destroy
Current.account.tags.find(params[:id]).destroy
redirect_to root_path
end
private
def tag_params
params.expect(tag: [ :title ])
end
def set_bubble
@bubble = @bucket.bubbles.find(params[:bubble_id])
end
+4
View File
@@ -7,4 +7,8 @@ module Bubble::Taggable
scope :tagged_with, ->(tags) { joins(:taggings).where(taggings: { tag: tags }) }
end
def tag(title)
taggings.create! tag: bucket.account.tags.find_or_create_by!(title: title)
end
end
+25
View File
@@ -0,0 +1,25 @@
require "test_helper"
class TagsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :kevin
end
test "create with existing tag" do
assert_no_difference -> { accounts(:"37s").tags.count } do
post bucket_bubble_tags_url(buckets(:writebook), bubbles(:logo)), params: { tag: { title: "Web" } }
end
assert_redirected_to bubbles(:logo)
end
test "create with new tag" do
assert_difference -> { accounts(:"37s").tags.count }, +1 do
post bucket_bubble_tags_url(buckets(:writebook), bubbles(:logo)), params: { tag: { title: "Horizons" } }
end
assert_redirected_to bubbles(:logo)
assert bubbles(:logo).tags.pluck(:title).include?("Horizons")
end
end