How to override one method of AssetTagHelper


#1

I created a file ./lib/action_view/helpers/asset_tag_helper.rb

and put in it only the method I want to override. like this

module ActionView
module Helpers #:nodoc:
module AssetTagHelper
def image_path(source)
compute_public_path(source, ‘images’)
end
end
end
end

But as soon I try this all the others methods from the overriden module
are not available anymore.

Any idea?


#2

On 28 Oct 2008, at 17:46, Rémi Gagnon <rails-mailing-list@andreas-
s.net> wrote:

I created a file ./lib/action_view/helpers/asset_tag_helper.rb

and put in it only the method I want to override. like this

Don’t. Create lib/my_module.rb and then include MyModule into
ActionView::Base. Or it might be more convenient to do this in
application_helper


#3

Thank you, you are really right I should put it in my
application_helper. But Here we will have a lot of projets and I want
to share this kind of changes to all teams. So that’s why I would like
to ultimatly put it in a gem.

So I’m curious to see how you would include mymodule on
ActionView::Base.

Thanks in advance

Frederick C. wrote:

On 28 Oct 2008, at 17:46, Rémi Gagnon <rails-mailing-list@andreas-
s.net> wrote:

I created a file ./lib/action_view/helpers/asset_tag_helper.rb

and put in it only the method I want to override. like this

Don’t. Create lib/my_module.rb and then include MyModule into
ActionView::Base. Or it might be more convenient to do this in
application_helper


#4

On Oct 28, 6:18 pm, Rémi Gagnon removed_email_address@domain.invalid
wrote:

Thank you, you are really right I should put it in my
application_helper. But Here we will have a lot of projets and I want
to share this kind of changes to all teams. So that’s why I would like
to ultimatly put it in a gem.

So I’m curious to see how you would include mymodule on
ActionView::Base.

ActionView::Base.send :include, MyModule

With a plugin you’d typically put this in the plugin’s init.rb

Fred


#5

Hi,

In our project we are using lib file as suggested

file name would be:
lib/action_view_ext.rb

module ActionView
module Helpers #:nodoc:
module AssetTagHelper
def image_path(source)
compute_public_path(source, ‘images’)
end
end
end
end

the best way to include the file !always! (doesn’t matter if the
application helper is loaded or not)
is to add one line at the end of the

config/environment.rb

the line would be:

require ‘lib/action_view_ext.rb’

Cheers


#6

Thanks a lot for the quick response!

BTW the reason why I want to override a method is because I need to
support images for different locale.

for instance :

logo_fr.gif (for french)
logo_en.gif (for of course english)

In my view I want something like :

<% image_tag(‘logo.gif’, :locale => cookies[:locale])%>

any better way?

Thanks again


#7

In my view I want something like :

<% image_tag(‘logo.gif’, :locale => cookies[:locale])%>

any better way?

Will all the pictures on the site be localized?
Probably not - maybe it would be better if you create a method called
something like

image_localized_tag

in your application helper. It could take “locale” from cookie and
change passed file name (locale isn’t parameter now).
After changing the name it could just call image_tag and return the
result.

This is just a suggestion - IMHO it’s easier to implement and cleaner
later on for other developers - some of them may expect different
behaviour from image_tag

cheers


#8

After rethinking of it that’s what I plan to do. Just wnat to make sure
it is the right way and I wanted assements of experts.

Thank you very much.