Module: Appetizer::UI

Defined in:
lib/appetizer/ui.rb,
lib/appetizer/ui/spec.rb,
lib/appetizer/ui/assets.rb,
lib/appetizer/ui/assets/delivery.rb

Defined Under Namespace

Modules: Assets, Spec

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appetizer/ui.rb', line 10

def self.registered app

  # Make sure that exception handling works the same in
  # development and production.

  app.set :show_exceptions, false

  # All production apps better be using SSL.

  app.configure :production do
    require "rack/ssl"
    app.use Rack::SSL
  end

  # This stack in primarily intended for deployment on Heroku, so
  # only bother to log requests in development mode. Heroku's
  # logging is more than enough in production.

  app.configure :development do
    app.use Rack::CommonLogger, App.log
  end

  # Build CSS under tmp, not in the project root.

  app.set :scss, cache_location: "tmp/sass-cache", style: :compact

  # Set up cookie sessions and authenticity token checking. Add
  # some basic defaults, but allow them to be overridden.

  app.use Rack::Session::Cookie,
    key:    (ENV["APPETIZER_COOKIE_NAME"] || "app-session"),
    secret: (ENV["APPETIZER_SESSION_SECRET"] || "app-session-secret")

  app.use Rack::Protection::AuthenticityToken

  app.helpers do

    # JSONify `thing` and respond with a `201`.

    def created thing
      halt 201, json(thing)
    end

    # The current CSRF token.

    def csrf
      session[:csrf] ||= SecureRandom.hex 32
    end

    # Set a `:json` content-type and run `thing` through the Yajl
    # JSON encoder.

    def json thing
      content_type :json, charset: "utf-8"
      jsonify thing
    end

    # Encode `thing` as JSON.

    def jsonify thing
      Yajl::Encoder.encode thing
    end

    # The asset manifest.

    def manifest
      Appetizer::UI::Assets.manifest
    end
  end
end