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
Adding a helper to render all messages stored on flash object. #3009
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
module ActionView | |||
module Helpers | |||
module FlashHelper | |||
# Helper to render all messages stored on +flash+ object. You can customize the output with the +options+ hash. | |||
# | |||
# ==== Options | |||
# * <tt>:with_parent_element</tt> - Sets the parent element that will be used on each message (defaults to :p). | |||
# * <tt>:using_class</tt> - Sets the class used on the parent element of each message (defaults to :flash). | |||
# | |||
# ==== Examples | |||
# | |||
# On Controller: | |||
# | |||
# class PostsController < ApplicationController | |||
# def index | |||
# flash[:notice] = "Everything is ok :)" | |||
# flash[:alert] = "Error :(" | |||
# end | |||
# end | |||
# | |||
# Then on the view: | |||
# | |||
# <%= flash_messages %> | |||
# # => <p class="flash flash_notice">Everything is ok :)</p> | |||
# # <p class="flash flash_alert">Error :(</p> | |||
# | |||
# <%= flash_messages :with_parent_element => :div %> | |||
# # => <div class="flash flash_notice">Everything is ok :)</div> | |||
# # <div class="flash flash_alert">Error :(</div> | |||
# | |||
# <%= flash_messages :using_class => :message %> | |||
# # => <p class="message message_notice">Everything is ok :)</p> | |||
# # <p class="message message_alert">Error :(</p> | |||
# | |||
# <%= flash_messages :with_parent_element => :div, :using_class => :message %> | |||
# # => <div class="message message_notice">Everything is ok :)</div> | |||
# # <div class="message message_alert">Error :(</div> | |||
# | |||
# <%= flash_messages :using_class => :nil %> | |||
# # => <p>Everything is ok :)</p> | |||
# # <p>Error :(</p> | |||
def flash_messages(options = {}) | |||
parent_element = options.delete(:with_parent_element) { :p } | |||
|
|||
messages = "" | |||
options.default = "" # This is needed to use the key :using_class with nil value | |||
|
|||
flash.each do |key, value| | |||
parent_element_class = class_for_flash_message_parent_element(options.merge(:flash_message_key => key)) | |||
|
|||
messages << content_tag(parent_element, :class => parent_element_class) { value } + "\n" | |||
end | |||
|
|||
messages.html_safe | |||
end | |||
|
|||
protected | |||
def class_for_flash_message_parent_element(options = {}) | |||
if options[:using_class] | |||
parent_element_class = (options.delete(:using_class) { "flash" }).to_s | |||
|
|||
"#{parent_element_class} #{parent_element_class}_#{options[:flash_message_key]}" | |||
else | |||
nil | |||
end | |||
end | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,62 @@ | |||
# encoding: utf-8 | |||
|
|||
require 'abstract_unit' | |||
|
|||
class FlashHelperTest < ActionView::TestCase | |||
tests ActionView::Helpers::FlashHelper | |||
|
|||
def test_flash_messages_without_messages | |||
assert flash_messages.blank? | |||
end | |||
|
|||
def test_flash_messages_is_html_safe | |||
flash[:notice] = "Some random resource was succesfuly saved." | |||
|
|||
assert flash_messages.html_safe? | |||
end | |||
|
|||
def test_flash_messages_with_notice_message | |||
flash[:notice] = "Everything is ok." | |||
|
|||
assert_equal %Q(<p class="flash flash_notice">Everything is ok.</p>\n), flash_messages | |||
end | |||
|
|||
def test_flash_messages_with_alert_message | |||
flash[:alert] = "10 minutes to world explosion." | |||
|
|||
assert_equal %Q(<p class="flash flash_alert">10 minutes to world explosion.</p>\n), flash_messages | |||
end | |||
|
|||
def test_flash_messages_with_notice_and_alert_messages | |||
flash[:notice] = "This application is just fine." | |||
flash[:alert] = "The previous message is wrong!" | |||
|
|||
messages = flash_messages | |||
|
|||
assert messages.include?(%Q(<p class="flash flash_notice">This application is just fine.</p>\n)) | |||
messages.gsub!(%Q(<p class="flash flash_notice">This application is just fine.</p>\n), "") | |||
|
|||
assert messages.include?(%Q(<p class="flash flash_alert">The previous message is wrong!</p>\n)) | |||
messages.gsub!(%Q(<p class="flash flash_alert">The previous message is wrong!</p>\n), "") | |||
|
|||
assert messages.blank? | |||
end | |||
|
|||
def test_flash_messages_with_custom_parent_element | |||
flash[:notice] = "Look at me mom, I'm on a flash message!" | |||
|
|||
assert_equal %Q(<div class="flash flash_notice">Look at me mom, I'm on a flash message!</div>\n), flash_messages(:with_parent_element => :div) | |||
end | |||
|
|||
def test_flash_messages_with_custom_class_for_parent_element | |||
flash[:alert] = "Houston, we have a problem." | |||
|
|||
assert_equal %Q(<p class="message message_alert">Houston, we have a problem.</p>\n), flash_messages(:using_class => :message) | |||
end | |||
|
|||
def test_flash_messages_without_class_on_parent_element | |||
flash[:notice] = "Up and running!" | |||
|
|||
assert_equal %Q(<p>Up and running!</p>\n), flash_messages(:using_class => nil) | |||
end | |||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be blankly made html safe? What if your flash message reiterates user input?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe user input on flash messages is far from being the most common scenario. So, based on the principle of making life easier for the majority, I think people who use flash messages with content from their own code base should not have to turn the flash messages HTML safe. If the content comes from a static string from the application, I think is reasonable to suppose it's safe.
In the case you've mentioned, the flash message would have to be used without the use of this helper, which I think it's ok, as I believe this is not a common usage of flash messages, and should net harm the usage of the majority.