Module: Astro::Assets

Defined in:
lib/astro/assets.rb

Overview

A simple sinatra extension to set up the sprockets environment.

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra extension interface.



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
# File 'lib/astro/assets.rb', line 11

def self.registered( app )
  app.helpers do

    ##
    # The sprockets environment instance.
    def assets
      env[ 'astro.sprockets' ]
    end
  end

  # development and production environments use assets
  # in `app/assets`.
  app.configure :development, :production do |app|
    app.before do
      assets.append_path 'app/assets/javascripts'
    end
  end

  # the test environment uses assets in `test/assets`.
  app.configure :test do |app|
    app.before do
      assets.append_path 'spec/assets/javascripts'
    end
  end

  app.before do
    assets.register_preprocessor \
      'application/javascript', :astro do |context, data|

      ##
      # If we're processing the root asset, require all of
      # the assets in the assets list and return the data
      # as-is. The root processed asset will take care of
      # resolving the dependencies.
      if context.logical_path == 'astro'
        env[ 'astro.assets' ].each do |asset|
          context.require_asset( asset )
        end
      end

      data
    end
  end

end