-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare entry search for nested categories
- Loading branch information
Showing
11 changed files
with
122 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class Account::EntrySearch | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :search, :string | ||
attribute :amount, :string | ||
attribute :amount_operator, :string | ||
attribute :types, :string | ||
attribute :accounts, :string | ||
attribute :account_ids, :string | ||
attribute :start_date, :string | ||
attribute :end_date, :string | ||
|
||
class << self | ||
def from_entryable_search(entryable_search) | ||
new(entryable_search.attributes.slice(*attribute_names)) | ||
end | ||
end | ||
|
||
def build_query(scope) | ||
query = scope | ||
|
||
query = query.where("account_entries.name ILIKE :search OR account_entries.enriched_name ILIKE :search", | ||
search: "%#{ActiveRecord::Base.sanitize_sql_like(search)}%" | ||
) if search.present? | ||
query = query.where("account_entries.date >= ?", start_date) if start_date.present? | ||
query = query.where("account_entries.date <= ?", end_date) if end_date.present? | ||
|
||
if types.present? | ||
query = query.where(marked_as_transfer: false) unless types.include?("transfer") | ||
|
||
if types.include?("income") && !types.include?("expense") | ||
query = query.where("account_entries.amount < 0") | ||
elsif types.include?("expense") && !types.include?("income") | ||
query = query.where("account_entries.amount >= 0") | ||
end | ||
end | ||
|
||
if amount.present? && amount_operator.present? | ||
case amount_operator | ||
when "equal" | ||
query = query.where("ABS(ABS(account_entries.amount) - ?) <= 0.01", amount.to_f.abs) | ||
when "less" | ||
query = query.where("ABS(account_entries.amount) < ?", amount.to_f.abs) | ||
when "greater" | ||
query = query.where("ABS(account_entries.amount) > ?", amount.to_f.abs) | ||
end | ||
end | ||
|
||
if accounts.present? || account_ids.present? | ||
query = query.joins(:account) | ||
end | ||
|
||
query = query.where(accounts: { name: accounts }) if accounts.present? | ||
query = query.where(accounts: { id: account_ids }) if account_ids.present? | ||
|
||
query | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Account::TransactionSearch | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :search, :string | ||
attribute :amount, :string | ||
attribute :amount_operator, :string | ||
attribute :types, array: true | ||
attribute :accounts, array: true | ||
attribute :account_ids, array: true | ||
attribute :start_date, :string | ||
attribute :end_date, :string | ||
attribute :categories, array: true | ||
attribute :merchants, array: true | ||
attribute :tags, array: true | ||
|
||
# Returns array of Account::Entry objects to stay consistent with partials, which only deal with Account::Entry | ||
def build_query(scope) | ||
query = scope | ||
|
||
if categories.present? | ||
if categories.exclude?("Uncategorized") | ||
query = query | ||
.joins(:category) | ||
.where(categories: { name: categories }) | ||
else | ||
query = query | ||
.left_joins(:category) | ||
.where(categories: { name: categories }) | ||
.or(query.where(category_id: nil)) | ||
end | ||
end | ||
|
||
query = query.joins(:merchant).where(merchants: { name: merchants }) if merchants.present? | ||
|
||
query = query.joins(:tags).where(tags: { name: tags }) if tags.present? | ||
|
||
entries_scope = Account::Entry.account_transactions.where(entryable_id: query.select(:id)) | ||
|
||
Account::EntrySearch.from_entryable_search(self).build_query(entries_scope) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
class Account::Valuation < ApplicationRecord | ||
include Account::Entryable | ||
|
||
class << self | ||
def search(_params) | ||
all | ||
end | ||
|
||
def requires_search?(_params) | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddParentCategory < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :categories, :parent_category_id, :uuid | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters