Informatika Mihelac
February 28 2007 rails | ruby

Override helpers in Rails

Sooner or later need to override some built in Rails helpers would occur. For example, I like to use number_to_currency helper to format prices in my application, but I want all prices to be formated in euros instead of dollars.

Worth noting is that you cannot simple redefine number_to_currency in application_helper.rb what would be the first thing on my mind to do. You can not redefine original helper method in ActionView::Base class as well. That is because helpers are included as mixin and methods from module are made available to class that includes the module, they are not copied, so if module methods are changed later so are the modules method in a class. Original helper method should be replaced in module where it comes from and in case of currencies this is ActionView::Helpers::NumberHelper.

Here is example that replaces original number_to_currency method and set default currency to Euro:

module ActionView
  module Helpers
    module NumberHelper
      def number_to_currency_with_euro(number, options = {})
        defaults = {:unit => ''}
        s = number_to_currency_without_euro(number, defaults.merge(options))
        s << ' &euro;' unless options[:unit]
      end
      alias_method_chain :number_to_currency, :euro
    end
  end
end
comments feed

9 comments

  1. # On March 07, 2007 at 13:03 PM, Jeff said:
    I'm sure you thought of this, but perhaps you could have just provided your own helper as a wrapper? def number_to_euro(amount) number_to_currency(amount, :unit => ' &euro') end Of course, then you'd have to tell everyone else on your team to use this new helper, which might not be ideal.
  2. # On March 07, 2007 at 15:03 PM, Bojan Mihelac said:
    Jeff, sure I started with that, but I liked idea to use _standard_ helpers early in development and customize after without search/replace all over. It's maybe unjustified to write so much code for this simple example, but I wanted to show method how to replace helpers and how mixed methods behave in class.
  3. # On March 14, 2007 at 11:03 AM, cptvitamin said:
    Great tutorial, for noobs, you might want to mention that you have to freeze rails if you are hosting on a shared server (then make the changes in /vendor/rails). Also, any changes made to the frozen rails requires a restart of WEBrick for the changes to take effect.
  4. # On April 21, 2007 at 11:04 AM, Stan said:
    And where place this code, so rails can pickup it? For example i don't want to freeze rails and change core files?
  5. # On April 21, 2007 at 12:04 PM, Bojan Mihelac said:
    Stan, put code in +lib+ folder and include it in +config/environment.rb+. It would not be good idea to change core rails files as it wouldn't be easy to upgrade.
  6. # On April 22, 2007 at 02:04 AM, Stan said:
    Thanks a lot, Bojan. I have almost same situation, except i want to redefine private method: ActionView::Helpers::InstanceTag.add_options The problem is that i wrongly put path before (ActionView::Helpers::FormOptionsHelper::InstanceTag). So i had code almost identical to yours and wondered why it didn't work (i put it initially directly to environment.rb). P.S. To avoid such errors further do u know how to calculate way to certain method, not digging through source code? Thanks.
  7. # On April 22, 2007 at 05:04 AM, Bojan Mihelac said:
    techno weenie explains both "Rails initialization process":http://weblog.techno-weenie.net/2007/1/24/understanding-the-rails-initialization-process and "Rails Plugin initialization process":http://weblog.techno-weenie.net/2007/1/26/understanding-the-rails-plugin-initialization-process. Very useful to know how it works if you need to extend Rails or write plugins.
  8. # On December 10, 2007 at 23:12 PM, dee said:

    I’m a newly wed and we together with my spouse decided to start building good credit as we started a family of our own, as in future we’ll have buy a house and cars and many other things. I wanted a credit card for both of us, so that we could use it together and accumulate credit rating. Happily, I found a card for spouses at

    barclays bank balance transfers with 12 months interest free with no credit fee charge

  9. # On December 11, 2007 at 02:12 AM, clorinda said:

    You can’t always have cash to pay for things you need to obtain immediately. For such occasions banks offer credit cards which differ in purpose and features like interest rates, rewards, fees. But you need to build good credit history to be able to use all the opportunities credit cards give. At

    o percent interest discover card

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in Informatika Mihelac.

Contact: bmihelac@mihelac.org

RSS feedSubscribe to RSS Feed