Module: Sprockets::Rails::LegacyAssetUrlHelper

Included in:
Helper
Defined in:
lib/sprockets/rails/legacy_asset_url_helper.rb

Overview

Backports of AssetUrlHelper methods for Rails 2.x and 3.x.

Constant Summary collapse

URI_REGEXP =
%r{^[-a-z]+://|^(?:cid|data):|^//}
ASSET_EXTENSIONS =
{
  :javascript => '.js',
  :stylesheet => '.css'
}
ASSET_PUBLIC_DIRECTORIES =
{
  :audio      => '/audios',
  :font       => '/fonts',
  :image      => '/images',
  :javascript => '/javascripts',
  :stylesheet => '/stylesheets',
  :video      => '/videos'
}

Instance Method Summary collapse

Instance Method Details

#asset_path(source, options = {}) ⇒ Object Also known as: path_to_asset



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 9

def asset_path(source, options = {})
  source = source.to_s
  return "" unless source.present?
  return source if source =~ URI_REGEXP

  tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')

  if extname = compute_asset_extname(source, options)
    source = "#{source}#{extname}"
  end

  if source[0] != ?/
    source = compute_asset_path(source, options)
  end

  relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
  if relative_url_root
    source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
  end

  if host = compute_asset_host(source, options)
    source = "#{host}#{source}"
  end

  "#{source}#{tail}"
end

#asset_url(source, options = {}) ⇒ Object



37
38
39
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 37

def asset_url(source, options = {})
  path_to_asset(source, options.merge(:protocol => :request))
end

#audio_path(source, options = {}) ⇒ Object Also known as: path_to_audio



122
123
124
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 122

def audio_path(source, options = {})
  path_to_asset(source, {:type => :audio}.merge(options))
end

#compute_asset_extname(source, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 46

def compute_asset_extname(source, options = {})
  return if options[:extname] == false
  extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
  extname if extname && File.extname(source) != extname
end

#compute_asset_host(source = "", options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 66

def compute_asset_host(source = "", options = {})
  request = self.request if respond_to?(:request)

  if defined? config
    host = config.asset_host
  elsif defined? ActionController::Base.asset_host
    host = ActionController::Base.asset_host
  end

  host ||= request.base_url if request && options[:protocol] == :request
  return unless host

  if host.respond_to?(:call)
    arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
    args = [source]
    args << request if request && (arity > 1 || arity < 0)
    host = host.call(*args)
  elsif host =~ /%d/
    host = host % (Zlib.crc32(source) % 4)
  end

  if host =~ URI_REGEXP
    host
  else
    protocol = options[:protocol] || (request ? :request : :relative)
    case protocol
    when :relative
      "//#{host}"
    when :request
      "#{request.protocol}#{host}"
    else
      "#{protocol}://#{host}"
    end
  end
end

#compute_asset_path(source, options = {}) ⇒ Object



61
62
63
64
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 61

def compute_asset_path(source, options = {})
  dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
  File.join(dir, source)
end

#font_path(source, options = {}) ⇒ Object Also known as: path_to_font



127
128
129
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 127

def font_path(source, options = {})
  path_to_asset(source, {:type => :font}.merge(options))
end

#image_path(source, options = {}) ⇒ Object Also known as: path_to_image



112
113
114
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 112

def image_path(source, options = {})
  path_to_asset(source, {:type => :image}.merge(options))
end

#javascript_path(source, options = {}) ⇒ Object Also known as: path_to_javascript



102
103
104
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 102

def javascript_path(source, options = {})
  path_to_asset(source, {:type => :javascript}.merge(options))
end

#stylesheet_path(source, options = {}) ⇒ Object Also known as: path_to_stylesheet



107
108
109
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 107

def stylesheet_path(source, options = {})
  path_to_asset(source, {:type => :stylesheet}.merge(options))
end

#video_path(source, options = {}) ⇒ Object Also known as: path_to_video



117
118
119
# File 'lib/sprockets/rails/legacy_asset_url_helper.rb', line 117

def video_path(source, options = {})
  path_to_asset(source, {:type => :video}.merge(options))
end