Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Support "not equal to" and "is not empty" for tag rule conditions (#4246)

alexcastgonze913d1d71da8
diff --git a/app/models/assistant/function/get_rule_options.rb b/app/models/assistant/function/get_rule_options.rb
index 6bb3e97dec..36370572cb 100644
--- a/app/models/assistant/function/get_rule_options.rb
+++ b/app/models/assistant/function/get_rule_options.rb
@@ -49,17 +49,11 @@ class Assistant::Function::GetRuleOptions < Assistant::Function
condition_type: filter.key,
label: filter.label,
value_type: filter.type,
- operators: filter_operators(filter).map { |label, value| { value: value, label: label } },
+ operators: filter.operators.map { |label, value| { value: value, label: label } },
values: value_spec(filter.key)
}.compact
end
- # The tag filter lists the generic select operators but only applies these.
- def filter_operators(filter)
- ops = filter.operators
- filter.key == "transaction_tag" ? ops.select { |_, v| %w[= is_null].include?(v) } : ops
- end
-
def compound_option
{
condition_type: "compound",
diff --git a/app/models/rule/condition_filter/transaction_tag.rb b/app/models/rule/condition_filter/transaction_tag.rb
index 89c635bd6a..202cb3bc4b 100644
--- a/app/models/rule/condition_filter/transaction_tag.rb
+++ b/app/models/rule/condition_filter/transaction_tag.rb
@@ -22,8 +22,12 @@ class Rule::ConditionFilter::TransactionTag < Rule::ConditionFilter
case operator
when "="
scope.where(tagging_exists(tag_id: value))
+ when "!="
+ scope.where(Arel::Nodes::Not.new(tagging_exists(tag_id: value)))
when "is_null"
scope.where(Arel::Nodes::Not.new(tagging_exists))
+ when "is_not_null"
+ scope.where(tagging_exists)
else
raise UnsupportedOperatorError, "Unsupported operator: #{operator} for type: #{type}"
end
@@ -31,8 +35,8 @@ class Rule::ConditionFilter::TransactionTag < Rule::ConditionFilter
private
# Builds an Arel EXISTS node correlating taggings to the outer transactions
- # row. When +tag_id+ is given it matches that specific tag; otherwise it
- # matches any tag (negated for the "is_null" / has-no-tags operator).
+ # row. When +tag_id+ is given it matches that specific tag (negated for "!=");
+ # otherwise it matches any tag (negated for the "is_null" / has-no-tags operator).
def tagging_exists(tag_id: nil)
taggings = Tagging
.where(Tagging.arel_table[:taggable_id].eq(Transaction.arel_table[:id]))
diff --git a/test/models/assistant/function/get_rule_options_test.rb b/test/models/assistant/function/get_rule_options_test.rb
index 15040dca3d..0b4abecd35 100644
--- a/test/models/assistant/function/get_rule_options_test.rb
+++ b/test/models/assistant/function/get_rule_options_test.rb
@@ -17,7 +17,7 @@ class Assistant::Function::GetRuleOptionsTest < ActiveSupport::TestCase
assert_includes amount[:operators].map { |o| o[:value] }, "="
tag = result[:conditions].find { |c| c[:condition_type] == "transaction_tag" }
- assert_equal %w[= is_null], tag[:operators].map { |o| o[:value] }
+ assert_equal %w[= != is_null is_not_null].sort, tag[:operators].map { |o| o[:value] }.sort
action_types = result[:actions].map { |a| a[:action_type] }
assert_includes action_types, "set_transaction_category"
diff --git a/test/models/assistant/function/preview_rule_test.rb b/test/models/assistant/function/preview_rule_test.rb
index 2d96f4c6ac..704dc514f1 100644
--- a/test/models/assistant/function/preview_rule_test.rb
+++ b/test/models/assistant/function/preview_rule_test.rb
@@ -85,12 +85,20 @@ class Assistant::Function::PreviewRuleTest < ActiveSupport::TestCase
assert_match "must be a number", amount[:problems].first
end
- test "rejects operators a filter lists but cannot apply" do
- result = @fn.call(definition(conditions: [ { "condition_type" => "transaction_tag", "operator" => "!=", "value" => tags(:one).id } ]))
+ test "rejects operators a filter does not list" do
+ result = @fn.call(definition(conditions: [ { "condition_type" => "transaction_tag", "operator" => ">", "value" => tags(:one).id } ]))
assert_equal "invalid_rule", result[:error]
end
+ test "accepts every operator the tag filter lists" do
+ %w[!= is_not_null].each do |operator|
+ result = @fn.call(definition(conditions: [ { "condition_type" => "transaction_tag", "operator" => operator, "value" => tags(:one).id } ]))
+
+ assert_nil result[:error], "#{operator}: #{result.inspect}"
+ end
+ end
+
test "requires exactly one of rule_id or a definition" do
assert_equal "invalid_arguments", @fn.call({})[:error]
diff --git a/test/models/rule/condition_test.rb b/test/models/rule/condition_test.rb
index cf1e568188..b82a6ba112 100644
--- a/test/models/rule/condition_test.rb
+++ b/test/models/rule/condition_test.rb
@@ -319,6 +319,52 @@ class Rule::ConditionTest < ActiveSupport::TestCase
assert filtered.none? { |t| t.tags.include?(tag) }
end
+ test "applies != condition for transaction_tag" do
+ scope = @rule_scope
+
+ tag = @family.tags.create!(name: "Reimbursable")
+ other_tag = @family.tags.create!(name: "Business")
+ @account.transactions.first.tags << tag
+ @account.transactions.second.tags << other_tag
+
+ condition = Rule::Condition.new(
+ rule: @transaction_rule,
+ condition_type: "transaction_tag",
+ operator: "!=",
+ value: tag.id
+ )
+
+ scope = condition.prepare(scope)
+ filtered = condition.apply(scope)
+
+ # Every transaction except the one carrying the tag, including untagged ones
+ assert_equal 4, filtered.count
+ assert filtered.none? { |t| t.tags.include?(tag) }
+ end
+
+ test "applies is_not_null condition for transaction_tag" do
+ scope = @rule_scope
+
+ tag = @family.tags.create!(name: "Reimbursable")
+ other_tag = @family.tags.create!(name: "Business")
+ @account.transactions.first.tags << [ tag, other_tag ]
+ @account.transactions.second.tags << other_tag
+
+ condition = Rule::Condition.new(
+ rule: @transaction_rule,
+ condition_type: "transaction_tag",
+ operator: "is_not_null",
+ value: nil
+ )
+
+ scope = condition.prepare(scope)
+ filtered = condition.apply(scope)
+
+ # The 2 tagged transactions, once each
+ assert_equal 2, filtered.count
+ assert_equal [ @account.transactions.first.id, @account.transactions.second.id ].sort, filtered.map(&:id).sort
+ end
+
test "compound AND of two transaction_tag conditions matches transactions having both tags" do
scope = @rule_scope