Permalink
Browse files

Initial Entry

  • Loading branch information...
0 parents commit 555ddba9208e54085a3a1a62a6629abf1035326c Benjamin Bloch committed Feb 17, 2012
Showing with 634 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 .rspec
  3. +9 −0 .yardopts
  4. +19 −0 LICENSE
  5. +28 −0 README.md
  6. +30 −0 Rakefile
  7. +22 −0 lib/padrino-flash.rb
  8. +102 −0 lib/padrino-flash/helpers.rb
  9. +157 −0 lib/padrino-flash/storage.rb
  10. +6 −0 lib/padrino-flash/version.rb
  11. +25 −0 padrino-flash.gemspec
  12. +2 −0 spec/fixtures/locale/en.yml
  13. +15 −0 spec/flash_spec.rb
  14. +72 −0 spec/helpers_spec.rb
  15. +35 −0 spec/spec.rb
  16. +104 −0 spec/storage_spec.rb
@@ -0,0 +1,6 @@
+*.gem
+archive/*
+doc/*
+.yardoc
+.bundle
+Gemfile.lock
2 .rspec
@@ -0,0 +1,2 @@
+--format documentation
+--color
@@ -0,0 +1,9 @@
+--no-save
+--no-private
+--title Padrino Flash
+--markup-provider redcarpet
+--markup markdown
+lib/**/*.rb
+-
+README.md
+LICENSE
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Benjamin Bloch
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,28 @@
+### Overview
+
+Padrino Flash is a plugin for the [Padrino](https://github.com/padrino/padrino-framework) web framework which adds support for [Rails](https://github.com/rails/rails) like flash messages.
+
+### Setup & Installation
+
+Include it in your project's `Gemfile` with Bundler:
+
+``` ruby
+gem 'padrino-flash'
+```
+
+Modify your `app/app.rb` file to register the plugin:
+
+``` ruby
+class ExampleApplication < Padrino::Application
+ register Padrino::Flash
+end
+```
+
+### Dependencies
+
+* [Padrino-Core](https://github.com/padrino/padrino-framework) and [Padrino-Helpers](https://github.com/padrino/padrino-framework)
+* [Ruby](http://www.ruby-lang.org/en) >= 1.9.2
+
+### Copyright
+
+Copyright © 2012 Benjamin Bloch (Cirex). See LICENSE for details.
@@ -0,0 +1,30 @@
+$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
+require 'padrino-flash/version'
+
+require 'rake'
+require 'yard'
+require 'rspec'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new do |task|
+ task.pattern = 'spec/**/*_spec.rb'
+end
+
+YARD::Rake::YardocTask.new
+
+task :build do
+ `gem build padrino-flash.gemspec`
+end
+
+task :install => :build do
+ `gem install padrino-flash-#{Padrino::Flash::VERSION}.gem`
+end
+
+desc 'Releases the current version into the wild'
+task :release => :build do
+ `git tag -a v#{Padrino::Flash::VERSION} -m "Version #{Padrino::Flash::VERSION}"`
+ `gem push padrino-flash-#{Padrino::Flash::VERSION}.gem`
+ `git push --tags`
+end
+
+task :default => :spec
@@ -0,0 +1,22 @@
+# encoding: utf-8
+require 'padrino-core'
+require 'padrino-helpers'
+
+FileSet.glob_require('padrino-flash/**/*.rb', __FILE__)
+
+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
+ end # Flash
+end # Padrino
@@ -0,0 +1,102 @@
+# encoding: UTF-8
+module Padrino
+ module Flash
+ 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
@@ -0,0 +1,157 @@
+# encoding: UTF-8
+module Padrino
+ module Flash
+ 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
+ end # Flash
+end # Padrino
Oops, something went wrong.

0 comments on commit 555ddba

Please sign in to comment.