Spree overriding helper method
Asked Answered
C

1

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.

Charmer answered 16/8, 2012 at 13:31 Comment(1)
For me only worked when used Spree::BaseHelper.class_evalKaila
K
22

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.

Kitchenware answered 22/8, 2012 at 14:26 Comment(3)
Ryan, where do we put this code. I am in need of overriding link_to_cartmethod 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.Dentistry
Create a folder under app/helpers, called spree, and put the code from Ryan Bigg into a file caalled base_helper.rbDahlgren
@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.Interphone

© 2022 - 2024 — McMap. All rights reserved.