asset helpers with custom paths #50

Closed
atambo opened this Issue Sep 15, 2011 · 24 comments

Comments

Projects
None yet
6 participants

atambo commented Sep 15, 2011

It would be awesome if the asset helpers allowed for custom paths. This is needed in order to write applications that can be deployed under any subdirectory, subdirectories, or at the root.

Example usage:

image-url("nav_bg.png", "../assets");

would compiles to

url(../assets/nav_bg-78ce4921cd8bd8512d8e054273d49015.png);

Is there any way to achieve this now and if not does this seem like a feature you would want implemented?

Collaborator

chriseppstein commented Sep 15, 2011

Why do you want a URL relative to your stylesheet?

chriseppstein reopened this Sep 15, 2011

atambo commented Sep 15, 2011

My specific use case is a rails 3.1 application that is packaged as a war file using https://github.com/jruby/warbler and can be deployed to any subdirectory or the root within an application server such as tomcat. Currently, when the war file is generated the assets are precompiled with absolute paths such as /assets/image.png. I need my assets to be precompiled with relative paths such as ../assets/image.png so that way when my application is deployed under a subdirectory or the root paths they both work.

Collaborator

chriseppstein commented Sep 15, 2011

This is supported in compass by providing a "relative_assets" setting. In sass-rails we delegate URL computation to sprockets/rails. if they have a similar setting I will be happy to support it.

/cc @sstephenson @josh

atambo commented Sep 15, 2011

Looking at the compass config at http://compass-style.org/help/tutorials/configuration-reference/:

relative_assets Boolean

Indicates whether the compass helper functions should generate relative urls from the generated css to assets, or absolute urls using the http path for that asset type.

the relative_assets = true setting seems to be exactly what I need

Owner

guilleiguaran commented Sep 15, 2011

@atambo, I opened an issue about this in Warbler some days ago: jruby/warbler#50

Please try with Rails 3.1.1.rc1, I've fixed Rails to handle correctly assets paths under relative_url_root (rails/rails@9279d11). This must fix the problems with precompiled assets under JRuby apps packed with Warbler

Let me know if it's working fine in your apps.

atambo commented Sep 15, 2011

@guilleiguaran, relative_url_root can't help me in this case because I can't rebuild my app for each subdirectory I want to deploy into. I can only precompile the assets once and then those assets need to work no matter what the relative_url_root gets set to by jruby-rack.

Collaborator

chriseppstein commented Sep 15, 2011

Except that if you use the asset pipeline with compass you get the rails helpers, not the compass helpers.

:(

Hunt & pecked on my iPhone... Sorry if it's brief!

On Sep 14, 2011, at 6:19 PM, Alex Tambellinireply@reply.github.com wrote:

Looking at the compass config at http://compass-style.org/help/tutorials/configuration-reference/:

relative_assets Boolean

Indicates whether the compass helper functions should generate relative urls from the generated css to assets, or absolute urls using the http path for that asset type.

the relative_assets = true setting seems to be exactly what I need

Reply to this email directly or view it on GitHub:
#50 (comment)

atambo commented Sep 15, 2011

So does having a setting like

config.assets.relative_assets = true

that adds .. in front of the asset_path sound correct?

So then

image-url("nav_bg.png");

becomes

url(../assets/nav_bg-78ce4921cd8bd8512d8e054273d49015.png);
Collaborator

chriseppstein commented Sep 15, 2011

no. generated stylesheets can be in folders. the url generation has to do a proper path resolution between the two assets.

Collaborator

chriseppstein commented Sep 15, 2011

@atambo that is correct.

atambo commented Sep 15, 2011

@chriseppstein from looking at https://github.com/rails/rails/blob/master/actionpack/lib/action_view/asset_paths.rb and https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/helpers/rails_helper.rb it doesn't seem like there is any existing functionality for generating relative paths. Do you think it makes sense to add the compute_relative_path functionality to the sprockets asset paths? If so, I'd be up for giving it a try.

Collaborator

chriseppstein commented Sep 15, 2011

I think it makes sense.

atambo commented Sep 18, 2011

@chriseppstein, I'm stuck trying to find the path of the stylesheet the asset is relative to.

The sass asset helpers seem to only receive the asset filename and the directory the asset is located in:

https://github.com/rails/sass-rails/blob/master/lib/sass/rails/helpers.rb#L14

In order to compute the relative path I also need the path of the stylesheet the asset is relative to so that I can pass it to the compute_public_path method inside of the options hash:

https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/helpers/rails_helper.rb#L105

How can I find the which stylesheet the asset is referenced from?

Collaborator

chriseppstein commented Sep 19, 2011

options[:css_filename] should be set to the name of the output file in sass-rails like it is in compass.

docwhat commented Sep 19, 2011

I'm using JRuby and Warbler as well....I'm glad I saw this issue... +1

Collaborator

chriseppstein commented Sep 19, 2011

Sorry I think you misunderstood when the sass engine is created that should be passed as an option. I have no idea what option should be passed (if any) to sprockets.

atambo commented Sep 19, 2011

@chriseppstein, you mean we should pass the :css_filename option to Sass::Engine.new by adding the option to this options hash:?

https://github.com/rails/sass-rails/blob/master/lib/sass/rails/template_handlers.rb#L55

I can do that. But I'll still need to pass an option to sprockets so that it can use it for relative_path generation. Does the way I have it not seem like the correct way to do it?

Collaborator

chriseppstein commented Sep 19, 2011

Yes, that is what I mean.

The way you have done it passes to sprockets. I doubt that :css_filename is what they are expecting. You will still need to pass something to sprockets somewhere, but having the css filename (the one it would have if it was written to disk) in the sass options hash is the first step to that.

atambo commented Sep 19, 2011

@chriseppstein, I added :css_filename to the sass options hash: #52

However, I still need to pass the stylesheet path to sprockets somehow. I think the only way to do that is by passing it within the options hash of the compute_public_path method. Does that sound right to you?

atambo commented Dec 5, 2011

@chriseppstein, I saw that you added the css_filepath entry, are you going to make the changes to sprockets as well?

Collaborator

chriseppstein commented Dec 5, 2011

@atambo, I wasn't planning on it.

Contributor

frodsan commented Apr 18, 2013

@atambo Is this still an issue?

Owner

rafaelfranca commented Apr 18, 2013

I'm closing this issue since it seems more a feature request. Feel free to ask to reopen if you think we should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment