Forum: Ruby on Rails number_to_currency() helper == non DRY == could be improved

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
855ddc9e30065b4061b08a7bd349a0af?d=identicon&s=25 Mats Persson (Guest)
on 2006-03-03 20:38
(Received via mailing list)
Hi all,

Apologies in advance if I got this one wrong, I'm still just a
newbie ;-)

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 ;-)

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 ;-)  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"
- www.macromates.com -
E24b2a1d71b7365186a934a09ee6f7c3?d=identicon&s=25 Carl-Johan Kihlbom (Guest)
on 2006-03-04 10:20
(Received via mailing list)
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
This topic is locked and can not be replied to.