Skip to content

Commit

Permalink
Support all number helpers of ActiveSupport::NumberHelper (#2427)
Browse files Browse the repository at this point in the history
  • Loading branch information
shouichi committed Nov 13, 2023
1 parent 2e0595a commit 60aee78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
3 changes: 1 addition & 2 deletions docs/customizing_dashboards.md
Expand Up @@ -171,8 +171,7 @@ more results than expected. Default is `false`.
and works by by passing a hash that includes the formatter (`formatter`) and
the options for the formatter (`formatter_options`). Defaults to the locale's
delimiter when `formatter_options` does not include a `delimiter`. See the
example below. Note that currently only
`ActiveSupport::NumberHelper.number_to_delimited` is supported.
example below. All helpers from `ActiveSupport::NumberHelper` are supported.

For example, you might use the following to display U.S. currency:

Expand Down
10 changes: 2 additions & 8 deletions lib/administrate/field/number.rb
Expand Up @@ -38,14 +38,8 @@ def format(result)
formatter = options[:format][:formatter]
formatter_options = options[:format][:formatter_options].to_h

case formatter
when :number_to_delimited
ActiveSupport::NumberHelper.number_to_delimited(
result, **formatter_options
)
else
result
end
ActiveSupport::NumberHelper.
try(formatter, result, **formatter_options) || result
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/fields/number_spec.rb
Expand Up @@ -112,6 +112,15 @@
end
end

context "when `formatter: :number_to_currency`" do
it "includes the currency" do
with_currency = number_with_options(
100, format: { formatter: :number_to_currency }
)
expect(with_currency.to_s).to eq("$100.00")
end
end

context "when passed incorrect `formatter`" do
it "works" do
thousand = number_with_options(1_000, format: { formatter: :rubbish })
Expand Down

0 comments on commit 60aee78

Please sign in to comment.