Skip to content

Reflecting on monetized attribute configuration #722

@mroach

Description

@mroach

Background

When using monetize in a model there are options that can be set such as which column stores the currency when this deviates from the default e.g.

monetize :net_amount, with_model_currency: :currency

When a field is monetized, it gets tracked in a hash that's {attribute_name => cents_column_name} and this is accessible via .monetized_attributes e.g.

Invoice.monetized_attributes
=> {"net_amount" => "net_amount_cents"}

(Our) Issue

There's currently no way to reflect on any configuration except the column storing the cents. In our case, we want to know the name of the column storing the currency. We maintain a library of automated data integrity checks and want to detect if we ever have cases where the cents attribute is set but not the currency, or vice versa.

The goal is being able to dynamically generate SQL like:

SELECT id FROM invoices WHERE (net_amount_cents IS NULL) <> (currency IS NULL)

Currently this isn't possible since there's no way to reflect on the currency column being used for each attribute.

Question

Would it be reasonable to store all configuration options in the attributes hash so that they can be reflected upon?

Invoice.monetized_attributes
=> {"net_amount" => {cents: "net_amount_cents", currency: "currency", allow_nil: true}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions