Encapsulate tagging as a taggable domain method
Tag scope control should not float around in the controller
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user