Skip to content

Commit

Permalink
Support all number helpers of ActiveSupport::NumberHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
shouichi committed Sep 1, 2023
1 parent 852b62c commit a8195ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
3 changes: 1 addition & 2 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,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
9 changes: 1 addition & 8 deletions lib/administrate/field/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ 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
Original file line number Diff line number Diff line change
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 a8195ab

Please sign in to comment.