image_pathを読む
公開日:
:
最終更新日:2014/01/31
Ruby on Rails
記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
% mdfind "image_path" | grep asset_tag_helper.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/asset_tag_helper.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/helpers/asset_tag_helper.rb
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/asset_tag_helper.rb
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/asset_tag_helper.rb
/Library/Ruby/Gems/1.8/gems/actionpack-1.13.6/lib/action_view/helpers/asset_tag_helper.rb
% vim /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/helpers/asset_tag_helper.rb
# Computes the path to an image asset in the public images directory.
# Full paths from the document root will be passed through.
# Used internally by image_tag to build the image path.
#
# ==== Examples
# image_path("edit") # => /images/edit
# image_path("edit.png") # => /images/edit.png
# image_path("icons/edit.png") # => /images/icons/edit.png
# image_path("/icons/edit.png") # => /icons/edit.png
# image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png
def image_path(source)
compute_public_path(source, 'images')
end
compute_public_pathを読む。
# Add the .ext if not present. Return full URLs otherwise untouched.
# Prefix with /dir/ if lacking a leading /. Account for relative URL
# roots. Rewrite the asset path for cache-busting asset ids. Include
# asset host, if configured, with the correct request protocol.
def compute_public_path(source, dir, ext = nil, include_host = true)
has_request = @controller.respond_to?(:request)
cache_key =
if has_request
[ @controller.request.protocol,
ActionController::Base.asset_host.to_s,
@controller.request.relative_url_root,
dir, source, ext, include_host ].join
else
[ ActionController::Base.asset_host.to_s,
dir, source, ext, include_host ].join
end
ActionView::Base.computed_public_paths[cache_key] ||=
begin
source += ".#{ext}" if File.extname(source).blank? && ext
if source =~ %r{^[-a-z]+://}
source
else
source = "/#{dir}/#{source}" unless source[0] == ?/
if has_request
source = "#{@controller.request.relative_url_root}#{source}"
end
rewrite_asset_path!(source)
if include_host
host = compute_asset_host(source)
if has_request && !host.blank? && host !~ %r{^[-a-z]+://}
host = "#{@controller.request.protocol}#{host}"
end
"#{host}#{source}"
else
source
end
end
end
end
rewrite_asset_pathを読む。
# Break out the asset path rewrite so you wish to put the asset id
# someplace other than the query string.
def rewrite_asset_path!(source)
asset_id = rails_asset_id(source)
source << "?#{asset_id}" if !asset_id.blank?
end
rails_asset_idを読む。
# Use the RAILS_ASSET_ID environment variable or the source's
# modification time as its cache-busting asset id.
def rails_asset_id(source)
if asset_id = ENV["RAILS_ASSET_ID"]
asset_id
else
path = File.join(ASSETS_DIR, source)
if File.exist?(path)
File.mtime(path).to_i.to_s
else
''
end
end
end
‘sample.jpg?1206075143’ の正体はこれか!!