aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/sprockets/railtie.rb
blob: fe3c8c9783f7c9b0553bd852990b272d8347fbdd (plain) (blame)
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
49
50
51
52
53
54
55
56
module Sprockets
  class Railtie < Rails::Railtie
    def self.using_coffee?
      require 'coffee-script'
      defined?(CoffeeScript)
    rescue LoadError
      false
    end

    def self.using_scss?
      require 'sass'
      defined?(Sass)
    rescue LoadError
      false
    end

    config.app_generators.javascript_engine :coffee if using_coffee?
    config.app_generators.stylesheet_engine :scss   if using_scss?

    # Configure ActionController to use sprockets.
    initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
      ActiveSupport.on_load(:action_controller) do
        self.use_sprockets = app.config.assets.enabled
      end
    end

    # We need to configure this after initialization to ensure we collect
    # paths from all engines. This hook is invoked exactly before routes
    # are compiled.
    config.after_initialize do |app|
      assets = app.config.assets
      next unless assets.enabled

      app.assets = asset_environment(app)
      app.routes.append do
        mount app.assets => assets.prefix
      end

      if config.action_controller.perform_caching
        app.assets = app.assets.index
      end
    end

    protected

    def asset_environment(app)
      require "sprockets"
      assets = app.config.assets
      env = Sprockets::Environment.new(app.root.to_s)
      env.static_root = File.join(app.root.join("public"), assets.prefix)
      env.paths.concat assets.paths
      env.logger = Rails.logger
      env
    end
  end
end