17

I'm trying to overriding a helper method of base_helper.rb by using this:

module Spree
  module BaseHelper.class_eval do

    def taxons_tree(root_taxon, current_taxon, max_level = 1)
      .....
    end

  end
end

It's not working for me. Anyone know what I am missing here?

Thank you!

Fixed:

I should use:

Spree::BaseHelper.module_eval do

    def taxons_tree(root_taxon, current_taxon, max_level = 1)
      ...
    end

end

instead.

lnguyen55
  • 737
  • 6
  • 16

1 Answers1

23

Re-opening the module will work just as well:

module Spree
  module BaseHelper
   def taxons_tree(root_taxon, current_taxon, max_level = 1)
      ...
   end
  end
end

There's no particular reason to use class_eval and module_eval, it's just been the habit in the Spree project for a very long time.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • 2
    Ryan, where do we put this code. I am in need of overriding `link_to_cart`method to make a customized cart segment. I am coming form PHP and if this is a Rails question, I am sorry but highly appreciate a comment. – Ziyan Junaideen May 09 '13 at 17:31
  • 3
    Create a folder under app/helpers, called spree, and put the code from Ryan Bigg into a file caalled base_helper.rb – Joao Pereira Oct 05 '13 at 16:49
  • 16
    @JoaoPereira if you call base_helper.rb, spree won't load the original base_helper.rb. Your would have to copy/paste every function, for it to function properly. But if you call base_helper_decorator.rb, it works. – alexandrecosta Jun 04 '14 at 15:00