From 91c4d283be6b88bd585af9fe765177418fbc91dc Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sun, 28 Apr 2013 20:04:33 -0400 Subject: [PATCH] tests and docs for modal helper --- README.md | 12 +++++ app/helpers/modal_helper.rb | 45 ++++++++++++------- .../modal_helper_spec.rb | 40 +++++++++++++++++ 3 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 spec/lib/twitter_bootstrap_rails/modal_helper_spec.rb diff --git a/README.md b/README.md index 5f43179b..45df5e06 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/helpers/modal_helper.rb b/app/helpers/modal_helper.rb index 538a6cf8..a9b4c957 100644 --- a/app/helpers/modal_helper.rb +++ b/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("" + 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("") 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 diff --git a/spec/lib/twitter_bootstrap_rails/modal_helper_spec.rb b/spec/lib/twitter_bootstrap_rails/modal_helper_spec.rb new file mode 100644 index 00000000..bcf55996 --- /dev/null +++ b/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 +

Modal header

This is the body
+HTML + +MODAL_HEADER_WITHOUT_CLOSE = <<-HTML + +HTML + +MODAL_HEADER_WITH_CLOSE = <<-HTML + +HTML