module Sinatra::ChassisHelpers

Public: Helpers used in the Chassis module. Can be overriden from an app.

Public Instance Methods

find_template(views, name, engine, &block) click to toggle source

Public: Prepends the Sinatra find_template method to find .mobile templates if the mobile_views setting is true the request is from a mobile device.

Example

erb :my_view

Renders the my_view.mobile.erb instead of my_view.erb.

Calls superclass method
# File lib/sinatra/chassis.rb, line 47
def find_template views, name, engine, &block
  enable :reload_templates if settings.mobile_views
  name = "#{name}.mobile"   if
    (settings.mobile_views) &&
    (mobile_request?)       &&
    (view_exists?("#{name}.mobile.#{@preferred_extension}"))
  Array(views).each { |v| super(v, name, engine, &block) }
end
mobile_request?() click to toggle source

Public: Compares the request user agent against the mobile_user_agents setting Array items (should contain regexes).

Example

mobile_request?
# => true

Returns true/false.

# File lib/sinatra/chassis.rb, line 18
def mobile_request?
  settings.mobile_user_agents.any? { |agent| request.env['HTTP_USER_AGENT'] =~ agent }
end
view_exists?(template) click to toggle source

Public: Checks to see if a view template exists. It will check through all registered view directories.

template - String of view template filename to look for

Example

view_template('show.haml')
# => true

Returns true/false.

# File lib/sinatra/chassis.rb, line 33
def view_exists? template
  Array(settings.views).each { |v| return true if File.exists?("#{v}/#{template}") }
  false
end