Module: MoneyAttribute::AmountCondition Private

Included in:
QueryMethods
Defined in:
lib/money_attribute/query/amount_condition.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Internal amount-filter resolution for the where_amount query helper.

Supports two input forms: a hash of attribute/value pairs resolved to Arel predicates, and a SQL string with ? placeholders where attribute names are substituted for backing columns and Mint::Money binds are decomposed.

Constant Summary collapse

ALLOWED_KEYWORDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[and or not is null].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#resolve_amount_condition(attr, value) ⇒ ActiveRecord::Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an amount filter for the registered money attribute.

Parameters:

  • attr (Symbol)

    the money attribute name

  • value (Mint::Money, Numeric, Range, Array)

    the filter value

Returns:

  • (ActiveRecord::Relation)

Raises:

  • (ArgumentError)

    if the attribute is not a registered money attribute



21
22
23
24
25
26
# File 'lib/money_attribute/query/amount_condition.rb', line 21

def resolve_amount_condition(attr, value)
  spec = money_attribute_spec!(attr)
  col = arel_table[spec.amount_column]

  where(build_amount_predicate(col, spec, value))
end

#resolve_amount_condition_from_sql(sql, *values) ⇒ ActiveRecord::Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an amount filter using a SQL string with ? placeholders.

Only money attribute names, and, or, not, is, and null are allowed as identifiers. Mint::Money bind values are decomposed to raw storage values automatically.

Parameters:

  • sql (String)

    SQL fragment using attribute names and ? placeholders

  • values (Array)

    bind values

Returns:

  • (ActiveRecord::Relation)

Raises:

  • (ArgumentError)

    on unknown identifiers or placeholder mismatch



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/money_attribute/query/amount_condition.rb', line 39

def resolve_amount_condition_from_sql(sql, *values)
  specs = klass.money_attribute_specs
  attr_names = klass.money_attribute_names_set

  validate_sql_identifiers!(sql, attr_names)
  value_specs = map_placeholders_to_specs(sql, specs)
  decomposed = decompose_values(values, value_specs)
  substituted = substitute_attribute_names(sql, specs)

  where(substituted, *decomposed)
end