Module: ModalHelper

Includes:
ActionView::Helpers, FormatHelper
Defined in:
app/helpers/modal_helper.rb

Instance Method Summary collapse

Methods included from FormatHelper

#prepend_class, #squeeze_n_strip

Instance Method Details



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/modal_helper.rb', line 5

def modal(options={}, &block)
  button_options = options.delete(:button) || {}

  caption               = button_options.delete(:caption) || 'Modal'
  modal_dialog_id       = options[:id] || "modal-#{SecureRandom.hex(3)}"
  button_options[:data] = (button_options[:data] || {}).merge({toggle: 'modal', target: "##{modal_dialog_id}"})
  size                  = case options.delete(:size).try(:to_sym)
                          when :xsmall
                            'modal-sm'
                          when :large
                            'modal-lg'
                          else
                          end

  prepend_class(options, 'modal', 'fade')
  options.merge!({id: modal_dialog_id, tabindex: -1, role: 'dialog', aria: {hidden: true}})

  ((button caption, button_options) +
    ( :div, options do
       :div, class: "modal-dialog #{size}" do
         :div, class: 'modal-content' do
          yield if block_given?
        end
      end
    end)).html_safe
end


45
46
47
48
49
50
51
52
53
# File 'app/helpers/modal_helper.rb', line 45

def modal_body(content_or_options=nil, options={}, &block)
  content_or_options.is_a?(Hash) ? options = content_or_options : content = content_or_options

  prepend_class(options, 'modal-body')

   :div, options do
    content.presence || capture(&block)
  end
end


55
56
57
58
59
60
61
62
63
# File 'app/helpers/modal_helper.rb', line 55

def modal_footer(content_or_options=nil, options={}, &block)
  content_or_options.is_a?(Hash) ? options = content_or_options : content = content_or_options

  prepend_class(options, 'modal-footer')

   :div, options do
    content.presence || capture(&block)
  end
end


32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/modal_helper.rb', line 32

def modal_header(content_or_options=nil, options={}, &block)
  content_or_options.is_a?(Hash) ? options = content_or_options : content = content_or_options

  prepend_class(options, 'modal-title')

   :div, class: 'modal-header' do
    ("<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span></button>" +
      ( :h3, options do
        content.presence || capture(&block)
      end)).html_safe
  end
end