Navigation Menu

Skip to content

Commit

Permalink
tests and docs for modal helper
Browse files Browse the repository at this point in the history
  • Loading branch information
toadkicker committed Apr 29, 2013
1 parent 1f3d040 commit 91c4d28
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -234,7 +234,19 @@ jQuery ->

## Using Helpers

### Modal Helper

```ruby
<%= content_tag :a, "Modal", :href => "#modal", :class => 'btn', :data => {:toggle => 'modal'} %>
<%= modal_dialog :id => "modal",
:header => { :show_close => true, :dismiss => 'modal', :title => 'Modal header' },
:body => 'This is the body',
:footer => content_tag(:button, 'Save', :class => 'btn') %>
```
### Flash helper
Add flash helper `<%= bootstrap_flash %>` to your layout (built-in with layout generator)
### Breadcrumbs Helpers
Expand Down
45 changes: 29 additions & 16 deletions app/helpers/modal_helper.rb
@@ -1,42 +1,55 @@
module ModalHelper
def modal_dialog(options = {}, escape = true, &block)
default_options = {:class => "bootstrap-modal modal"}
content_tag :div, nil, options.merge(default_options), escape, &block

#modals have a header, a body, a footer for options.
def modal_dialog(options = {}, &block)
content_tag :div, :id => options[:id], :class => "bootstrap-modal modal hide fade" do
modal_header(options[:header]) +
modal_body(options[:body]) +
modal_footer(options[:footer])
end
end

def modal_header(options = {}, escape = true, &block)
default_options = {:class => 'modal-header'}
content_tag :div, nil, options.merge(default_options), escape do
raw("<button class=\"close\" data-dismiss=\"modal\">&times;</button>" + capture(&block))
def modal_header(options = {}, &block)
content_tag :div, :class => 'modal-header' do
if options[:show_close]
close_button(options[:dismiss]) +
content_tag(:h3, options[:title], &block)
else
content_tag(:h3, options[:title], &block)
end
end
end

def modal_body(options = {}, escape = true, &block)
default_options = {:class => 'modal-body'}
content_tag :div, nil, options.merge(default_options), escape, &block
def modal_body(options = {}, &block)
content_tag :div, options, :class => 'modal-body', &block
end

def modal_footer(options = {}, escape = true, &block)
default_options = {:class => 'modal-footer'}
content_tag :div, nil, options.merge(default_options), escape, &block
def modal_footer(options = {}, &block)
content_tag :div, options, :class => 'modal-footer', &block
end

def close_button(dismiss)
#It doesn't seem to like content_tag, so we do this instead.
raw("<button class=\"close\" data-dismiss=\"#{dismiss}\">&times;</button>")
end

def modal_toggle(content_or_options = nil, options = {}, &block)
if block_given?
options = content_or_options if content_or_options.is_a?(Hash)
default_options = {:class => 'btn', "data-toggle" => "modal", "href" => options[:dialog]}.merge(options)
default_options = { :class => 'btn', "data-toggle" => "modal", "href" => options[:dialog] }.merge(options)

content_tag :a, nil, default_options, true, &block
else
default_options = {:class => 'btn', "data-toggle" => "modal", "href" => options[:dialog]}.merge(options)
default_options = { :class => 'btn', "data-toggle" => "modal", "href" => options[:dialog] }.merge(options)
content_tag :a, content_or_options, default_options, true
end
end

def modal_cancel_button content, options = {}
default_options = {:class => "btn bootstrap-modal-cancel-button"}
default_options = { :class => "btn bootstrap-modal-cancel-button" }

content_tag_string "a", content, default_options.merge(options)
end

end

40 changes: 40 additions & 0 deletions spec/lib/twitter_bootstrap_rails/modal_helper_spec.rb
@@ -0,0 +1,40 @@
# encoding: utf-8
require 'spec_helper'
require_relative '../../../app/helpers/modal_helper'

include ActionView::Helpers
include ActionView::Context
include ModalHelper

describe ModalHelper, :type => :helper do
header_with_close = { :show_close => true, :dismiss => 'modal', :title => 'Modal header' }
header_without_close = { :show_close => false, :title => 'Modal header' }
options = { :header => header_with_close,
:body => 'This is the body',
:footer => content_tag(:button, 'Save', :class => 'btn')
}

it 'returns a complete modal' do
modal_dialog(options).gsub(/\n/, "").should eql BASIC_MODAL.gsub(/\n/, "")
end

it 'returns a modal header with a close button if show_close is true' do
modal_header(header_with_close).gsub(/\n/, "").should eql MODAL_HEADER_WITH_CLOSE.gsub(/\n/, "")
end

it 'renders a modal header without a close button' do
modal_header(header_without_close).gsub(/\n/, "").should eql MODAL_HEADER_WITHOUT_CLOSE.gsub(/\n/, "")
end

end
BASIC_MODAL = <<-HTML
<div class=\"bootstrap-modal modal\"><div class=\"modal-header\"><button class=\"close\" data-dismiss=\"modal\">&times;</button><h3>Modal header</h3></div><div class=\"modal-body\">This is the body</div><div class=\"modal-footer\"><button class=\"btn\">Save</button></div></div>
HTML

MODAL_HEADER_WITHOUT_CLOSE = <<-HTML
<div class="modal-header"><h3>Modal header</h3></div>
HTML

MODAL_HEADER_WITH_CLOSE = <<-HTML
<div class="modal-header"><button class="close" data-dismiss="modal">&times;</button><h3>Modal header</h3></div>
HTML

0 comments on commit 91c4d28

Please sign in to comment.