diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index ffe890a1..ed8f579d 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -94,7 +94,7 @@ class TransactionsController < ApplicationController def search_params params.fetch(:q, {}) - .permit(:start_date, :end_date, :search, :amount, :amount_operator, accounts: [], account_ids: [], categories: [], merchants: [], types: []) + .permit(:start_date, :end_date, :search, :amount, :amount_operator, accounts: [], account_ids: [], categories: [], merchants: [], types: [], tags: []) end def transaction_entry_params diff --git a/app/helpers/transactions_helper.rb b/app/helpers/transactions_helper.rb index 23bbede6..5c6f4d7b 100644 --- a/app/helpers/transactions_helper.rb +++ b/app/helpers/transactions_helper.rb @@ -3,9 +3,10 @@ module TransactionsHelper [ { key: "account_filter", icon: "layers" }, { key: "date_filter", icon: "calendar" }, - { key: "type_filter", icon: "shapes" }, + { key: "type_filter", icon: "tag" }, { key: "amount_filter", icon: "hash" }, - { key: "category_filter", icon: "tag" }, + { key: "category_filter", icon: "shapes" }, + { key: "tag_filter", icon: "tags" }, { key: "merchant_filter", icon: "store" } ] end diff --git a/app/models/account/transaction.rb b/app/models/account/transaction.rb index e8e83d2d..bf08ef0c 100644 --- a/app/models/account/transaction.rb +++ b/app/models/account/transaction.rb @@ -13,8 +13,15 @@ class Account::Transaction < ApplicationRecord class << self def search(params) query = all - query = query.joins("LEFT JOIN categories ON categories.id = account_transactions.category_id").where(categories: { name: params[:categories] }) if params[:categories].present? - query = query.joins("LEFT JOIN merchants ON merchants.id = account_transactions.merchant_id").where(merchants: { name: params[:merchants] }) if params[:merchants].present? + query = query.joins(:category).where(categories: { name: params[:categories] }) if params[:categories].present? + query = query.joins(:merchant).where(merchants: { name: params[:merchants] }) if params[:merchants].present? + + if params[:tags].present? + query = query.joins(:tags) + .where(tags: { name: params[:tags] }) + .distinct + end + query end @@ -25,7 +32,7 @@ class Account::Transaction < ApplicationRecord private def searchable_keys - %i[categories merchants] + %i[categories merchants tags] end end diff --git a/app/views/transactions/searches/filters/_tag_filter.html.erb b/app/views/transactions/searches/filters/_tag_filter.html.erb new file mode 100644 index 00000000..af86283a --- /dev/null +++ b/app/views/transactions/searches/filters/_tag_filter.html.erb @@ -0,0 +1,25 @@ +<%# locals: (form:) %> +