Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f3d040
commit 91c4d28
Showing
3 changed files
with
81 additions
and
16 deletions.
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 | Diff line number | Diff line change |
---|---|---|
@@ -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\">×</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}\">×</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 | ||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -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\">×</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">×</button><h3>Modal header</h3></div> | ||
HTML |