diff --git a/app/controllers/bubbles/taggings_controller.rb b/app/controllers/bubbles/taggings_controller.rb index 4672898af..5dc971773 100644 --- a/app/controllers/bubbles/taggings_controller.rb +++ b/app/controllers/bubbles/taggings_controller.rb @@ -2,34 +2,15 @@ class Bubbles::TaggingsController < ApplicationController include BubbleScoped def new - render partial: "bubbles/tagging", locals: { bubble: @bubble, tags: Current.account.tags } + @tags = Current.account.tags.alphabetically end def create - if params[:tag_title].present? - sanitized_title = params[:tag_title].strip.gsub(/\A#/, "") - tag = Current.account.tags.find_or_create_by!(title: sanitized_title) - @bubble.toggle_tag(tag) - else - new_tag_ids = Array(params[:tag_id]) - current_tags = @bubble.tags - - current_tags.each do |tag| - @bubble.toggle_tag(tag) unless new_tag_ids.include?(tag.id.to_s) - end - - new_tag_ids.each do |id| - tag = Current.account.tags.find(id) - @bubble.toggle_tag(tag) unless current_tags.include?(tag) - end - end - - @bubble.tags.reload - - respond_to do |format| - format.turbo_stream do - render turbo_stream: turbo_stream.replace([ @bubble, :tags ], partial: "bubbles/cards/perma/tags", locals: { bubble: @bubble }) - end - end + @bubble.toggle_tag(tag_title_param) end + + private + def tag_title_param + params.required(:tag_title).strip.gsub(/\A#/, "") + end end diff --git a/app/models/bubble/taggable.rb b/app/models/bubble/taggable.rb index 6b6462e9b..14a762407 100644 --- a/app/models/bubble/taggable.rb +++ b/app/models/bubble/taggable.rb @@ -8,8 +8,11 @@ module Bubble::Taggable scope :tagged_with, ->(tags) { joins(:taggings).where(taggings: { tag: tags }) } end - def toggle_tag(tag) - tagged_with?(tag) ? untag(tag) : tag(tag) + def toggle_tag(title) + transaction do + tag = find_or_create_tag_with_title(title) + tagged_with?(tag) ? untag_with(tag) : tag_with(tag) + end end def tagged_with?(tag) @@ -17,13 +20,15 @@ module Bubble::Taggable end private - def tag(tag) - taggings.create! tag: tag - rescue ActiveRecord::RecordNotUnique - # Already tagged + def find_or_create_tag_with_title(title) + bucket.account.tags.find_or_create_by!(title: title) end - def untag(tag) + def tag_with(tag) + taggings.create tag: tag + end + + def untag_with(tag) taggings.destroy_by tag: tag end end diff --git a/app/models/tag.rb b/app/models/tag.rb index 51e27c30f..742dfcfea 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -8,6 +8,8 @@ class Tag < ApplicationRecord validates :title, format: { without: /\A#/ } + scope :alphabetically, -> { order("lower(title)") } + def hashtag "#" + title end diff --git a/app/views/bubbles/_tagging.html.erb b/app/views/bubbles/_tagging.html.erb deleted file mode 100644 index b76c4bd3e..000000000 --- a/app/views/bubbles/_tagging.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -<% cache [ bubble, Current.account ] do %> - <%= turbo_frame_tag bubble, :tagging do %> - - Tag this… - - <%= form_with url: bubble_taggings_path(bubble), class: "flex gap-half align-center full-width pad-inline-half margin-block-end" do |form| %> - <%= form.text_field :tag_title, placeholder: "New tag name...", class: "input txt-small full-width" %> - <%= form.button "Add a tag…", type: "submit", class: "btn txt-small" do %> - <%= icon_tag "add" %> - Add a tag - <% end %> - <% end %> - - <%= form_with url: bubble_taggings_path(bubble), class: "flex flex-column full-width popup__list", data: { controller: "form" } do |form| %> - <% tags.sort_by { |tag| tag.hashtag.downcase }.each do |tag| %> - - <% end %> - <% end %> - - <% end %> -<% end %> diff --git a/app/views/bubbles/taggings/create.turbo_stream.erb b/app/views/bubbles/taggings/create.turbo_stream.erb new file mode 100644 index 000000000..b261c7f0b --- /dev/null +++ b/app/views/bubbles/taggings/create.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.replace([ @bubble, :tags ], partial: "bubbles/cards/perma/tags", locals: { bubble: @bubble }) %> diff --git a/app/views/bubbles/taggings/new.html.erb b/app/views/bubbles/taggings/new.html.erb new file mode 100644 index 000000000..c6272f9b3 --- /dev/null +++ b/app/views/bubbles/taggings/new.html.erb @@ -0,0 +1,24 @@ +<% cache [ @bubble, Current.account ] do %> + <%= turbo_frame_tag @bubble, :tagging do %> + + Tag this… + + <%= form_with url: bubble_taggings_path(@bubble), class: "flex gap-half align-center full-width pad-inline-half margin-block-end" do |form| %> + <%= form.text_field :tag_title, placeholder: "New tag name...", class: "input txt-small full-width" %> + <%= form.button "Add a tag…", type: "submit", class: "btn txt-small" do %> + <%= icon_tag "add" %> + Add a tag + <% end %> + <% end %> + +
+ <% @tags.each do |tag| %> + + <% end %> +
+
+ <% end %> +<% end %> diff --git a/test/models/bubble_test.rb b/test/models/bubble_test.rb index 6aed88b09..40473a3c4 100644 --- a/test/models/bubble_test.rb +++ b/test/models/bubble_test.rb @@ -50,20 +50,20 @@ class BubbleTest < ActiveSupport::TestCase end test "tag toggling" do - assert bubbles(:logo).tagged_with?(tags(:web)) + assert bubbles(:logo).tagged_with?(tags(:web).title) assert_difference "bubbles(:logo).taggings.count", -1 do - bubbles(:logo).toggle_tag tags(:web) + bubbles(:logo).toggle_tag tags(:web).title end - assert_not bubbles(:logo).tagged_with?(tags(:web)) + assert_not bubbles(:logo).tagged_with?(tags(:web).title) assert_difference "bubbles(:logo).taggings.count", +1 do - bubbles(:logo).toggle_tag tags(:web) + bubbles(:logo).toggle_tag tags(:web).title end - assert bubbles(:logo).tagged_with?(tags(:web)) + assert bubbles(:logo).tagged_with?(tags(:web).title) assert_difference %w[ bubbles(:logo).taggings.count accounts("37s").tags.count ], +1 do - bubbles(:logo).toggle_tag Tag.new(title: "prioritized") + bubbles(:logo).toggle_tag "prioritized" end assert_equal "prioritized", bubbles(:logo).taggings.last.tag.title end