Module: Rack::RespondTo

Defined in:
lib/sinatra/rack_accept.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

This method is triggered after this helper is registred within Sinatra. We need to overide the default render method to supply correct path to the template, since Sinatra is by default looking in the current __FILE__ path



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sinatra/rack_accept.rb', line 25

def self.registered(app)
  app.use Rack::Accept
  app.use Rack::MediaType
  app.helpers Rack::RespondTo::Helpers
  app.class_eval do
    alias :render_without_format :render
    def render(*args, &block)
      begin
        assumed_layout = args[1] == :layout
        args[1] = "#{args[1]}.#{@media_type}".to_sym if args[1].is_a?(::Symbol)
        render_without_format(*args, &block)
      rescue Errno::ENOENT => e
        raise "ERROR: Missing template: #{args[1]}.#{args[0]}" unless assumed_layout
        raise e
      end
    end
    private :render
  end
end