diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 4490d1fbf..14b0240e6 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb index bad321ad0..90bef0eb2 100644 --- a/app/models/bubble/taggable.rb +++ b/app/models/bubble/taggable.rb @@ -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 diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb new file mode 100644 index 000000000..2ece908e5 --- /dev/null +++ b/test/controllers/tags_controller_test.rb @@ -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 +