Number_to_currency() helper == non DRY == could be improved


#1

Hi all,

Apologies in advance if I got this one wrong, I’m still just a
newbie :wink:

Going through the Apple ADC RoR tutorial it suddenly occurred to me
that the ‘number_to_currency()’ helper method was not DRY streamlined.

Scenario::
The “number_to_currency()” methods options hash works great when I
need to dynamically display different currencies when I call
“number_to_currency()”, as long a I have an idea of what the currency
should be each time :wink:

But, what if I want all instances of a currency to display as GBP, £,
SEK or any other currency ? Then I would have to create a CONSTANT
for that currency and then include that option in every instance of
the ‘number_to_currency()’ method.

number_to_currency(0.02) versus number_to_currency(0.02,
{:unit => MY_CURRENCY_CONSTANT} )

Not to bad some may say, but it’s still not exactly DRY nor elegant
is it ?

Instead I would propose as similar scheme to the ‘session_key’
override functionality that I use a lot.

add a custom session ID for this site (when developing with many

apps on local system)
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] =
‘00_shows_at_top_of_sorted_cookies_list’

Whereby we could define the DEFAULT_CURRENCY_TYPE in the
environment.rb or something similar.

However, an app could also need to be localized, ie I might want the
default currency to be £ and someone else might want the � (euro) in
their session of the same app.

So, I see this issue, but is not (yet) knowledgeable enough to figure
out a proper fix for it and submit it as a ticket, hence this post
here, for someone else to pick up and hopefully ‘run’ with :wink: As I
said above, apologies if this is the wrong approach or a stupid idea.

Kind regards,

Mats


“TextMate, coding with an incredible sense of joy and ease”


#2

Why don’t you just define your own helper action that calls
number_to_currency with the proper options? Something like:

def number_to_pounds (number)
number_to_currency(number, {:unit => “£”, :separator => “,”,
:delimiter => “”})
end

/ CJ