flash_messages_helper 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,26 +1,40 @@
1
1
  h1. Flash Messages Helper
2
2
 
3
- Ruby on Rails view helper for displaying html flash messages in your Rails applications.
3
+ A configurable Ruby on Rails view helper for displaying html flash messages in your Rails applications.
4
+
5
+ h2. Recent Changes
6
+
7
+ * 0.2.0
8
+ ** Added a proper configuration DSL
9
+ ** html_safe called on the output if available
4
10
 
5
11
  h2. Install as a Ruby Gem
6
12
 
7
- <pre>sudo gem install flash_messages_helper</pre>
13
+ <pre>gem install flash_messages_helper</pre>
14
+
15
+ h3. Rails 2
8
16
 
9
17
  p. Then add the following line to your _environment.rb_
10
18
 
11
19
  <pre>config.gem 'flash_messages_helper'</pre>
12
20
 
21
+ h3. Rails 3
22
+
23
+ p. Add the following to your _Gemfile_
24
+ <pre>gem 'flash_messages_helper'</pre>
25
+
26
+
13
27
  h2. Installation as a Ruby on Rails Plugin
14
28
 
15
29
  <pre>./script/plugin install git://github.com/mdeering/flash_messages_helper.git</pre>
16
30
 
17
31
  h2. Usage
18
32
 
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
33
+ 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 view file
20
34
 
21
- <pre>flash_messages</pre>
35
+ <pre>= flash_messages</pre>
22
36
 
23
- h2. Configuration Points
37
+ h2. Configuration
24
38
 
25
39
  h3. Changing the default id of the flash elements
26
40
 
@@ -31,10 +45,13 @@ By default the id of the flash message element will come through as flash-_error
31
45
  * flash-warning
32
46
  * _ect..._
33
47
 
34
- To change this you can pass set a lambda that will get called with the key of the message type.
48
+ To change this use the _dom_id_ configuration attribute.
35
49
 
36
- <pre># config/initializers/flash_messages_helper_settings.rb
37
- ActionView::Base.flash_message_id_proc = lambda {|key| "#{key}-message"}</pre>
50
+ <pre><code># config/initializers/flash_messages_helper.rb
51
+ FlashMessagesHelper.configure do |config|
52
+ config.dom_id = lambda { |key| "#{key}-message" }
53
+ end
54
+ </code></pre>
38
55
 
39
56
  A _error_ message will now displayed with the dom element id of _error-message_ rather then _flash-error_ throughout the application
40
57
 
@@ -49,21 +66,27 @@ By default the class of the flash message element will come through as _error-ty
49
66
  * warning
50
67
  * _ect..._
51
68
 
52
- To change this you can pass set a lambda that will get called with the key of the message type.
69
+ To change this use the _css_class_ configuration attribute.
53
70
 
54
- <pre># config/initializers/flash_messages_helper_settings.rb
55
- ActionView::Base.flash_message_class_proc = lambda {|key| "#{key} hideable"}</pre>
71
+ <pre><code># config/initializers/flash_messages_helper.rb
72
+ FlashMessagesHelper.configure do |config|
73
+ config.css_class = lambda { |key| "#{key} dismissible" }
74
+ end
75
+ </code></pre>
56
76
 
57
- A _error_ message will now displayed with the dom element class of _error hideable rather then _error_ throughout the application
77
+ A _error_ message will now displayed with the dom element class of _error dismissible rather then _error_ throughout the application
58
78
 
59
- <pre><div class="error hideable" id="flash-error">There was an error!</div></pre>
79
+ <pre><div class="error dismissible" id="flash-error">There was an error!</div></pre>
60
80
 
61
81
  h3. Changing the default html tag type of the flash elements
62
82
 
63
83
  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
84
 
65
- <pre># config/initializers/flash_messages_helper_settings.rb
66
- ActionView::Base.flash_message_tag = :p</pre>
85
+ <pre><code># config/initializers/flash_messages_helper.rb
86
+ FlashMessagesHelper.configure do |config|
87
+ config.wrapper = :p
88
+ end
89
+ </code></pre>
67
90
 
68
91
  With the above setting flash messages will be wrapped inside of a paragraph tag rather then a div.
69
92
 
@@ -71,4 +94,4 @@ With the above setting flash messages will be wrapped inside of a paragraph tag
71
94
 
72
95
  h2. Credits
73
96
 
74
- p. Copyright (c) 2009 "Michael Deering(Ruby on Rails Development Edmonton)":http://mdeering.com, released under the MIT license
97
+ p. Copyright (c) 2011 "Michael Deering(Ruby on Rails Development Edmonton)":http://mdeering.com, released under the MIT license
@@ -1,29 +1,64 @@
1
1
  module FlashMessagesHelper
2
2
 
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configuration
8
+ @configuration ||= Configuration.new
9
+ end
10
+
11
+ def self.configure
12
+ yield(configuration)
13
+ end
14
+
15
+ class Configuration
16
+ attr_accessor :css_class, :dom_id, :wrapper
17
+ def initialize
18
+ @css_class = lambda { |key| "#{key}" }
19
+ @dom_id = lambda { |key| "flash-#{key}" }
20
+ @wrapper = :div
21
+ end
22
+ end
23
+
3
24
  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
25
+ target.extend ClassMethods
10
26
  target.send :include, InstanceMethods
11
27
  end
12
28
 
29
+ module ClassMethods
30
+
31
+ # Deprications
32
+ {
33
+ :flash_message_class_proc => :css_class,
34
+ :flash_message_id_proc => :dom_id,
35
+ :flash_message_tag => :wrapper
36
+ }.each do |old_method, new_method|
37
+ define_method "#{old_method}=" do |value|
38
+ ActiveSupport::Deprecation.warn "FlashMessagesHelper.#{old_method} has been removed in favor of #{new_method}. Please refer to the README https://github.com/mdeering/flash_messages_helper for the configuration DSL"
39
+ FlashMessagesHelper.configure do |config|
40
+ config.send("#{new_method}=", value)
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+
13
47
  module InstanceMethods
14
48
  def flash_messages(options = {})
15
49
  ret = []
16
50
  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)
51
+ ret << content_tag(FlashMessagesHelper.configuration.wrapper, value, {
52
+ :class => FlashMessagesHelper.configuration.css_class.call(key),
53
+ :id => FlashMessagesHelper.configuration.dom_id.call(key)
20
54
  }.merge(options)
21
55
  )
22
56
  end
23
- return ret.join("\n")
57
+ return_string = ret.join("\n")
58
+ return return_string.respond_to?(:html_safe) ? return_string.html_safe : return_string
24
59
  end
25
60
  end
26
61
 
27
62
  end
28
63
 
29
- ActionView::Base.send(:include, FlashMessagesHelper)
64
+ ActionView::Base.send(:include, FlashMessagesHelper) if defined?(ActionView::Base)
@@ -6,42 +6,50 @@ ActionView::Base.send(:include, FlashMessagesHelper)
6
6
 
7
7
  describe FlashMessagesHelper do
8
8
 
9
- view = ActionView::Base.new
10
- controller = ActionController::Base.new
11
- view.controller = controller
9
+ before do
10
+ @view = ActionView::Base.new
11
+ @controller = ActionController::Base.new
12
+ @view.controller = @controller
13
+ FlashMessagesHelper.configuration = FlashMessagesHelper::Configuration.new
14
+ end
12
15
 
13
16
  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 == ''
17
+ @controller.stub!(:flash).and_return({})
18
+ @view.flash_messages.should == ''
16
19
  end
17
20
 
18
21
  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>"
22
+ @controller.stub!(:flash).and_return({ :error => 'There was an error' })
23
+ @view.flash_messages.should == "<div class=\"error\" id=\"flash-error\">There was an error</div>"
21
24
  end
22
25
 
23
26
  {
24
- :flash_message_class_proc => lambda { |key| "#{key}-message hiddable"},
25
- :flash_message_id_proc => lambda { |key| "flash-#{key}-message"},
27
+ :flash_message_class_proc => lambda { |key| "#{key}-message hiddable" },
28
+ :flash_message_id_proc => lambda { |key| "flash-#{key}-message" },
26
29
  :flash_message_tag => :p
27
30
  }.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
31
+ it "should give deprication warnings for 0.1.0 usage but still set configuration points" do
32
+ ActiveSupport::Deprecation.should_receive(:warn)
30
33
  ActionView::Base.send("#{singleton_variable}=", value)
31
- ActionView::Base.send(singleton_variable).should == value
34
+ FlashMessagesHelper.configuration.css_class == value if singleton_variable == :flash_message_class_proc
35
+ FlashMessagesHelper.configuration.dom_id == value if singleton_variable == :flash_message_id_proc
36
+ FlashMessagesHelper.configuration.wrapper == value if singleton_variable == :flash_message_tag
32
37
  end
33
38
  end
34
39
 
35
40
  # Now that the defaults have been set
36
41
 
37
42
  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>"
43
+ FlashMessagesHelper.configuration.wrapper = :p
44
+ @controller.stub!(:flash).and_return({ :error => 'There was an error' })
45
+ @view.flash_messages.should == "<p class=\"error\" id=\"flash-error\">There was an error</p>"
40
46
  end
41
47
 
42
48
  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>"
49
+ @controller.stub!(:flash).and_return({ :error => 'There was an error' })
50
+ @view.flash_messages(:class => 'my-class').should == "<div class=\"my-class\" id=\"flash-error\">There was an error</div>"
45
51
  end
46
52
 
53
+ it 'will call html_safe on the return string if available'
54
+
47
55
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flash_messages_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - Michael Deering
@@ -9,10 +15,53 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-01-19 00:00:00 -07:00
18
+ date: 2011-03-19 00:00:00 -06:00
13
19
  default_executable:
14
- dependencies: []
15
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: jeweler
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ prerelease: false
33
+ type: :development
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rails
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ hash: 7
43
+ segments:
44
+ - 3
45
+ - 0
46
+ version: "3.0"
47
+ prerelease: false
48
+ type: :development
49
+ requirement: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - <
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 2
60
+ - 0
61
+ version: "2.0"
62
+ prerelease: false
63
+ type: :development
64
+ requirement: *id003
16
65
  description:
17
66
  email: mdeering@mdeering.com
18
67
  executables: []
@@ -36,26 +85,32 @@ homepage: http://github.com/mdeering/flash_messages_helper
36
85
  licenses: []
37
86
 
38
87
  post_install_message:
39
- rdoc_options:
40
- - --charset=UTF-8
88
+ rdoc_options: []
89
+
41
90
  require_paths:
42
91
  - lib
43
92
  required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
44
94
  requirements:
45
95
  - - ">="
46
96
  - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
47
100
  version: "0"
48
- version:
49
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
50
103
  requirements:
51
104
  - - ">="
52
105
  - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
53
109
  version: "0"
54
- version:
55
110
  requirements: []
56
111
 
57
112
  rubyforge_project:
58
- rubygems_version: 1.3.5
113
+ rubygems_version: 1.5.2
59
114
  signing_key:
60
115
  specification_version: 3
61
116
  summary: A simple yet configurable rails view helper for displaying flash messages.