Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Rails 3.1 asset pipeline for images only?

I'm upgrading a Rails 3 app to 3.2 and setting up the asset pipeline. It's great for css/js but I don't really see the point in using it for images, and unfortunately I have css with a ton of references /images/*.png and the like.

Is there a way to disable the asset pipeline just for images so image_tag("x.png") will go back to returning <img src="/images/x.png"> instead of <img src="/assets/x.png">? Thanks!

like image 750
swrobel Avatar asked Oct 08 '22 13:10

swrobel


1 Answers

You can monkey-patch ActionView::Base, try this in rails console:

helper.image_path "foo" #=> "/assets/foo"

module OldImagePath
  def image_path(source)
    asset_paths.compute_public_path(source, 'images')
  end
  alias_method :path_to_image, :image_path
end
ActionView::Base.send :include, OldImagePath

helper.image_path "foo" #=> "/images/foo"

You can place this in an initializer for example. By default ActionView::Base includes ActionView::Helpers::AssetTagHelper and Sprockets::Helpers::RailsHelper which both define image_path but the latter take precedence. I'm including my own module which take precedence over all of them (the code inside is taken from ActionView::Helpers::AssetTagHelper).

Although, it makes sense to use asset pipeline for images too. They get hash sum in their filenames so that they can be cached forever on the client side without asking the server whether the file was changed.

like image 99
Simon Perepelitsa Avatar answered Oct 13 '22 10:10

Simon Perepelitsa