Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Add tag filtering (#1240)

Zach Gollwitzere8d7ee327023
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:) %>
+<div data-controller="list-filter">
+ <div class="relative">
+ <input type="search" autocomplete="off" placeholder="Filter tags" data-list-filter-target="input" data-action="input->list-filter#filter" class="block w-full border border-gray-200 rounded-md py-2 pl-10 pr-3 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
+ <%= lucide_icon("search", class: "w-5 h-5 text-gray-500 absolute inset-y-0 left-2 top-1/2 transform -translate-y-1/2") %>
+ </div>
+ <div class="my-2" id="list" data-list-filter-target="list">
+ <% Current.family.tags.alphabetically.each do |tag| %>
+ <div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= tag.name %>">
+ <%= form.check_box :tags,
+ {
+ multiple: true,
+ checked: @q[:tags]&.include?(tag.name),
+ class: "maybe-checkbox maybe-checkbox--light"
+ },
+ tag.name,
+ nil %>
+ <%= form.label :tags, value: tag.name, class: "text-sm text-gray-900 flex items-center gap-2" do %>
+ <%= circle_logo(tag.name, hex: tag.color || Tag::UNCATEGORIZED_COLOR, size: "sm") %>
+ <%= tag.name %>
+ <% end %>
+ </div>
+ <% end %>
+ </div>
+</div>
diff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.yml
index 5089fad5..7b7189e8 100644
--- a/config/locales/views/transactions/en.yml
+++ b/config/locales/views/transactions/en.yml
@@ -70,6 +70,7 @@ en:
clear_filters: Clear filters
date_filter: Date
merchant_filter: Merchant
+ tag_filter: Tag
type_filter: Type
search:
equal_to: equal to