Skip to content

Commit

Permalink
Validate transaction filtering params
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkottnauer committed May 26, 2024
1 parent 8f35665 commit 5288819
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/transactions/merchants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Transactions::MerchantsController < ApplicationController
before_action :set_merchant, only: %i[ edit update destroy ]

def index
@merchants = Current.family.transaction_merchants
@merchants = Current.family.transaction_merchants.alphabetically
end

def new
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def index
income: result.inflows.sum(&:amount_money).abs,
expense: result.outflows.sum(&:amount_money).abs
}
@filter_list = Transaction.build_filter_list(search_params, Current.family)
@filter_list, valid_params = Transaction.build_filter_list(search_params, Current.family)
session[ransack_session_key] = valid_params

respond_to do |format|
format.html
Expand Down
27 changes: 20 additions & 7 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def self.ransackable_associations(auth_object = nil)

def self.build_filter_list(params, family)
filters = []
valid_params = {}

date_filters = { gteq: nil, lteq: nil }

Expand All @@ -74,23 +75,35 @@ def self.build_filter_list(params, family)

case key
when "account_id_in"
value.each do |account_id|
filters << { type: "account", value: family.accounts.find(account_id), original: { key: key, value: account_id } }
valid_accounts = value.select do |account_id|
account = family.accounts.find_by(id: account_id)
filters << { type: "account", value: account, original: { key: key, value: account_id } } if account.present?
account.present?
end
valid_params[key] = valid_accounts unless valid_accounts.empty?
when "category_id_in"
value.each do |category_id|
filters << { type: "category", value: family.transaction_categories.find(category_id), original: { key: key, value: category_id } }
valid_categories = value.select do |category_id|
category = family.transaction_categories.find_by(id: category_id)
filters << { type: "category", value: category, original: { key: key, value: category_id } } if category.present?
category.present?
end
valid_params[key] = valid_categories unless valid_categories.empty?
when "merchant_id_in"
value.each do |merchant_id|
filters << { type: "merchant", value: family.transaction_merchants.find(merchant_id), original: { key: key, value: merchant_id } }
valid_merchants = value.select do |merchant_id|
merchant = family.transaction_merchants.find_by(id: merchant_id)
filters << { type: "merchant", value: merchant, original: { key: key, value: merchant_id } } if merchant.present?
merchant.present?
end
valid_params[key] = valid_merchants unless valid_merchants.empty?
when "category_name_or_merchant_name_or_account_name_or_name_cont"
filters << { type: "search", value: value, original: { key: key, value: nil } }
valid_params[key] = value
when "date_gteq"
date_filters[:gteq] = value
valid_params[key] = value
when "date_lteq"
date_filters[:lteq] = value
valid_params[key] = value
end
end

Expand All @@ -99,6 +112,6 @@ def self.build_filter_list(params, family)
end
end

filters
[ filters, valid_params ]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= 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.accounts.each do |account| %>
<% Current.family.accounts.alphabetically.each do |account| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= account.name %>">
<%= form.check_box :account_id_in, { multiple: true, class: "rounded-sm border-gray-300 text-indigo-600 shadow-xs focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" }, account.id, nil %>
<%= form.label :account_id_in, account.name, value: account.id, class: "text-sm text-gray-900" %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= 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.transaction_categories.each do |transaction_category| %>
<% Current.family.transaction_categories.alphabetically.each do |transaction_category| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= transaction_category.name %>">
<%= form.check_box :category_id_in, { "data-auto-submit-form-target": "auto", multiple: true, class: "rounded-sm border-gray-300 text-indigo-600 shadow-xs focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" }, transaction_category.id, nil %>
<%= form.label :category_id_in, transaction_category.name, value: transaction_category.id, class: "text-sm text-gray-900 cursor-pointer" do %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= 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.transaction_merchants.each do |merchant| %>
<% Current.family.transaction_merchants.alphabetically.each do |merchant| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= merchant.name %>">
<%= form.check_box :merchant_id_in, { multiple: true, class: "rounded-sm border-gray-300 text-indigo-600 shadow-xs focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" }, merchant.id, nil %>
<%= form.label :merchant_id_in, merchant.name, value: merchant.id, class: "text-sm text-gray-900" %>
Expand Down

0 comments on commit 5288819

Please sign in to comment.