flash_messages_helper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +74 -0
- data/Rakefile +50 -0
- data/install.rb +1 -0
- data/install.txt +12 -0
- data/lib/flash_messages_helper.rb +29 -0
- data/rails/init.rb +1 -0
- data/spec/flash_messages_helper_spec.rb +47 -0
- data/spec/test_helper.rb +9 -0
- metadata +64 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
h1. Flash Messages Helper
|
2
|
+
|
3
|
+
Ruby on Rails view helper for displaying html flash messages in your Rails applications.
|
4
|
+
|
5
|
+
h2. Install as a Ruby Gem
|
6
|
+
|
7
|
+
<pre>sudo gem install flash_messages_helper</pre>
|
8
|
+
|
9
|
+
p. Then add the following line to your _environment.rb_
|
10
|
+
|
11
|
+
<pre>config.gem 'flash_messages_helper'</pre>
|
12
|
+
|
13
|
+
h2. Installation as a Ruby on Rails Plugin
|
14
|
+
|
15
|
+
<pre>./script/plugin install git://github.com/mdeering/flash_messages_helper.git</pre>
|
16
|
+
|
17
|
+
h2. Usage
|
18
|
+
|
19
|
+
Once you have installed it as a plugin/gem in your rails app usage is simple. Just call the flash_messages function within you Rails layout or view file
|
20
|
+
|
21
|
+
<pre>flash_messages</pre>
|
22
|
+
|
23
|
+
h2. Configuration Points
|
24
|
+
|
25
|
+
h3. Changing the default id of the flash elements
|
26
|
+
|
27
|
+
By default the id of the flash message element will come through as flash-_error-type_
|
28
|
+
|
29
|
+
* flash-error
|
30
|
+
* flash-notice
|
31
|
+
* flash-warning
|
32
|
+
* _ect..._
|
33
|
+
|
34
|
+
To change this you can pass set a lambda that will get called with the key of the message type.
|
35
|
+
|
36
|
+
<pre># config/initializers/flash_messages_helper_settings.rb
|
37
|
+
ActionView::Base.flash_message_id_proc = lambda {|key| "#{key}-message"}</pre>
|
38
|
+
|
39
|
+
A _error_ message will now displayed with the dom element id of _error-message_ rather then _flash-error_ throughout the application
|
40
|
+
|
41
|
+
<pre><div class="error" id="error-message">There was an error!</div></pre>
|
42
|
+
|
43
|
+
h3. Changing the default class of the flash elements
|
44
|
+
|
45
|
+
By default the class of the flash message element will come through as _error-type_
|
46
|
+
|
47
|
+
* error
|
48
|
+
* notice
|
49
|
+
* warning
|
50
|
+
* _ect..._
|
51
|
+
|
52
|
+
To change this you can pass set a lambda that will get called with the key of the message type.
|
53
|
+
|
54
|
+
<pre># config/initializers/flash_messages_helper_settings.rb
|
55
|
+
ActionView::Base.flash_message_class_proc = lambda {|key| "#{key} hideable"}</pre>
|
56
|
+
|
57
|
+
A _error_ message will now displayed with the dom element class of _error hideable rather then _error_ throughout the application
|
58
|
+
|
59
|
+
<pre><div class="error hideable" id="flash-error">There was an error!</div></pre>
|
60
|
+
|
61
|
+
h3. Changing the default html tag type of the flash elements
|
62
|
+
|
63
|
+
As a default the html tag used to wrap the flash messages is a div element. This can be easily globally changed with the following setting.
|
64
|
+
|
65
|
+
<pre># config/initializers/flash_messages_helper_settings.rb
|
66
|
+
ActionView::Base.flash_message_tag = :p</pre>
|
67
|
+
|
68
|
+
With the above setting flash messages will be wrapped inside of a paragraph tag rather then a div.
|
69
|
+
|
70
|
+
<pre><p class="error" id="flash-error">There was an error!</p></pre>
|
71
|
+
|
72
|
+
h2. Credits
|
73
|
+
|
74
|
+
p. Copyright (c) 2009 "Michael Deering(Ruby on Rails Development Edmonton)":http://mdeering.com, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
AUTHOR = "Michael Deering"
|
7
|
+
EMAIL = "mdeering@mdeering.com"
|
8
|
+
GEM = "flash_messages_helper"
|
9
|
+
HOMEPAGE = "http://github.com/mdeering/flash_messages_helper"
|
10
|
+
SUMMARY = "A simple yet configurable rails view helper for displaying flash messages."
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |s|
|
14
|
+
s.author = AUTHOR
|
15
|
+
s.email = EMAIL
|
16
|
+
s.files = %w(install.rb install.txt MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
|
17
|
+
s.homepage = HOMEPAGE
|
18
|
+
s.name = GEM
|
19
|
+
s.require_path = 'lib'
|
20
|
+
s.summary = SUMMARY
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Default: spec tests.'
|
28
|
+
task :default => :spec
|
29
|
+
|
30
|
+
desc 'Test the flash_messages_helper gem.'
|
31
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
32
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
33
|
+
t.spec_opts = ["-c"]
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Run all examples with RCov"
|
37
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
|
+
t.rcov = true
|
40
|
+
t.rcov_opts = ['--exclude', '/opt,spec,Library']
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Generate documentation for the flash_messages_helper plugin.'
|
44
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = 'FlashMessagesHelper'
|
47
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
48
|
+
rdoc.rdoc_files.include('README')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
puts IO.read(File.join(File.dirname(__FILE__), 'install.txt'))
|
data/install.txt
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
-----------------------------------------------------------------------
|
2
|
+
Flash Messages Helper:
|
3
|
+
|
4
|
+
Thank you for using Flash Messages Helper. You will find full
|
5
|
+
documentation on usage and configuration options here:
|
6
|
+
http://github.com/mdeering/flash_messages_helper
|
7
|
+
|
8
|
+
All comments, suggestions, and pull requests are welcome.
|
9
|
+
|
10
|
+
Cheers,
|
11
|
+
Michael Deering http://mdeering.com
|
12
|
+
-----------------------------------------------------------------------
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module FlashMessagesHelper
|
2
|
+
|
3
|
+
def self.included(target)
|
4
|
+
target.cattr_accessor :flash_message_class_proc
|
5
|
+
target.flash_message_class_proc = lambda { |key| "#{key}" }
|
6
|
+
target.cattr_accessor :flash_message_id_proc
|
7
|
+
target.flash_message_id_proc = lambda { |key| "flash-#{key}" }
|
8
|
+
target.cattr_accessor :flash_message_tag
|
9
|
+
target.flash_message_tag = :div
|
10
|
+
target.send :include, InstanceMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def flash_messages(options = {})
|
15
|
+
ret = []
|
16
|
+
flash.each do |key, value|
|
17
|
+
ret << content_tag(ActionView::Base.flash_message_tag, value, {
|
18
|
+
:class => ActionView::Base.flash_message_class_proc.call(key),
|
19
|
+
:id => ActionView::Base.flash_message_id_proc.call(key)
|
20
|
+
}.merge(options)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
return ret.join("\n")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
ActionView::Base.send(:include, FlashMessagesHelper)
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[.. lib flash_messages_helper])
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
require 'flash_messages_helper'
|
4
|
+
|
5
|
+
ActionView::Base.send(:include, FlashMessagesHelper)
|
6
|
+
|
7
|
+
describe FlashMessagesHelper do
|
8
|
+
|
9
|
+
view = ActionView::Base.new
|
10
|
+
controller = ActionController::Base.new
|
11
|
+
view.controller = controller
|
12
|
+
|
13
|
+
it 'will have nothing to display of the flash is not set in any way' do
|
14
|
+
controller.stub!(:flash).and_return({})
|
15
|
+
view.flash_messages.should == ''
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'will return a div with the error message' do
|
19
|
+
controller.stub!(:flash).and_return({:error => 'There was an error'})
|
20
|
+
view.flash_messages.should == "<div class=\"error\" id=\"flash-error\">There was an error</div>"
|
21
|
+
end
|
22
|
+
|
23
|
+
{
|
24
|
+
:flash_message_class_proc => lambda { |key| "#{key}-message hiddable"},
|
25
|
+
:flash_message_id_proc => lambda { |key| "flash-#{key}-message"},
|
26
|
+
:flash_message_tag => :p
|
27
|
+
}.each do |singleton_variable, value|
|
28
|
+
it "should create flash_messages class (singleton) variable #{singleton_variable} on its included class" do
|
29
|
+
ActionView::Base.send(singleton_variable).should_not == nil
|
30
|
+
ActionView::Base.send("#{singleton_variable}=", value)
|
31
|
+
ActionView::Base.send(singleton_variable).should == value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Now that the defaults have been set
|
36
|
+
|
37
|
+
it 'will return a p with the error message and the defaults set differently' do
|
38
|
+
controller.stub!(:flash).and_return({:error => 'There was an error'})
|
39
|
+
view.flash_messages.should == "<p class=\"error-message hiddable\" id=\"flash-error-message\">There was an error</p>"
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'will still honor the html options passed in' do
|
43
|
+
controller.stub!(:flash).and_return({:error => 'There was an error'})
|
44
|
+
view.flash_messages(:class => 'my-class').should == "<p class=\"my-class\" id=\"flash-error-message\">There was an error</p>"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flash_messages_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Deering
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-19 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mdeering@mdeering.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.textile
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README.textile
|
27
|
+
- Rakefile
|
28
|
+
- install.rb
|
29
|
+
- install.txt
|
30
|
+
- lib/flash_messages_helper.rb
|
31
|
+
- rails/init.rb
|
32
|
+
- spec/flash_messages_helper_spec.rb
|
33
|
+
- spec/test_helper.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/mdeering/flash_messages_helper
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A simple yet configurable rails view helper for displaying flash messages.
|
62
|
+
test_files:
|
63
|
+
- spec/flash_messages_helper_spec.rb
|
64
|
+
- spec/test_helper.rb
|