25 Apr
Active Support helpers configuration - Rails Tricks Issue 4
Hi, this week I am bringing you a trick to configure Active Support helpers!
Active Support is a collection of utility classes and extensions to Ruby core and standard library classes. It provides quite a few valuable data formatting helpers, and if you want to change their formatting settings globally, you can do so by utilizing Rails’ localization.
One such a helper is number_to_currency, which takes precision, unit, delimiter, format, negative_format, and strip_insignificant_zeros as options when you call the helper. If you want to strip insignificant zeros globally, instead of passing the same option over and over to the helper in your code, you can just change the option in your locale file:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTAsInB1ciI6ImJsb2JfaWQifX0=--55f47778f40e0c368e4dedda00db7e993f8f09d7/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/f892d2c5-bed1-4f4b-8688-b0d2b3cb8c43.png)
There is also the number_to_human_size helper, which has delimiter, precision, significant, and strip_insignificant_zero options. You can change those globally in the locale file too:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTEsInB1ciI6ImJsb2JfaWQifX0=--40b2a253ab617227b965cdf3f8d1bed3cc40f49d/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/628fc3ff-eb58-40a2-8e33-b7383bbbf4f3.png)
Dates are also often formatted into specific formats within an application by calling to_s(:date_format), which is deprecated now in favor of to_fs(:date_format). If you want to use a custom format, you can do so in the locale file:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTIsInB1ciI6ImJsb2JfaWQifX0=--d850c50a3ef3475d5eeed703cae728e2b1799e2a/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/4d09ad69-0652-4d49-85aa-dffce2b27f20.png)
You are probably working with DateTime objects too in your app, so it is a good idea to set the same formats for Time too:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTMsInB1ciI6ImJsb2JfaWQifX0=--577f3f5e1677429595fbb035dc2642cb6624e86a/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/dc1735e2-0d81-4d02-b165-91991a345063.png)
Last week, I mentioned the Array#to_sentence helper, which can be configured globally from the locale file too:
![](https://pombomailer.com/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6NTQsInB1ciI6ImJsb2JfaWQifX0=--e9271af631c96a64365ffac7785688f510cb88fc/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fbGltaXQiOlsxMDI0LDc2OF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--cefc62acd607acb36c209b04aa5893ccc86cdf29/53f83c5b-7500-40db-bf69-fb9e39bce3c2.png)
That’s it for this week. Until next time!