Module: Sprockets::Rails::Helper

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

Defined Under Namespace

Classes: AssetNotPrecompiled

Constant Summary collapse

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

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.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.



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

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.



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

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



75
76
77
78
79
80
81
82
83
# File 'lib/sprockets/rails/helper.rb', line 75

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
    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.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sprockets/rails/helper.rb', line 122

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.



87
88
89
90
91
# File 'lib/sprockets/rails/helper.rb', line 87

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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sprockets/rails/helper.rb', line 151

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