Skip to content

Commit

Permalink
add ability to pass extra options to bootstrap_flash
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaximin committed May 8, 2015
1 parent 20984d4 commit 1007d3c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<li>Christian Joudrey</li>
<li>Todd Baur</li>
<li>Leonid Shevtsov</li>
<li>Christophe Maximin</li>
</ul>
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ It also takes the :pull option to drag it to the left or right.

### Flash helper

Add flash helper `<%= bootstrap_flash %>` to your layout (built-in with layout generator)
Add flash helper `<%= bootstrap_flash %>` to your layout (built-in with layout generator).
You can pass the attributes you want to add to the main div returned: `<%= bootstrap_flash(class: "extra-class", id: "your-id") %>`


### Breadcrumbs Helpers

Expand Down
11 changes: 8 additions & 3 deletions app/helpers/bootstrap_flash_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ def bootstrap_flash(options = {})
type = :danger if type == :error
next unless ALERT_TYPES.include?(type)

tag_class = options.extract!(:class)[:class]
tag_options = {
class: "alert fade in alert-#{type} #{tag_class}"
}.merge(options)

close_button = content_tag(:button, raw("&times;"), class: "close", "data-dismiss" => "alert")

Array(message).each do |msg|
text = content_tag(:div,
content_tag(:button, raw("&times;"), :class => "close", "data-dismiss" => "alert") +
msg.html_safe, :class => "alert fade in alert-#{type} #{options[:class]}")
text = content_tag(:div, close_button + msg.html_safe, tag_options)
flash_messages << text if msg
end
end
Expand Down
90 changes: 90 additions & 0 deletions spec/lib/twitter_bootstrap_rails/bootstrap_flash_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# encoding: utf-8
require 'spec_helper'
require 'action_view'
require 'active_support'
require_relative '../../../app/helpers/bootstrap_flash_helper'

include ActionView::Helpers
include ActionView::Context
include BootstrapFlashHelper

describe BootstrapFlashHelper, type: :helper do
before do
allow(self).to receive(:uri_state) { :inactive }
allow(self).to receive(:root_url) { '/' }
end

describe "bootstrap_flash" do
it "should not return anything without flashes" do
allow(self).to receive(:flash) { {} }

element = bootstrap_flash

expect(element).to eql("")
end

it "should work with a notice" do
allow(self).to receive(:flash) { {notice: "Hello"} }

element = bootstrap_flash

expect(element).to have_tag(:div,
text: "×Hello",
with: {class: "alert fade in alert-success"}) {

with_tag(:button,
text: "×",
with: {
class: "close",
"data-dismiss" => "alert"
}
)

}
end

it "should work with a notice and an extra class" do
allow(self).to receive(:flash) { {notice: "Hello"} }

element = bootstrap_flash(class: "extra-class")

expect(element).to have_tag(:div,
text: "×Hello",
with: {class: "alert fade in alert-success extra-class"}) {

with_tag(:button,
text: "×",
with: {
class: "close",
"data-dismiss" => "alert"
}
)

}
end

it "should work with a notice and an extra class and an extra attribute" do
allow(self).to receive(:flash) { {notice: "Hello"} }

element = bootstrap_flash(class: "extra-class", "data-no-transition-cache" => true)

expect(element).to have_tag(:div,
text: "×Hello",
with: {
class: "alert fade in alert-success extra-class",
"data-no-transition-cache" => true
}) {

with_tag(:button,
text: "×",
with: {
class: "close",
"data-dismiss" => "alert"
}
)

}
end
end

end

0 comments on commit 1007d3c

Please sign in to comment.