File: context.rb

package info (click to toggle)
ruby-sprockets-rails 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 436 kB
  • sloc: ruby: 2,071; makefile: 4; javascript: 4
file content (48 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'action_view/helpers'
require 'sprockets'

module Sprockets
  module Rails
    module Context
      include ActionView::Helpers::AssetUrlHelper
      include ActionView::Helpers::AssetTagHelper

      def self.included(klass)
        klass.class_eval do
          class_attribute :config, :assets_prefix, :digest_assets
        end
      end

      def compute_asset_path(path, options = {})
        @dependencies << 'actioncontroller-asset-url-config'

        begin
          asset_uri = resolve(path)
        rescue FileNotFound
          # TODO: eh, we should be able to use a form of locate that returns
          # nil instead of raising an exception.
        end

        if asset_uri
          asset = link_asset(path)
          digest_path = asset.digest_path
          path = digest_path if digest_assets
          File.join(assets_prefix || "/", path)
        else
          super
        end
      end
    end
  end

  register_dependency_resolver 'actioncontroller-asset-url-config' do |env|
    config = env.context_class.config
    [config.relative_url_root,
    (config.asset_host unless config.asset_host.respond_to?(:call))]
  end

  # fallback to the default pipeline when using Sprockets 3.x
  unless config[:pipelines].include? :debug
    register_pipeline :debug, config[:pipelines][:default]
  end
end