Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10521 (closed enhancement: fixed)

Opened 7 months ago

Last modified 7 months ago

[PATCH] Allow proc for ActionController::Base.asset_host for maximum configurability

Reported by: chuyeow Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc:

Description

Allows you to set ActionController::Base.asset_host as a proc.

This allows you to get by the hardcoded assumption that you can setup 4 hosts numbered from 0-3 and also to do possibly smarter things depending on the asset path. E.g this allows you to randomly get assets from 2 hosts (assets1.example.com and assets2.example.com).

ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" }

Another example where you can use the source param that's passed to the block to decide where to get the asset from (useful if you have, say, images hosted elsewhere, or you want an SEO-friendly URL for your images):

ActionController::Base.asset_host = Proc.new { |source|
  if source.starts_with?('/images')
    "http://images.example.com"
  else
    "http://assets.example.com"
  end
}

It may sound like it could be expensive with a complicated proc, but this asset and path computation happens only the first time and is cached afterwards like all other computed paths.

Docs and tests in the patch.

Attachments

allow_procs_for_asset_host.diff (7.0 kB) - added by chuyeow on 12/16/07 09:26:36.
allow_procs_for_asset_host.2.diff (7.0 kB) - added by chuyeow on 12/16/07 09:38:33.

Change History

12/16/07 09:26:36 changed by chuyeow

  • attachment allow_procs_for_asset_host.diff added.

12/16/07 09:38:33 changed by chuyeow

  • attachment allow_procs_for_asset_host.2.diff added.

12/16/07 23:50:05 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [8421]) Added option to pass proc to ActionController::Base.asset_host for maximum configurability (closes #10521) [chuyeow]