Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More code cleanup #1448

Merged
merged 4 commits into from Oct 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 18 additions & 23 deletions padrino-core/lib/padrino-core.rb
Expand Up @@ -62,18 +62,10 @@ def env
# No applications were mounted.
#
def application
raise ApplicationLoadError, "At least one app must be mounted!" unless Padrino.mounted_apps && Padrino.mounted_apps.any?
raise ApplicationLoadError, "At least one app must be mounted!" unless Padrino.mounted_apps.present?
router = Padrino::Router.new
Padrino.mounted_apps.each { |app| app.map_onto(router) }

if middleware.present?
builder = Rack::Builder.new
middleware.each { |c,a,b| builder.use(c, *a, &b) }
builder.run(router)
builder.to_app
else
router
end
middleware.present? ? add_middleware(router) : router
end

##
Expand All @@ -91,21 +83,14 @@ def application
#
def configure_apps(&block)
return unless block_given?
@@_global_configurations ||= []
@@_global_configurations << block
@_global_configuration = lambda do |app|
@@_global_configurations.each do |configuration|
app.class_eval(&configuration)
end
end
global_configurations << block
end

##
# Returns project-wide configuration settings defined in
# {configure_apps} block.
# Stores global configuration blocks.
#
def apps_configuration
@_global_configuration
def global_configurations
@_global_configurations ||= []
end

##
Expand All @@ -129,6 +114,16 @@ def set_encoding
nil
end

##
# Creates Rack stack with the router added to the middleware chain.
#
def add_middleware(router)
builder = Rack::Builder.new
middleware.each{ |mw,args,block| builder.use(mw, *args, &block) }
builder.run(router)
builder.to_app
end

##
# A Rack::Builder object that allows to add middlewares in front of all
# Padrino applications.
Expand Down Expand Up @@ -162,8 +157,8 @@ def clear_middleware!
# @yield []
# The given block will be passed to the initialized middleware.
#
def use(m, *args, &block)
middleware << [m, args, block]
def use(mw, *args, &block)
middleware << [mw, args, block]
end

##
Expand Down
103 changes: 67 additions & 36 deletions padrino-core/lib/padrino-core/application.rb
Expand Up @@ -4,9 +4,6 @@
require 'padrino-core/application/showexceptions'

module Padrino
class ApplicationSetupError < RuntimeError
end

##
# Subclasses of this become independent Padrino applications
# (stemming from Sinatra::Application).
Expand All @@ -31,15 +28,8 @@ def inherited(base)
begun_at = Time.now
CALLERS_TO_IGNORE.concat(PADRINO_IGNORE_CALLERS)
base.default_configuration!
base.prerequisites.concat([
File.join(base.root, '/models.rb'),
File.join(base.root, '/models/**/*.rb'),
File.join(base.root, '/lib.rb'),
File.join(base.root, '/lib/**/*.rb')
]).uniq!
Padrino.require_dependencies(base.prerequisites)
logger.devel :setup, begun_at, base
super(base) # Loading the subclass inherited method
super(base)
end

##
Expand All @@ -57,7 +47,7 @@ def reload!
logger.devel "Reloading application #{settings}"
reset!
reset_router!
Padrino.require_dependencies(settings.app_file, :force => true) # Reload the app file
Padrino.require_dependencies(settings.app_file, :force => true)
require_dependencies
default_filters!
default_routes!
Expand Down Expand Up @@ -108,7 +98,6 @@ def setup_application!
I18n.reload!
end
@_configured = true
@_configured
end

##
Expand All @@ -128,7 +117,13 @@ def run!(options={})
# directory that need to be added to +$LOAD_PATHS+ from this application
#
def load_paths
@_load_paths ||= %w[models lib mailers controllers helpers].map { |path| File.join(settings.root, path) }
@_load_paths ||= [
'models',
'lib',
'mailers',
'controllers',
'helpers',
].map { |path| File.join(settings.root, path) }
end

##
Expand All @@ -144,8 +139,14 @@ def load_paths
#
def dependencies
[
'urls.rb', 'config/urls.rb', 'mailers/*.rb', 'mailers.rb',
'controllers/**/*.rb', 'controllers.rb', 'helpers/**/*.rb', 'helpers.rb'
'urls.rb',
'config/urls.rb',
'mailers/*.rb',
'mailers.rb',
'controllers/**/*.rb',
'controllers.rb',
'helpers/**/*.rb',
'helpers.rb',
].map { |file| Dir[File.join(settings.root, file)] }.flatten
end

Expand All @@ -169,37 +170,56 @@ def prerequisites
end

protected

##
# Defines default settings for Padrino application.
#
def default_configuration!
# Overwriting Sinatra defaults
set :app_file, File.expand_path(caller_files.first || $0) # Assume app file is first caller
set :app_file, File.expand_path(caller_files.first || $0)
set :app_name, settings.to_s.underscore.to_sym

set :environment, Padrino.env
set :reload, Proc.new { development? }
set :logging, Proc.new { development? }

set :method_override, true
set :sessions, false
set :public_folder, Proc.new { Padrino.root('public', uri_root) }
set :views, Proc.new { File.join(root, 'views') }
set :images_path, Proc.new { File.join(public_folder, 'images') }
set :protection, true
set :default_builder, 'StandardFormBuilder'

set :haml, { :ugly => (Padrino.env == :production) } if defined?(Haml)
default_paths!
default_security!
global_configuration!
setup_prerequisites!
end

# Padrino specific
set :uri_root, '/'
set :app_name, settings.to_s.underscore.to_sym
set :default_builder, 'StandardFormBuilder'
set :authentication, false
def setup_prerequisites!
prerequisites.concat(default_prerequisites).uniq!
Padrino.require_dependencies(prerequisites)
end

set :locale_path, Proc.new { Dir[File.join(settings.root, '/locale/**/*.{rb,yml}')] }
def default_paths!
set :locale_path, Proc.new { Dir.glob File.join(root, 'locale/**/*.{rb,yml}') }
set :views, Proc.new { File.join(root, 'views') }

# Authenticity token
set :uri_root, '/'
set :public_folder, Proc.new { Padrino.root('public', uri_root) }
set :images_path, Proc.new { File.join(public_folder, 'images') }
end

def default_security!
set :protection, true
set :authentication, false
set :sessions, false
set :protect_from_csrf, false
set :allow_disabled_csrf, false
# Load the Global Configurations
class_eval(&Padrino.apps_configuration) if Padrino.apps_configuration
end

##
# Applies global padrino configuration blocks to current application.
#
def global_configuration!
Padrino.global_configurations.each do |configuration|
class_eval(&configuration)
end
end

##
Expand All @@ -221,9 +241,7 @@ def default_routes!
#
def default_filters!
before do
unless @_content_type
response['Content-Type'] = 'text/html;charset=utf-8'
end
response['Content-Type'] = 'text/html;charset=utf-8' unless @_content_type
end
end

Expand All @@ -250,7 +268,20 @@ def require_dependencies
Padrino.require_dependencies(dependencies, :force => true)
end

##
# Returns globs of default paths of application prerequisites.
#
def default_prerequisites
[
'/models.rb',
'/models/**/*.rb',
'/lib.rb',
'/lib/**/*.rb',
].map{ |glob| File.join(settings.root, glob) }
end

private

# Overrides the default middleware for Sinatra based on Padrino conventions.
# Also initializes the application after setting up the middleware.
def setup_default_middleware(builder)
Expand Down
Expand Up @@ -40,15 +40,18 @@ class Template < Tilt::ErubisTemplate
def render(*args)
app = args.first
app_class = app.class
@padrino_app = app.kind_of?(Padrino::Application) ||
(app_class.respond_to?(:erb) && app_class.erb[:engine_class] == Padrino::Erubis::SafeBufferTemplate)
@is_padrino_app = app.kind_of?(Padrino::Application) ||
(app_class.respond_to?(:erb) && app_class.erb[:engine_class] == Padrino::Erubis::SafeBufferTemplate)
super
end

##
# In preamble we need a flag `__in_erb_template` and SafeBuffer for padrino apps.
#
def precompiled_preamble(locals)
buf = @padrino_app ? "ActiveSupport::SafeBuffer.new" : "''"
old_postamble = super.split("\n")[0..-2]
[old_postamble, "__in_erb_template = true;#{@outvar} = _buf = (#{@outvar} || #{buf})"].join("\n")
original = super
return original unless @is_padrino_app
"__in_erb_template = true\n" << original.rpartition("\n").first << "#{@outvar} = _buf = ActiveSupport::SafeBuffer.new\n"
end
end
end
Expand All @@ -57,8 +60,9 @@ def precompiled_preamble(locals)
Tilt.prefer(Padrino::Erubis::Template, :erb)

if defined? Padrino::Rendering
Padrino::Rendering.engine_configurations[:erb] =
{:engine_class => Padrino::Erubis::SafeBufferTemplate}
Padrino::Rendering.engine_configurations[:erb] = {
:engine_class => Padrino::Erubis::SafeBufferTemplate,
}
end
rescue LoadError
end
Expand Up @@ -17,8 +17,9 @@ def self.rails_xss_safe?
end

if defined? Padrino::Rendering
Padrino::Rendering.engine_configurations[:haml] =
{:escape_html => true}
Padrino::Rendering.engine_configurations[:haml] = {
:escape_html => true,
}

class Tilt::HamlTemplate
include Padrino::Rendering::SafeTemplate
Expand Down
Expand Up @@ -2,15 +2,18 @@
require 'slim'

if defined? Padrino::Rendering
Padrino::Rendering.engine_configurations[:slim] =
{:generator => Temple::Generators::RailsOutputBuffer,
:buffer => "@_out_buf", :use_html_safe => true, :disable_capture => true}
Padrino::Rendering.engine_configurations[:slim] = {
:generator => Temple::Generators::RailsOutputBuffer,
:buffer => "@_out_buf",
:use_html_safe => true,
:disable_capture => true,
}

class Slim::Template
include Padrino::Rendering::SafeTemplate

def precompiled_preamble(locals)
"__in_slim_template = true;#{super}"
"__in_slim_template = true\n" << super
end
end
end
Expand Down
13 changes: 0 additions & 13 deletions padrino-core/test/test_application.rb
Expand Up @@ -25,19 +25,6 @@ class PadrinoTestApp2 < Padrino::Application; end
assert !Padrino.configure_apps
end

should 'check haml options on production' do
assert defined?(Haml), 'Haml not defined'
assert_equal :test, PadrinoPristine.environment
assert !PadrinoPristine.haml[:ugly]
Padrino.stub :env, :production do
PadrinoPristine.send :default_configuration!
assert_equal :production, Padrino.env
assert_equal :production, PadrinoPristine.environment
assert PadrinoPristine.haml[:ugly]
PadrinoPristine.environment = :test
end
end

should 'check padrino specific options' do
assert !PadrinoPristine.instance_variable_get(:@_configured)
PadrinoPristine.send(:setup_application!)
Expand Down