Created
January 26, 2011 03:36
-
-
Save eltiare/796179 to your computer and use it in GitHub Desktop.
Hacking Sinatra's views to make organization easier when using multiple apps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
class Kazoo::Sinatra < Sinatra::Base | |
def render(engine, data, options = {}, *args) | |
if !options[:views] && data.is_a?(Symbol) && !data.to_s.match('/') | |
data = :"#{decamelize(self.class.name)}/#{data}" | |
options[:layout] ||= :'layouts/application' | |
end | |
if options[:layout] && !options[:layout].to_s.match('/') | |
options[:layout] = :"layouts/#{options[:layout]}" | |
end | |
super(engine, data, options, *args) | |
end | |
private | |
def decamelize(class_name) | |
parts = class_name.split('::') | |
parts.map { |part| | |
part.gsub(/([A-Z])/, '_\1').gsub(/(^\_)|(\_$)/, '').downcase | |
}.join('/') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment