jacquescrocker / jammit-s3

Extensions so you can use jammit with s3/cloudfront for your Rails app
http://documentcloud.github.com/jammit/
MIT License
124 stars 31 forks source link

S3 asset_url #21

Open guiocavalcanti opened 13 years ago

guiocavalcanti commented 13 years ago

Hello,

I'm doing a lazy loading feature (loading some assets via JavaScript) and I can't realize how to get the asset S3 URL. I tried Jammit.asset_url(:lazy_asset, :js) but it gave me something like /assets/lazy_asset.js, which is not what I expected.

I'm doing this job on the following helper:

 def async_include_javascripts(*packages, &block)
  tags = packages.map do |pack|
    should_package? ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
  end

  javascript_tag(:type => 'text/javascript') do
    result = "LazyLoad.js(#{tags.flatten.to_json}"

    if block.nil?
      result << ");"
    else
      result << ", function(){ #{capture(&block)}});"
    end

    result.html_safe
  end.html_safe
end

And the result is:

<script type="text/javascript">
  //<![CDATA[
    LazyLoad.js(["/assets/lazy_asset.js"]);
  //]]>
</script>

Which I expected to be:

<script type="text/javascript">
  //<![CDATA[
    LazyLoad.js(["http://assets-host.s3.amazonaws.com/assets/lazy_asset.js"]);
  //]]>
</script>
guiocavalcanti commented 13 years ago

Anyone?

kmamykin commented 13 years ago

The quick and short answer is that you need to prepend the items in your tags var with

ActionController::Base.asset_host.call(source, request)

It is a hack because you assume the asset_host is a Proc (probably true if you are using jammit-s3 and have not manually set asset_host in your environment configs).

The proper answer is to use one of ActiveView helpers, but which one depends on the version of rails you are using. Take a look at your actionpack/lib/action_view/helpers/asset_tag_helper.rb and search for asset_host.