Skip to content

Commit

Permalink
Added Flash helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAddYE committed Sep 26, 2011
1 parent 10031f7 commit 3d4c391
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -29,4 +29,4 @@ In your Padrino project edit:
# app.rb # app.rb
class MyApp < Padrino::Application class MyApp < Padrino::Application
register Padrino::Contrib::ExceptionNotifier register Padrino::Contrib::ExceptionNotifier
end end
11 changes: 10 additions & 1 deletion lib/padrino-contrib.rb
Expand Up @@ -8,6 +8,15 @@ module Contrib
module Helpers module Helpers
autoload :AssetsCompressor, 'padrino-contrib/helpers/assets_compressor.rb' autoload :AssetsCompressor, 'padrino-contrib/helpers/assets_compressor.rb'
autoload :JQuery, 'padrino-contrib/helpers/jquery.rb' autoload :JQuery, 'padrino-contrib/helpers/jquery.rb'
autoload :Flash, 'padrino-contrib/helpers/flash.rb'
end # Helpers end # Helpers
end # Contrib end # Contrib
end # Padrino end # Padrino

if defined?(ActiveRecord)
Dir[File.join(File.expand_path('../', __FILE__), '/padrino-contrib/orm/active_record/**/*.rb')].each { |d| require d }
end

if defined?(MongoMapper)
Dir[File.join(File.expand_path('../', __FILE__), '/padrino-contrib/orm/mongo_mapper/**/*.rb')].each { |d| require d }
end
6 changes: 3 additions & 3 deletions lib/padrino-contrib/auto_locale.rb
Expand Up @@ -22,7 +22,7 @@ module Helpers
# This reload the page changing the I18n.locale # This reload the page changing the I18n.locale
# #
def switch_to_lang(lang) def switch_to_lang(lang)
request.path_info.sub(/\/#{I18n.locale}/, "/#{lang}") if options.locales.include?(lang) request.path_info.sub(/\/#{I18n.locale}/, "/#{lang}") if settings.locales.include?(lang)
end end
end # Helpers end # Helpers


Expand All @@ -31,10 +31,10 @@ def self.registered(app)
app.extend ClassMethods app.extend ClassMethods
app.set :locales, [:en] app.set :locales, [:en]
app.before do app.before do
if request.path_info =~ /^\/(#{options.locales.join('|')})\b/ if request.path_info =~ /^\/(#{settings.locales.join('|')})\b/
I18n.locale = $1.to_sym I18n.locale = $1.to_sym
else else
I18n.locale = options.locales[0] I18n.locale = settings.locales[0]
not_found if request.path_info !~ /^\/?$/ not_found if request.path_info !~ /^\/?$/
end end
end end
Expand Down
10 changes: 5 additions & 5 deletions lib/padrino-contrib/exception_notifier.rb
Expand Up @@ -20,11 +20,11 @@ module Contrib
module ExceptionNotifier module ExceptionNotifier


def self.registered(app) def self.registered(app)
app.set :exceptions_subject, "Exception" app.set :exceptions_subject, "Exception" unless app.respond_to?(:exceptions_subject)
app.set :exceptions_to, "errors@localhost.local" app.set :exceptions_to, "errors@localhost.local" unless app.respond_to?(:exceptions_to)
app.set :exceptions_from, "foo@bar.local" app.set :exceptions_from, "foo@bar.local" unless app.respond_to?(:exceptions_from)
app.set :exceptions_layout, :layout app.set :exceptions_layout, :layout unless app.respond_to?(:exceptions_layout)
app.set :redmine, {} app.set :redmine, {} unless app.respond_to?(:redmine)
app.error 500 do app.error 500 do
boom = env['sinatra.error'] boom = env['sinatra.error']
body = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ") body = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
Expand Down
2 changes: 1 addition & 1 deletion lib/padrino-contrib/flash_session.rb
Expand Up @@ -34,4 +34,4 @@ def call(env)
end end
end # FlashSession end # FlashSession
end # Contrib end # Contrib
end # Padrino end # Padrino
2 changes: 1 addition & 1 deletion lib/padrino-contrib/helpers/assets_compressor.rb
Expand Up @@ -148,4 +148,4 @@ def find_last_modified(file)
end # AssetsCompressor end # AssetsCompressor
end # Helpers end # Helpers
end # Contrib end # Contrib
end # Padrino end # Padrino
32 changes: 32 additions & 0 deletions lib/padrino-contrib/helpers/flash.rb
@@ -0,0 +1,32 @@
module Padrino
module Contrib
module Helpers
##
# Dead simple version of rack-flash.
# You still use flash without session, but remember that
# they can't work after a redirect.
#
# ==== Usage:
#
# register Padrino::Contrib::Helpers::Flash
#
module Flash
def self.register(app)
app.before { @_flash, session[:_flash] = session[:_flash], nil if settings.sessions? && session[:_flash] }
app.helpers InstanceMethods
end

module InstanceMethods
def flash
@_flash ||= {}
end

def redirect(uri, *args)
session[:_flash] = @_flash if settings.sessions? && flash.present?
super(uri, *args)
end
end
end # Flash
end # Helpers
end # Contrib
end # Padrino
2 changes: 1 addition & 1 deletion lib/padrino-contrib/helpers/jquery.rb
Expand Up @@ -59,4 +59,4 @@ def javascript_include_tag_jquery(options={})
end # JQuery end # JQuery
end # Helpers end # Helpers
end # Contrib end # Contrib
end # Padrino end # Padrino
2 changes: 1 addition & 1 deletion lib/padrino-contrib/orm/active_record/permalink.rb
Expand Up @@ -38,7 +38,7 @@ def generate_permalink
end end
end # InstanceMethods end # InstanceMethods
end # Permalink end # Permalink
end # Ar end # ActiveRecord
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
Expand Down
2 changes: 1 addition & 1 deletion lib/padrino-contrib/orm/active_record/permalink_i18n.rb
Expand Up @@ -56,7 +56,7 @@ def generate_permalinks
end end
end # InstanceMethods end # InstanceMethods
end # PemalinkI18n end # PemalinkI18n
end # Ar end # ActiveRecord
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
Expand Down
7 changes: 3 additions & 4 deletions lib/padrino-contrib/orm/active_record/textile.rb
@@ -1,5 +1,3 @@
require 'RedCloth'

module Padrino module Padrino
module Contrib module Contrib
module Orm module Orm
Expand All @@ -18,6 +16,7 @@ module ActiveRecord
module Textile module Textile
module ClassMethods module ClassMethods
def has_textile(*fields) def has_textile(*fields)
require 'RedCloth'
include InstanceMethods include InstanceMethods
options = fields.extract_options! options = fields.extract_options!
options.reverse_merge!(:internal_links => :blog) options.reverse_merge!(:internal_links => :blog)
Expand Down Expand Up @@ -52,8 +51,8 @@ def generate_textile
end end
end end
end # InstanceMethods end # InstanceMethods
end # Permalink end # Textile
end # Ar end # ActiveRecord
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
Expand Down
6 changes: 3 additions & 3 deletions lib/padrino-contrib/orm/active_record/translate.rb
Expand Up @@ -35,9 +35,9 @@ def method_missing(method_name, *arguments)
super super
end end
end # InstanceMethods end # InstanceMethods
end # ArTranslate end # Translate
end # Ar end # ActiveRecord
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
::ActiveRecord::Base.extend(Padrino::Contrib::Orm::ActiveRecord::Translate::ClassMethods) ::ActiveRecord::Base.extend(Padrino::Contrib::Orm::ActiveRecord::Translate::ClassMethods)
2 changes: 1 addition & 1 deletion lib/padrino-contrib/orm/mongo_mapper/permalink.rb
Expand Up @@ -47,7 +47,7 @@ def generate_permalink
end end
end # InstanceMethods end # InstanceMethods
end # Permalink end # Permalink
end # Mm end # MongoMapper
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
Expand Down
2 changes: 1 addition & 1 deletion lib/padrino-contrib/orm/mongo_mapper/search.rb
Expand Up @@ -32,7 +32,7 @@ def search(text, options={})
end end
end end
end # Permalink end # Permalink
end # Mm end # MongoMapper
end # Orm end # Orm
end # Contrib end # Contrib
end # Padrino end # Padrino
Expand Down
2 changes: 1 addition & 1 deletion spec/autoload_spec.rb
Expand Up @@ -18,4 +18,4 @@


it(klass) { eval("#{klass}").should be_true } it(klass) { eval("#{klass}").should be_true }
end end
end end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,3 +1,3 @@
require 'rubygems' unless defined?(Gem) require 'rubygems' unless defined?(Gem)
require 'rspec' unless defined?(RSpec) require 'rspec' unless defined?(RSpec)
require 'padrino-contrib' require 'padrino-contrib'

0 comments on commit 3d4c391

Please sign in to comment.