From 2681dd96b12dce344fcf45b4d4458e46eeb33d88 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Thu, 20 Jun 2024 08:15:09 -0400 Subject: [PATCH] Move categories to top-level namespace (#894) --- .../categories/deletions_controller.rb | 6 +- .../categories/dropdowns_controller.rb | 4 +- .../categories_controller.rb | 14 ++-- app/controllers/registrations_controller.rb | 2 +- app/helpers/categories_helper.rb | 7 ++ app/helpers/transactions/categories_helper.rb | 7 -- app/models/{transaction => }/category.rb | 4 +- app/models/family.rb | 2 +- app/models/import.rb | 2 +- app/models/transaction.rb | 2 +- .../transactions/_transaction.html.erb | 2 +- .../categories/_badge.html.erb | 0 .../categories/_form.html.erb | 2 +- .../categories/_menu.html.erb | 4 +- .../categories/_row.html.erb | 6 +- .../categories/deletions/new.html.erb | 4 +- .../categories/dropdowns/_row.html.erb | 6 +- .../categories/dropdowns/show.html.erb | 4 +- .../categories/edit.html.erb | 0 .../categories/index.html.erb | 4 +- .../categories/new.html.erb | 0 .../transactions/_transaction.html.erb | 2 +- app/views/settings/_nav.html.erb | 2 +- app/views/tags/index.html.erb | 2 +- app/views/transactions/_form.html.erb | 2 +- app/views/transactions/_header.html.erb | 2 +- app/views/transactions/_transaction.html.erb | 2 +- app/views/transactions/bulk_edit.html.erb | 2 +- .../transactions/merchants/index.html.erb | 2 +- .../filters/_category_filter.html.erb | 12 +-- app/views/transactions/show.html.erb | 2 +- config/locales/views/categories/en.yml | 43 +++++++++++ config/locales/views/transactions/en.yml | 41 ----------- config/routes.rb | 15 ++-- .../20240620114307_rename_categories_table.rb | 5 ++ db/schema.rb | 28 +++---- lib/tasks/demo_data.rake | 4 +- .../categories/deletions_controller_test.rb | 16 ++-- .../controllers/categories_controller_test.rb | 73 +++++++++++++++++++ .../registrations_controller_test.rb | 2 +- .../categories_controller_test.rb | 73 ------------------- .../transactions_controller_test.rb | 6 +- .../fixtures/{transaction => }/categories.yml | 0 .../models/{transaction => }/category_test.rb | 24 +++--- test/models/family_test.rb | 2 +- test/models/import_test.rb | 4 +- test/system/settings_test.rb | 2 +- test/system/transactions_test.rb | 2 +- 48 files changed, 229 insertions(+), 223 deletions(-) rename app/controllers/{transactions => }/categories/deletions_controller.rb (60%) rename app/controllers/{transactions => }/categories/dropdowns_controller.rb (77%) rename app/controllers/{transactions => }/categories_controller.rb (58%) create mode 100644 app/helpers/categories_helper.rb delete mode 100644 app/helpers/transactions/categories_helper.rb rename app/models/{transaction => }/category.rb (94%) rename app/views/{transactions => }/categories/_badge.html.erb (100%) rename app/views/{transactions => }/categories/_form.html.erb (96%) rename app/views/{transactions => }/categories/_menu.html.erb (67%) rename app/views/{transactions => }/categories/_row.html.erb (80%) rename app/views/{transactions => }/categories/deletions/new.html.erb (90%) rename app/views/{transactions => }/categories/dropdowns/_row.html.erb (86%) rename app/views/{transactions => }/categories/dropdowns/show.html.erb (91%) rename app/views/{transactions => }/categories/edit.html.erb (100%) rename app/views/{transactions => }/categories/index.html.erb (75%) rename app/views/{transactions => }/categories/new.html.erb (100%) create mode 100644 config/locales/views/categories/en.yml create mode 100644 db/migrate/20240620114307_rename_categories_table.rb rename test/controllers/{transactions => }/categories/deletions_controller_test.rb (58%) create mode 100644 test/controllers/categories_controller_test.rb delete mode 100644 test/controllers/transactions/categories_controller_test.rb rename test/fixtures/{transaction => }/categories.yml (100%) rename test/models/{transaction => }/category_test.rb (52%) diff --git a/app/controllers/transactions/categories/deletions_controller.rb b/app/controllers/categories/deletions_controller.rb similarity index 60% rename from app/controllers/transactions/categories/deletions_controller.rb rename to app/controllers/categories/deletions_controller.rb index 3e67a762..678823f2 100644 --- a/app/controllers/transactions/categories/deletions_controller.rb +++ b/app/controllers/categories/deletions_controller.rb @@ -1,4 +1,4 @@ -class Transactions::Categories::DeletionsController < ApplicationController +class Categories::DeletionsController < ApplicationController layout "with_sidebar" before_action :set_category @@ -15,12 +15,12 @@ class Transactions::Categories::DeletionsController < ApplicationController private def set_category - @category = Current.family.transaction_categories.find(params[:category_id]) + @category = Current.family.categories.find(params[:category_id]) end def set_replacement_category if params[:replacement_category_id].present? - @replacement_category = Current.family.transaction_categories.find(params[:replacement_category_id]) + @replacement_category = Current.family.categories.find(params[:replacement_category_id]) end end end diff --git a/app/controllers/transactions/categories/dropdowns_controller.rb b/app/controllers/categories/dropdowns_controller.rb similarity index 77% rename from app/controllers/transactions/categories/dropdowns_controller.rb rename to app/controllers/categories/dropdowns_controller.rb index 80654467..3da1bfe6 100644 --- a/app/controllers/transactions/categories/dropdowns_controller.rb +++ b/app/controllers/categories/dropdowns_controller.rb @@ -1,4 +1,4 @@ -class Transactions::Categories::DropdownsController < ApplicationController +class Categories::DropdownsController < ApplicationController before_action :set_from_params def show @@ -17,6 +17,6 @@ class Transactions::Categories::DropdownsController < ApplicationController end def categories_scope - Current.family.transaction_categories.alphabetically + Current.family.categories.alphabetically end end diff --git a/app/controllers/transactions/categories_controller.rb b/app/controllers/categories_controller.rb similarity index 58% rename from app/controllers/transactions/categories_controller.rb rename to app/controllers/categories_controller.rb index 99bcf6fc..dfbe63e4 100644 --- a/app/controllers/transactions/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -1,20 +1,20 @@ -class Transactions::CategoriesController < ApplicationController +class CategoriesController < ApplicationController layout "with_sidebar" before_action :set_category, only: %i[ edit update ] before_action :set_transaction, only: :create def index - @categories = Current.family.transaction_categories.alphabetically + @categories = Current.family.categories.alphabetically end def new - @category = Current.family.transaction_categories.new color: Transaction::Category::COLORS.sample + @category = Current.family.categories.new color: Category::COLORS.sample end def create - Transaction::Category.transaction do - category = Current.family.transaction_categories.create!(category_params) + Category.transaction do + category = Current.family.categories.create!(category_params) @transaction.update!(category_id: category.id) if @transaction end @@ -32,7 +32,7 @@ class Transactions::CategoriesController < ApplicationController private def set_category - @category = Current.family.transaction_categories.find(params[:id]) + @category = Current.family.categories.find(params[:id]) end def set_transaction @@ -42,6 +42,6 @@ class Transactions::CategoriesController < ApplicationController end def category_params - params.require(:transaction_category).permit(:name, :color) + params.require(:category).permit(:name, :color) end end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 714fbb5e..3a18477d 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -16,7 +16,7 @@ class RegistrationsController < ApplicationController @user.role = :admin if @user.save - Transaction::Category.create_default_categories(@user.family) + Category.create_default_categories(@user.family) login @user flash[:notice] = t(".success") redirect_to root_path diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb new file mode 100644 index 00000000..83bddbf3 --- /dev/null +++ b/app/helpers/categories_helper.rb @@ -0,0 +1,7 @@ +module CategoriesHelper + def null_category + Category.new \ + name: "Uncategorized", + color: Category::UNCATEGORIZED_COLOR + end +end diff --git a/app/helpers/transactions/categories_helper.rb b/app/helpers/transactions/categories_helper.rb deleted file mode 100644 index 19d77a23..00000000 --- a/app/helpers/transactions/categories_helper.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Transactions::CategoriesHelper - def null_category - Transaction::Category.new \ - name: "Uncategorized", - color: Transaction::Category::UNCATEGORIZED_COLOR - end -end diff --git a/app/models/transaction/category.rb b/app/models/category.rb similarity index 94% rename from app/models/transaction/category.rb rename to app/models/category.rb index 58955720..b846ca51 100644 --- a/app/models/transaction/category.rb +++ b/app/models/category.rb @@ -1,4 +1,4 @@ -class Transaction::Category < ApplicationRecord +class Category < ApplicationRecord has_many :transactions, dependent: :nullify belongs_to :family @@ -24,7 +24,7 @@ class Transaction::Category < ApplicationRecord ] def self.create_default_categories(family) - if family.transaction_categories.size > 0 + if family.categories.size > 0 raise ArgumentError, "Family already has some categories" end diff --git a/app/models/family.rb b/app/models/family.rb index d7ccafd9..a86980ff 100644 --- a/app/models/family.rb +++ b/app/models/family.rb @@ -5,7 +5,7 @@ class Family < ApplicationRecord has_many :institutions, dependent: :destroy has_many :transactions, through: :accounts has_many :imports, through: :accounts - has_many :transaction_categories, dependent: :destroy, class_name: "Transaction::Category" + has_many :categories, dependent: :destroy has_many :transaction_merchants, dependent: :destroy, class_name: "Transaction::Merchant" def snapshot(period = Period.all) diff --git a/app/models/import.rb b/app/models/import.rb index cbe93697..49fc4f96 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -124,7 +124,7 @@ class Import < ApplicationRecord tags << tag_cache[tag_string] ||= account.family.tags.find_or_initialize_by(name: tag_string) end - category = category_cache[category_name] ||= account.family.transaction_categories.find_or_initialize_by(name: category_name) if category_name.present? + category = category_cache[category_name] ||= account.family.categories.find_or_initialize_by(name: category_name) if category_name.present? txn = account.transactions.build \ name: row["name"].presence || FALLBACK_TRANSACTION_NAME, diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 1d6b304e..7f534839 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -18,7 +18,7 @@ class Transaction < ApplicationRecord scope :inflows, -> { where("amount <= 0") } scope :outflows, -> { where("amount > 0") } scope :by_name, ->(name) { where("transactions.name ILIKE ?", "%#{name}%") } - scope :with_categories, ->(categories) { joins(:category).where(transaction_categories: { name: categories }) } + scope :with_categories, ->(categories) { joins(:category).where(categories: { name: categories }) } scope :with_accounts, ->(accounts) { joins(:account).where(accounts: { name: accounts }) } scope :with_account_ids, ->(account_ids) { joins(:account).where(accounts: { id: account_ids }) } scope :with_merchants, ->(merchants) { joins(:merchant).where(transaction_merchants: { name: merchants }) } diff --git a/app/views/accounts/transactions/_transaction.html.erb b/app/views/accounts/transactions/_transaction.html.erb index 5adc5817..e725c818 100644 --- a/app/views/accounts/transactions/_transaction.html.erb +++ b/app/views/accounts/transactions/_transaction.html.erb @@ -10,7 +10,7 @@

Transfer

<% else %> - <%= render "transactions/categories/badge", category: transaction.category %> + <%= render "categories/badge", category: transaction.category %> <% end %> diff --git a/app/views/transactions/categories/_badge.html.erb b/app/views/categories/_badge.html.erb similarity index 100% rename from app/views/transactions/categories/_badge.html.erb rename to app/views/categories/_badge.html.erb diff --git a/app/views/transactions/categories/_form.html.erb b/app/views/categories/_form.html.erb similarity index 96% rename from app/views/transactions/categories/_form.html.erb rename to app/views/categories/_form.html.erb index 0e12b4e7..b5fba349 100644 --- a/app/views/transactions/categories/_form.html.erb +++ b/app/views/categories/_form.html.erb @@ -14,7 +14,7 @@ <%= form.hidden_field :color, data: { color_select_target: "input" } %>