bootstrap_flash_messages 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +133 -0
- data/Rakefile +2 -0
- data/bootstrap_flash_messages.gemspec +21 -0
- data/init.rb +2 -0
- data/lib/bootstrap_flash_messages/flash_messages.rb +37 -0
- data/lib/bootstrap_flash_messages/helpers.rb +29 -0
- data/lib/bootstrap_flash_messages/railtie.rb +9 -0
- data/lib/bootstrap_flash_messages/version.rb +3 -0
- data/lib/bootstrap_flash_messages.rb +16 -0
- data/lib/generators/bootstrap_flash_messages/USAGE +7 -0
- data/lib/generators/bootstrap_flash_messages/locale_generator.rb +9 -0
- data/lib/generators/bootstrap_flash_messages/templates/flash.en.yml +35 -0
- metadata +81 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# bootstrap_flash_messages
|
2
|
+
|
3
|
+
version 0.0.1
|
4
|
+
Robin Brouwer
|
5
|
+
45north
|
6
|
+
|
7
|
+
Bootstrap alerts and Rails flash messages combined in one easy-to-use gem.
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
You can use this gem by putting the following inside your Gemfile:
|
13
|
+
|
14
|
+
gem "bootstrap_flash_messages"
|
15
|
+
|
16
|
+
Now you need flash.en.yml for the flash messages.
|
17
|
+
|
18
|
+
rails g bootstrap_flash_messages:locale
|
19
|
+
|
20
|
+
And that's it!
|
21
|
+
|
22
|
+
|
23
|
+
## Changes
|
24
|
+
|
25
|
+
Version 0.0.1 changes (08/08/2012):
|
26
|
+
|
27
|
+
- Changed redirect_to method
|
28
|
+
- Added flash! and flash_now! methods
|
29
|
+
- Added flash_messages helper
|
30
|
+
- Made a gem out of it
|
31
|
+
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
You need [Twitter Bootstrap](http://twitter.github.com/bootstrap) for the styling and close button. You can still use it without Bootstrap, but you need to style it yourself. This gem uses the [Bootstrap alerts](http://twitter.github.com/bootstrap/components.html#alerts).
|
36
|
+
|
37
|
+
All flash messages are defined inside config/locales/flash.en.yml. They are nested like this:
|
38
|
+
|
39
|
+
en:
|
40
|
+
flash_messages:
|
41
|
+
controller_name:
|
42
|
+
action_name:
|
43
|
+
success: "It worked!"
|
44
|
+
info: "There's something you need to know..."
|
45
|
+
warning: "You should watch out now."
|
46
|
+
error: "Oh no! Something went wrong."
|
47
|
+
|
48
|
+
You have four keys:
|
49
|
+
|
50
|
+
:success
|
51
|
+
:info
|
52
|
+
:warning
|
53
|
+
:error
|
54
|
+
|
55
|
+
When you've defined the messages it's really easy to call them inside your Controller.
|
56
|
+
|
57
|
+
class PostsController
|
58
|
+
def create
|
59
|
+
@post = Post.new(params[:post])
|
60
|
+
if @post.save
|
61
|
+
redirect_to(@post, :flash => :success)
|
62
|
+
else
|
63
|
+
flash_now!(:error)
|
64
|
+
render("new")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
You can use the `:flash` parameter inside the `redirect_to` method (note: this gem changes the redirect_to method!) to set the flash messages. You only need to pass the corresponding key and the gem will automatically set `gflash[:success]` to `t("flash_messages.posts.create.success")`. The `:flash` parameter also accepts an Array.
|
70
|
+
|
71
|
+
redirect_to(@post, :flash => [:success, :info])
|
72
|
+
|
73
|
+
You can still use a Hash to use the default `redirect_to` behavior. When you don't want to use the `redirect_to` method to set the flash messages, you can use the `flash!` method.
|
74
|
+
|
75
|
+
flash!(:success, :info)
|
76
|
+
redirect_to(@post)
|
77
|
+
|
78
|
+
When you need to use `flash.now` you can use the new `flash_now!` method.
|
79
|
+
|
80
|
+
flash_now!(:error, :warning)
|
81
|
+
|
82
|
+
You use `flash_now!` in combination with rendering a view instead of redirecting.
|
83
|
+
|
84
|
+
Now's the time to show these messages to the user. Inside the layout (or any other view), add the following:
|
85
|
+
|
86
|
+
<div id="flash_messages"><%= flash_messages %></div>
|
87
|
+
|
88
|
+
And that's it! To change the flash messages inside a `.js.erb` file, you can do the following:
|
89
|
+
|
90
|
+
$("#flash_messages").html("#{j(flash_messages)}");
|
91
|
+
|
92
|
+
The `flash_messages` helper shows a simple Bootstrap alert box. If you want to add a close button you can add the :close option.
|
93
|
+
|
94
|
+
<%= flash_messages(:close) %>
|
95
|
+
|
96
|
+
Want to use the `.alert-block` class? Just add :block.
|
97
|
+
|
98
|
+
<%= flash_messages(:close, :block) %>
|
99
|
+
|
100
|
+
Want a heading? Add :heading. The headings inside flash.en.yml are used.
|
101
|
+
|
102
|
+
<%= flash_messages(:close, :block, :heading) %>
|
103
|
+
|
104
|
+
And that's it! Have fun. :)
|
105
|
+
|
106
|
+
|
107
|
+
## Why I created this gem
|
108
|
+
|
109
|
+
I created the [gritter gem](https://github.com/RobinBrouwer/gritter) and used it in a lot of projects.
|
110
|
+
I started using Twitter Bootstrap and really liked the alerts. I loved the way gritter allowed you to set messages
|
111
|
+
and decided to do the same for the Bootstrap alerts. And this is the result!
|
112
|
+
|
113
|
+
## Copyright
|
114
|
+
|
115
|
+
Copyright (C) 2012 Robin Brouwer
|
116
|
+
|
117
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
118
|
+
this software and associated documentation files (the "Software"), to deal in
|
119
|
+
the Software without restriction, including without limitation the rights to
|
120
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
121
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
122
|
+
so, subject to the following conditions:
|
123
|
+
|
124
|
+
The above copyright notice and this permission notice shall be included in all
|
125
|
+
copies or substantial portions of the Software.
|
126
|
+
|
127
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
128
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
129
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
130
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
131
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
132
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
133
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "bootstrap_flash_messages/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "bootstrap_flash_messages"
|
7
|
+
s.version = BootstrapFlashMessages::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Robin Brouwer"]
|
10
|
+
s.email = ["robin@45north.nl"]
|
11
|
+
s.homepage = "http://www.45north.nl"
|
12
|
+
s.summary = %q{Bootstrap alerts and Rails flash messages combined in one easy-to-use gem.}
|
13
|
+
s.description = %q{Bootstrap alerts and Rails flash messages combined in one easy-to-use gem.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "nowarning"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module BootstrapFlashMessages
|
2
|
+
module FlashMessages
|
3
|
+
def redirect_to(options = {}, response_status_and_flash = {})
|
4
|
+
messages = response_status_and_flash[:flash]
|
5
|
+
if messages && (messages.is_a?(Symbol) || messages.is_a?(Array))
|
6
|
+
flashes = {}
|
7
|
+
if messages.is_a?(Array)
|
8
|
+
messages.each do |key|
|
9
|
+
flashes[key] = flash_messages(params[:controller], params[:action], key)
|
10
|
+
end
|
11
|
+
else
|
12
|
+
flashes[messages] = flash_messages(params[:controller], params[:action], messages)
|
13
|
+
end
|
14
|
+
response_status_and_flash[:flash] = flashes
|
15
|
+
end
|
16
|
+
super(options, response_status_and_flash)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def flash!(*args)
|
22
|
+
args.each do |key|
|
23
|
+
flash[key] = flash_messages(params[:controller], params[:action], key)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def flash_now!(*args)
|
28
|
+
args.each do |key|
|
29
|
+
flash.now[key] = flash_messages(params[:controller], params[:action], key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def flash_messages(*args)
|
34
|
+
I18n.t("flash_messages.#{args.join(".")}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module BootstrapFlashMessages
|
2
|
+
module Helpers
|
3
|
+
def flash_messages(*args)
|
4
|
+
if flash.present?
|
5
|
+
block = args.include?(:block)
|
6
|
+
show_heading = args.include?(:heading)
|
7
|
+
show_close = args.include?(:close)
|
8
|
+
|
9
|
+
messages = []
|
10
|
+
flash.each do |key, value|
|
11
|
+
heading = ""
|
12
|
+
if show_heading
|
13
|
+
heading_text = I18n.t("flash_messages.headings.#{key}")
|
14
|
+
heading = (block ? content_tag(:h4, heading_text, :class => "alert-heading") : content_tag(:strong, heading_text))
|
15
|
+
end
|
16
|
+
close = ""
|
17
|
+
if show_close
|
18
|
+
close = link_to("x", "#", :class => "close", :data => { :dismiss => "alert" })
|
19
|
+
end
|
20
|
+
|
21
|
+
messages << content_tag(:div, :class => "alert alert-#{key} #{"alert-block" if block}") do
|
22
|
+
close + heading + " " + value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
raw(messages.join)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bootstrap_flash_messages/helpers'
|
2
|
+
require 'bootstrap_flash_messages/flash_messages'
|
3
|
+
|
4
|
+
module BootstrapFlashMessages
|
5
|
+
def self.initialize
|
6
|
+
return if @initialized
|
7
|
+
raise "ActionController is not available yet." unless defined?(ActionController)
|
8
|
+
ActionController::Base.send(:helper, BootstrapFlashMessages::Helpers)
|
9
|
+
ActionController::Base.send(:include, BootstrapFlashMessages::FlashMessages)
|
10
|
+
@initialized = true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if defined?(Rails::Railtie)
|
15
|
+
require 'bootstrap_flash_messages/railtie'
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
en:
|
2
|
+
flash_messages:
|
3
|
+
#
|
4
|
+
# These are the translations for the different headings.
|
5
|
+
#
|
6
|
+
headings:
|
7
|
+
success: "Success!"
|
8
|
+
info: "Info!"
|
9
|
+
warning: "Warning!"
|
10
|
+
error: "Error!"
|
11
|
+
#
|
12
|
+
# Add flash messages for controller actions:
|
13
|
+
# products:
|
14
|
+
# create:
|
15
|
+
# success: "This is a notification"
|
16
|
+
# error: "Something went wrong"
|
17
|
+
#
|
18
|
+
# Inside controller:
|
19
|
+
# def create
|
20
|
+
# flash!(:success, :error)
|
21
|
+
# redirect_to(:root)
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# OR
|
25
|
+
#
|
26
|
+
# def create
|
27
|
+
# redirect_to(:root, :flash => [:success, :error])
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# OR
|
31
|
+
#
|
32
|
+
# def create
|
33
|
+
# flash_now!(:success, :error)
|
34
|
+
# render("new")
|
35
|
+
# end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bootstrap_flash_messages
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Robin Brouwer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-08-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Bootstrap alerts and Rails flash messages combined in one easy-to-use gem.
|
23
|
+
email:
|
24
|
+
- robin@45north.nl
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- bootstrap_flash_messages.gemspec
|
37
|
+
- init.rb
|
38
|
+
- lib/bootstrap_flash_messages.rb
|
39
|
+
- lib/bootstrap_flash_messages/flash_messages.rb
|
40
|
+
- lib/bootstrap_flash_messages/helpers.rb
|
41
|
+
- lib/bootstrap_flash_messages/railtie.rb
|
42
|
+
- lib/bootstrap_flash_messages/version.rb
|
43
|
+
- lib/generators/bootstrap_flash_messages/USAGE
|
44
|
+
- lib/generators/bootstrap_flash_messages/locale_generator.rb
|
45
|
+
- lib/generators/bootstrap_flash_messages/templates/flash.en.yml
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://www.45north.nl
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: nowarning
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Bootstrap alerts and Rails flash messages combined in one easy-to-use gem.
|
80
|
+
test_files: []
|
81
|
+
|