Forum: Ruby on Rails How to override one method of AssetTagHelper

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.
Rémi G. (Guest)
on 2008-10-28 19:46
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?
Frederick C. (Guest)
on 2008-10-28 20:06
(Received via mailing list)
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
Rémi G. (Guest)
on 2008-10-28 20:18
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
Frederick C. (Guest)
on 2008-10-28 21:57
(Received via mailing list)
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
reHa (Guest)
on 2008-10-28 23:37
(Received via mailing list)
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
Rémi G. (Guest)
on 2008-10-28 23:44
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
reHa (Guest)
on 2008-10-29 00:13
(Received via mailing list)
> 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
Rémi G. (Guest)
on 2008-10-29 00:26
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.
This topic is locked and can not be replied to.