Module: Sprockets::Rails::Helper

Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::AssetUrlHelper, Utils
Defined in:
lib/sprockets/rails/helper.rb

Defined Under Namespace

Classes: AssetNotFound, AssetNotPrecompiled, AssetNotPrecompiledError

Constant Summary collapse

VIEW_ACCESSORS =
[
  :assets_environment, :assets_manifest,
  :assets_precompile, :precompiled_asset_checker,
  :assets_prefix, :digest_assets, :debug_assets,
  :resolve_assets_with, :check_precompiled_asset,
  :unknown_asset_fallback
]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#using_sprockets4?

Class Method Details

.extended(obj) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sprockets/rails/helper.rb', line 60

def self.extended(obj)
  obj.singleton_class.class_eval do
    attr_accessor(*VIEW_ACCESSORS)

    remove_method :assets_environment
    def assets_environment
      if env = @assets_environment
        @assets_environment = env.cached
      else
        nil
      end
    end
  end
end

.included(klass) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sprockets/rails/helper.rb', line 43

def self.included(klass)
  klass.class_attribute(*VIEW_ACCESSORS)

  klass.class_eval do
    remove_method :assets_environment
    def assets_environment
      if instance_variable_defined?(:@assets_environment)
        @assets_environment = @assets_environment.cached
      elsif env = self.class.assets_environment
        @assets_environment = env.cached
      else
        nil
      end
    end
  end
end

Instance Method Details

#asset_digest_path(path, options = {}) ⇒ Object

Expand asset path to digested form.

path - String path options - Hash options

Returns String path or nil if no asset was found.



112
113
114
115
116
# File 'lib/sprockets/rails/helper.rb', line 112

def asset_digest_path(path, options = {})
  resolve_asset do |resolver|
    resolver.digest_path path, options[:debug]
  end
end

#asset_integrity(path, options = {}) ⇒ Object

Experimental: Get integrity for asset path.

path - String path options - Hash options

Returns String integrity attribute or nil if no asset was found.



124
125
126
127
128
129
130
# File 'lib/sprockets/rails/helper.rb', line 124

def asset_integrity(path, options = {})
  path = path_with_extname(path, options)

  resolve_asset do |resolver|
    resolver.integrity path
  end
end

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

Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path to use the asset pipeline.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sprockets/rails/helper.rb', line 77

def compute_asset_path(path, options = {})
  debug = options[:debug]

  if asset_path = resolve_asset_path(path, debug)
    File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
  else
    message =  "The asset #{ path.inspect } is not present in the asset pipeline.\n"
    raise AssetNotFound, message unless unknown_asset_fallback

    if respond_to?(:public_compute_asset_path)
      message << "Falling back to an asset that may be in the public folder.\n"
      message << "This behavior is deprecated and will be removed.\n"
      message << "To bypass the asset pipeline and preserve this behavior,\n"
      message << "use the `skip_pipeline: true` option.\n"

      Sprockets::Rails.deprecator.warn(message, caller_locations)
    end
    super
  end
end

#javascript_include_tag(*sources) ⇒ Object

Override javascript tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sprockets/rails/helper.rb', line 135

def javascript_include_tag(*sources)
  options = sources.extract_options!.stringify_keys
  integrity = compute_integrity?(options)

  if options["debug"] != false && request_debug_assets?
    sources.map { |source|
      if asset = lookup_debug_asset(source, type: :javascript)
        if asset.respond_to?(:to_a)
          asset.to_a.map do |a|
            super(path_to_javascript(a.logical_path, debug: true), options)
          end
        else
          super(path_to_javascript(asset.logical_path, debug: true), options)
        end
      else
        super(source, options)
      end
    }.flatten.uniq.join("\n").html_safe
  else
    sources.map { |source|
      options = options.merge('integrity' => asset_integrity(source, type: :javascript)) if integrity
      super source, options
    }.join("\n").html_safe
  end
end

#resolve_asset_path(path, allow_non_precompiled = false) ⇒ Object

Resolve the asset path against the Sprockets manifest or environment. Returns nil if it’s an asset we don’t know about.



100
101
102
103
104
# File 'lib/sprockets/rails/helper.rb', line 100

def resolve_asset_path(path, allow_non_precompiled = false) #:nodoc:
  resolve_asset do |resolver|
    resolver.asset_path path, digest_assets, allow_non_precompiled
  end
end

Override stylesheet tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sprockets/rails/helper.rb', line 164

def stylesheet_link_tag(*sources)
  options = sources.extract_options!.stringify_keys
  integrity = compute_integrity?(options)

  if options["debug"] != false && request_debug_assets?
    sources.map { |source|
      if asset = lookup_debug_asset(source, type: :stylesheet)
        if asset.respond_to?(:to_a)
          asset.to_a.map do |a|
            super(path_to_stylesheet(a.logical_path, debug: true), options)
          end
        else
          super(path_to_stylesheet(asset.logical_path, debug: true), options)
        end
      else
        super(source, options)
      end
    }.flatten.uniq.join("\n").html_safe
  else
    sources.map { |source|
      options = options.merge('integrity' => asset_integrity(source, type: :stylesheet)) if integrity
      super source, options
    }.join("\n").html_safe
  end
end