From 40863453e04f2489ce0361094f16fd6d523b8214 Mon Sep 17 00:00:00 2001 From: Jeffrey Hardy Date: Thu, 5 Sep 2024 16:50:32 -0400 Subject: [PATCH] Recast categories as tags --- app/assets/stylesheets/bubble.css | 4 +- app/controllers/bubbles_controller.rb | 8 ++-- app/controllers/categories_controller.rb | 39 ------------------- app/controllers/tags_controller.rb | 34 ++++++++++++++++ app/models/bubble.rb | 4 +- app/models/categorization.rb | 4 -- app/models/category.rb | 4 -- app/models/tag.rb | 8 ++++ app/models/tagging.rb | 4 ++ app/views/bubbles/_bubble.html.erb | 14 +++---- app/views/bubbles/_form.html.erb | 11 +++--- app/views/bubbles/index.html.erb | 8 ++-- app/views/categories/index.html.erb | 20 ---------- app/views/categories/new.html.erb | 12 ------ app/views/sessions/new.html.erb | 4 +- app/views/tags/index.html.erb | 21 ++++++++++ app/views/tags/new.html.erb | 12 ++++++ config/routes.rb | 4 +- ...0240905191233_rename_categories_to_tags.rb | 8 ++++ db/schema.rb | 36 ++++++++--------- .../{categorizations.yml => taggings.yml} | 8 ++-- test/fixtures/{categories.yml => tags.yml} | 0 test/models/categorization_test.rb | 7 ---- test/models/{category_test.rb => tag_test.rb} | 2 +- 24 files changed, 139 insertions(+), 137 deletions(-) delete mode 100644 app/controllers/categories_controller.rb create mode 100644 app/controllers/tags_controller.rb delete mode 100644 app/models/categorization.rb delete mode 100644 app/models/category.rb create mode 100644 app/models/tag.rb create mode 100644 app/models/tagging.rb delete mode 100644 app/views/categories/index.html.erb delete mode 100644 app/views/categories/new.html.erb create mode 100644 app/views/tags/index.html.erb create mode 100644 app/views/tags/new.html.erb create mode 100644 db/migrate/20240905191233_rename_categories_to_tags.rb rename test/fixtures/{categorizations.yml => taggings.yml} (73%) rename test/fixtures/{categories.yml => tags.yml} (100%) delete mode 100644 test/models/categorization_test.rb rename test/models/{category_test.rb => tag_test.rb} (63%) diff --git a/app/assets/stylesheets/bubble.css b/app/assets/stylesheets/bubble.css index b29a78c7e..83f380174 100644 --- a/app/assets/stylesheets/bubble.css +++ b/app/assets/stylesheets/bubble.css @@ -98,7 +98,7 @@ } } - &.bubble__category { + &.bubble__tag { inset: auto auto 10cqi -3cqi; + & { @@ -109,7 +109,7 @@ } } - .windshield &:has(.bubble__category--new) { + .windshield &:has(.bubble__tag--new) { display: none; } } diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index 3cb25f5fd..796e4c03a 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -2,9 +2,9 @@ class BubblesController < ApplicationController before_action :set_bubble, only: %i[ show edit update ] def index - if params[:category_id] - @category = Category.find(params[:category_id]) - @bubbles = @category.bubbles + if params[:tag_id] + @tag = Tag.find(params[:tag_id]) + @bubbles = @tag.bubbles else @bubbles = Bubble.all end @@ -38,6 +38,6 @@ class BubblesController < ApplicationController end def bubble_params - params.require(:bubble).permit(:title, :body, :color, :image, category_ids: []) + params.require(:bubble).permit(:title, :body, :color, :image, tag_ids: []) end end diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb deleted file mode 100644 index 379e625ea..000000000 --- a/app/controllers/categories_controller.rb +++ /dev/null @@ -1,39 +0,0 @@ -class CategoriesController < ApplicationController - before_action :set_category, only: :destroy - before_action :set_bubble, only: %i[ new create ] - - def index - @categories = Category.all - end - - def new - @category = Category.new - end - - def create - @category = Category.find_or_create_by(category_params) - @category.save - - @category.bubbles << @bubble - redirect_to bubble_path(@bubble) - end - - def destroy - @category.destroy - redirect_to categories_path - end - - private - - def category_params - params.require(:category).permit(:title) - end - - def set_category - @category = Category.find(params[:id]) - end - - def set_bubble - @bubble = Bubble.find(params[:bubble_id]) - end -end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 000000000..95402f7e8 --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -0,0 +1,34 @@ +class TagsController < ApplicationController + before_action :set_bubble, only: %i[ new create ] + before_action :set_tag, only: :destroy + + def index + @tags = Tag.all + end + + def new + end + + def create + @bubble.tags << Tag.find_or_create_by!(tag_params) + redirect_to @bubble + end + + def destroy + @tag.destroy + redirect_to tags_path + end + + private + def tag_params + params.require(:tag).permit(:title) + end + + def set_tag + @tag = Tag.find(params[:id]) + end + + def set_bubble + @bubble = Bubble.find(params[:bubble_id]) + end +end diff --git a/app/models/bubble.rb b/app/models/bubble.rb index 98cdbcce8..6f80f2722 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -4,8 +4,8 @@ class Bubble < ApplicationRecord has_many :comments, dependent: :destroy has_many :boosts, dependent: :destroy - has_many :categorizations - has_many :categories, through: :categorizations, dependent: :destroy + has_many :taggings, dependent: :destroy + has_many :tags, through: :taggings has_one_attached :image, dependent: :purge_later diff --git a/app/models/categorization.rb b/app/models/categorization.rb deleted file mode 100644 index dba46484c..000000000 --- a/app/models/categorization.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Categorization < ApplicationRecord - belongs_to :bubble - belongs_to :category -end diff --git a/app/models/category.rb b/app/models/category.rb deleted file mode 100644 index 09f1548c5..000000000 --- a/app/models/category.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Category < ApplicationRecord - has_many :categorizations, dependent: :destroy - has_many :bubbles, through: :categorizations -end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 000000000..434e44828 --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,8 @@ +class Tag < ApplicationRecord + #belongs_to :account + + has_many :taggings, dependent: :destroy + has_many :bubbles, through: :taggings + + normalizes :title, with: ->(value) { value.to_s } +end diff --git a/app/models/tagging.rb b/app/models/tagging.rb new file mode 100644 index 000000000..242dd4825 --- /dev/null +++ b/app/models/tagging.rb @@ -0,0 +1,4 @@ +class Tagging < ApplicationRecord + belongs_to :tag + belongs_to :bubble, touch: true +end diff --git a/app/views/bubbles/_bubble.html.erb b/app/views/bubbles/_bubble.html.erb index f769ece8a..8d14747a5 100644 --- a/app/views/bubbles/_bubble.html.erb +++ b/app/views/bubbles/_bubble.html.erb @@ -15,15 +15,15 @@ - <% bubble.categories.each do | category | %> - <%= link_to "##{category.title}", bubbles_path(category_id: category.id), class: "bubble__bubble bubble__meta bubble__category" %> + <% bubble.tags.each do |tag| %> + <%= link_to "##{tag.title}", bubbles_path(tag_id: tag), class: "bubble__bubble bubble__meta bubble__tag" %> <% end %> - <% if bubble.categories.count < 3 %> - - - <%= link_to "#", new_bubble_category_path(bubble), class: "bubble__category--new" %> - + <% if bubble.tags.size < 3 %> + + <%= turbo_frame_tag :new_tag do %> + <%= link_to "#", new_bubble_tag_path(bubble), class: "bubble__tag--new" %> + <% end %> <% end %> diff --git a/app/views/bubbles/_form.html.erb b/app/views/bubbles/_form.html.erb index e966ea6a7..1d56bd36a 100644 --- a/app/views/bubbles/_form.html.erb +++ b/app/views/bubbles/_form.html.erb @@ -18,16 +18,16 @@
- Category + Tag
- <%= collection_check_boxes(:bubble, :category_ids, Category.all, :id, :title) do | category | %> - <%= category.label(class: "btn txt-medium") { category.check_box + tag.span(category.text) } %> + <%= collection_check_boxes(:bubble, :tag_ids, Tag.all, :id, :title) do |f| %> + <%= f.label(class: "btn txt-medium") { f.checkbox + tag.span(f.text) } %> <% end %>
@@ -36,7 +36,8 @@ Attachment diff --git a/app/views/bubbles/index.html.erb b/app/views/bubbles/index.html.erb index c973553e9..758874571 100644 --- a/app/views/bubbles/index.html.erb +++ b/app/views/bubbles/index.html.erb @@ -8,8 +8,8 @@

Bubbled up - <% if @category %> - in <%= @category.title %> + <% if @tag %> + in <%= @tag.title %> <%= link_to bubbles_path, class: "btn txt-small" do %> <%= image_tag "remove.svg", aria: { hidden: true }, size: 24 %> @@ -40,8 +40,8 @@ <%= bubble.title %> <% end %> - <% bubble.categories.each do |category| %> - <%= link_to "# #{ category.title }", bubbles_path(category_id: category.id), style: "color: #{ bubble.color }" %> + <% bubble.tags.each do |tag| %> + <%= link_to "##{tag.title}", bubbles_path(tag_id: tag.id), style: "color: #{ bubble.color }" %> <% end %> diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb deleted file mode 100644 index e3f9fc514..000000000 --- a/app/views/categories/index.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -
-

All categories

-
    - <% @categories.each do | category | %> -
  • - - <%= category.title %> - <%= category.id %> - - <%= category.bubbles.collect { |bubble| bubble.title }.to_sentence%> - <%= button_to category_path(category), - method: :delete, data: { turbo_confirm: "Are you sure you want to delete this category?" }, - class: "btn txt-small" do %> - <%= image_tag "remove.svg", aria: { hidden: true }, size: 24 %> - Delete - <% end %> -
  • - <% end %> -
-
diff --git a/app/views/categories/new.html.erb b/app/views/categories/new.html.erb deleted file mode 100644 index 5f5472198..000000000 --- a/app/views/categories/new.html.erb +++ /dev/null @@ -1,12 +0,0 @@ - - <%= form_with model: [@bubble, @category], data: { turbo_frame: "_top" } do |form| %> - <%= form.text_field :title, class: "input borderless", autofocus: "on", list: "categories-list" %> - <%= form.submit "Create Category", hidden: true %> - - - <%= Category.all.each do | category | %> - - <% end %> - - <% end %> - diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 8c31b7eb6..7bf3e4c96 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -5,8 +5,8 @@
-         -     +         +    

Fizzy

diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb new file mode 100644 index 000000000..d9a41c5ad --- /dev/null +++ b/app/views/tags/index.html.erb @@ -0,0 +1,21 @@ +
+

All tags

+
    + <% @tags.each do |tag| %> +
  • + + <%= tag.title %> + + + + <%= tag.bubbles.map(&:title).to_sentence %> + + + <%= button_to tag, class: "btn txt-small", method: :delete, data: { turbo_confirm: "Are you sure?" } do %> + <%= image_tag "remove.svg", aria: { hidden: true }, size: 24 %> + Delete + <% end %> +
  • + <% end %> +
+
diff --git a/app/views/tags/new.html.erb b/app/views/tags/new.html.erb new file mode 100644 index 000000000..3d6452307 --- /dev/null +++ b/app/views/tags/new.html.erb @@ -0,0 +1,12 @@ +<%= turbo_frame_tag :new_tag do %> + <%= form_with model: Tag.new, url: bubble_tags_path(@bubble), data: { turbo_frame: "_top" } do |form| %> + <%= form.text_field :title, class: "input borderless", autofocus: "on", list: "tags-list" %> + <%= form.submit "Create Tag", hidden: true %> + + + <%= Tag.all.each do |tag| %> + + <% end %> + + <% end %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 6a0fd78a3..b4b04abcb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,11 +5,11 @@ Rails.application.routes.draw do resources :bubbles do resources :boosts - resources :categories, shallow: true resources :comments + resources :tags, shallow: true end - resources :categories, only: :index + resources :tags, only: :index get "up", to: "rails/health#show", as: :rails_health_check end diff --git a/db/migrate/20240905191233_rename_categories_to_tags.rb b/db/migrate/20240905191233_rename_categories_to_tags.rb new file mode 100644 index 000000000..a294472e5 --- /dev/null +++ b/db/migrate/20240905191233_rename_categories_to_tags.rb @@ -0,0 +1,8 @@ +class RenameCategoriesToTags < ActiveRecord::Migration[8.0] + def change + rename_table :categories, :tags + rename_table :categorizations, :taggings + + rename_column :taggings, :category_id, :tag_id + end +end diff --git a/db/schema.rb b/db/schema.rb index c5ad86b64..fc9215339 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_09_04_180510) do +ActiveRecord::Schema[8.0].define(version: 2024_09_05_191233) do create_table "accounts", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false @@ -64,21 +64,6 @@ ActiveRecord::Schema[8.0].define(version: 2024_09_04_180510) do t.integer "creator_id", null: false end - create_table "categories", force: :cascade do |t| - t.string "title" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "categorizations", force: :cascade do |t| - t.integer "bubble_id", null: false - t.integer "category_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["bubble_id"], name: "index_categorizations_on_bubble_id" - t.index ["category_id"], name: "index_categorizations_on_category_id" - end - create_table "comments", force: :cascade do |t| t.text "body" t.integer "creator_id", null: false @@ -96,6 +81,21 @@ ActiveRecord::Schema[8.0].define(version: 2024_09_04_180510) do t.index ["user_id"], name: "index_sessions_on_user_id" end + create_table "taggings", force: :cascade do |t| + t.integer "bubble_id", null: false + t.integer "tag_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["bubble_id"], name: "index_taggings_on_bubble_id" + t.index ["tag_id"], name: "index_taggings_on_tag_id" + end + + create_table "tags", force: :cascade do |t| + t.string "title" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.integer "account_id", null: false t.string "name", null: false @@ -110,8 +110,8 @@ ActiveRecord::Schema[8.0].define(version: 2024_09_04_180510) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" - add_foreign_key "categorizations", "bubbles" - add_foreign_key "categorizations", "categories" add_foreign_key "sessions", "users" + add_foreign_key "taggings", "bubbles" + add_foreign_key "taggings", "tags" add_foreign_key "users", "accounts" end diff --git a/test/fixtures/categorizations.yml b/test/fixtures/taggings.yml similarity index 73% rename from test/fixtures/categorizations.yml rename to test/fixtures/taggings.yml index 273f9c725..74cb5f482 100644 --- a/test/fixtures/categorizations.yml +++ b/test/fixtures/taggings.yml @@ -2,16 +2,16 @@ one: bubble: one - category: one + tag: one two: bubble: two - category: two + tag: two three: bubble: three - category: one + tag: one four: bubble: three - category: two + tag: two diff --git a/test/fixtures/categories.yml b/test/fixtures/tags.yml similarity index 100% rename from test/fixtures/categories.yml rename to test/fixtures/tags.yml diff --git a/test/models/categorization_test.rb b/test/models/categorization_test.rb deleted file mode 100644 index 356053a9e..000000000 --- a/test/models/categorization_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "test_helper" - -class CategorizationTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/category_test.rb b/test/models/tag_test.rb similarity index 63% rename from test/models/category_test.rb rename to test/models/tag_test.rb index 869357c88..1846cdbcb 100644 --- a/test/models/category_test.rb +++ b/test/models/tag_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class CategoryTest < ActiveSupport::TestCase +class TagTest < ActiveSupport::TestCase # test "the truth" do # assert true # end