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

padrino-flash into padrino-core #904

Merged
merged 1 commit into from Aug 8, 2012
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
15 changes: 8 additions & 7 deletions padrino-core/lib/padrino-core/application.rb
@@ -1,5 +1,6 @@
require 'padrino-core/application/rendering'
require 'padrino-core/application/routing'
require 'padrino-core/application/flash'
require 'padrino-core/application/showexceptions'

module Padrino
Expand Down Expand Up @@ -184,7 +185,7 @@ def default_configuration!
set :uri_root, '/'
set :app_name, settings.to_s.underscore.to_sym
set :default_builder, 'StandardFormBuilder'
set :flash, defined?(Sinatra::Flash) || defined?(Rack::Flash)
set :flash, defined?(Padrino::Flash) || defined?(Rack::Flash)
set :authentication, false
# Padrino locale
set :locale_path, Proc.new { Dir[File.join(settings.root, '/locale/**/*.{rb,yml}')] }
Expand Down Expand Up @@ -256,14 +257,14 @@ def setup_default_middleware(builder)
end

# TODO Remove this in a few versions (rack-flash deprecation)
# Move register Sinatra::Flash into setup_default_middleware
# Initializes flash using sinatra-flash or rack-flash
# Initializes flash using padrino-flash or rack-flash
def setup_flash(builder)
register Sinatra::Flash if flash? && defined?(Sinatra::Flash)
if defined?(Rack::Flash) && !defined?(Sinatra::Flash)
register Padrino::Flash if flash? && defined?(Padrino::Flash)
if defined?(Rack::Flash) && !defined?(Padrino::Flash)
logger.warn %Q{
[Deprecation] In Gemfile, 'rack-flash' should be replaced with 'sinatra-flash'!
Rack-Flash is not compatible with later versions of Rack and should be replaced.
[Deprecation] 'rack-flash' can be removed in Gemfile.
Rack-Flash is not compatible with later versions of Rack and is
replaced with Padrino's own Flash-Helpers.
}
builder.use Rack::Flash, :sweep => true if flash?
end
Expand Down
271 changes: 271 additions & 0 deletions padrino-core/lib/padrino-core/application/flash.rb
@@ -0,0 +1,271 @@
# encoding: utf-8

module Padrino
module Flash

class << self
# @private
def registered(app)
app.helpers Helpers
app.enable :sessions
app.enable :flash

app.after do
session[:_flash] = @_flash.next if @_flash && app.flash?
end
end
end # self

class Storage
include Enumerable

attr_reader :now
attr_reader :next

# @private
def initialize(session)
@now = session || {}
@next = {}
end

# @since 0.1.0
# @api public
def [](type)
@now[type]
end

# @since 0.1.0
# @api public
def []=(type, message)
@next[type] = message
end

# @since 0.1.0
# @api public
def delete(type)
@now.delete(type)
self
end

# @since 0.1.0
# @api public
def keys
@now.keys
end

# @since 0.1.0
# @api public
def key?(type)
@now.key?(type)
end

# @since 0.1.0
# @api public
def each(&block)
@now.each(&block)
end

# @since 0.1.0
# @api public
def replace(hash)
@now.replace(hash)
self
end

# @since 0.1.0
# @api public
def update(hash)
@now.update(hash)
self
end
alias_method :merge!, :update

# @since 0.1.0
# @api public
def sweep
@now.replace(@next)
@next = {}
self
end

# @since 0.1.0
# @api public
def keep(key = nil)
if key
@next[key] = @now[key]
else
@next.merge!(@now)
end
end

# @since 0.1.0
# @api public
def discard(key = nil)
if key
@next.delete(key)
else
@next = {}
end
end

# @since 0.1.0
# @api public
def clear
@now.clear
end

# @since 0.1.0
# @api public
def empty?
@now.empty?
end

# @since 0.1.0
# @api public
def to_hash
@now.dup
end

# @since 0.1.0
# @api public
def to_s
@now.to_s
end

# @since 0.1.0
# @api public
def error=(message)
self[:error] = message
end

# @since 0.1.0
# @api public
def error
self[:error]
end

# @since 0.1.0
# @api public
def notice=(message)
self[:notice] = message
end

# @since 0.1.0
# @api public
def notice
self[:notice]
end

# @since 0.1.0
# @api public
def success=(message)
self[:success] = message
end

# @since 0.1.0
# @api public
def success
self[:success]
end
end # Storage

module Helpers
###
# Overloads the existing redirect helper in-order to provide support for flash messages
#
# @overload redirect(url)
# @param [String] url
#
# @overload redirect(url, status_code)
# @param [String] url
# @param [Fixnum] status_code
#
# @overload redirect(url, status_code, flash_messages)
# @param [String] url
# @param [Fixnum] status_code
# @param [Hash] flash_messages
#
# @overload redirect(url, flash_messages)
# @param [String] url
# @param [Hash] flash_messages
#
# @example
# redirect(dashboard, :success => :user_created)
# redirect(new_location, 301, notice: 'This page has moved. Please update your bookmarks!!')
#
# @since 0.1.0
# @api public
def redirect(url, *args)
flashes = args.extract_options!

flashes.each do |type, message|
message = I18n.translate(message) if message.is_a?(Symbol)
flash[type] = message
end

super(url, args)
end
alias_method :redirect_to, :redirect

###
# Returns HTML tags to display the current flash messages
#
# @return [String]
# Generated HTML for flash messages
#
# @example
# flash_messages
# # => <div id="flash">
# # => <span class="warning" title="Warning">Danger, Will Robinson!</span>
# # => </div>
#
# @since 0.1.0
# @api public
def flash_messages
flashes = flash.collect do |type, message|
content_tag(:span, message, :title => type.to_s.titleize, :class => type)
end.join("\n")

# Create the tag even if we don't need it so it can be dynamically altered
content_tag(:div, flashes, :id => 'flash')
end

###
# Returns an HTML div to display the specified flash if it exists
#
# @return [String]
# Generated HTML for the specified flash message
#
# @example
# flash_message :error
# # => <div id="flash-error" title="Error" class="error">
# # => Invalid Handle/Password Combination
# # => </div>
#
# flash_message :success
# # => <div id="flash-success" title="Success" class="success">
# # => Your account has been successfully registered!
# # => </div>
#
# @since 0.1.0
# @api public
def flash_message(type)
if flash[type]
content_tag(:div, flash[type], :id => "flash-#{type}", :title => type.to_s.titleize, :class => type)
end
end

###
# Returns the flash storage object
#
# @return [Storage]
#
# @since 0.1.0
# @api public
def flash
@_flash ||= Storage.new(session[:_flash])
end
end # Helpers

end # Flash
end # Padrino
Expand Up @@ -6,7 +6,6 @@ source :rubygems

# Project requirements
gem 'rake'
gem 'sinatra-flash', :require => 'sinatra/flash'

# Component requirements

Expand Down