Module: Bootstrap::ModalHelper

Defined in:
app/helpers/bootstrap/modal_helper.rb

Overview

Rails helper for producing Bootstrap modal dialogs

Instance Method Summary collapse

Instance Method Details

Returns a Bootstrap modal dialog



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/bootstrap/modal_helper.rb', line 16

def modal(options={})
  options = canonicalize_options(options)
  options.has_key?(:id) or raise(ArgumentError, "missing :id option")
  options = ensure_class(options, %w(modal fade))
  options.merge!(tabindex: "-1", role: "dialog")
  
  (:div, options) do
    (:div, class: "modal-dialog") do 
      (:div, class: "modal-content") do 
        yield
      end
    end
  end
end


104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/bootstrap/modal_helper.rb', line 104

def modal_alert(*args, &block)
  options = canonicalize_options(args.extract_options!)
  header = options.delete(:header) || "Alert"

  modal(options) do
    modal_header(header) + 
    modal_body(*args, &block) + 
    modal_footer
  end
end

Returns a DIV for Bootstrap modal body



48
49
50
51
52
53
54
55
56
# File 'app/helpers/bootstrap/modal_helper.rb', line 48

def modal_body(*args, &block)
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'modal-body')
  content = block_given? ? capture(&block) : args.shift

  (:div, options) do
    content
  end
end


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/bootstrap/modal_helper.rb', line 115

def modal_confirm(*args, &block)
  options = canonicalize_options(args.extract_options!)
  header = options.delete(:header) || "Confirmation"

  modal(options) do
    modal_header(header) + 
    modal_body(*args, &block) + 
    modal_footer do
      modal_footer_close_button(icon('remove', 'Cancel')) + 
      modal_footer_ok_button(icon('ok', :white, 'Ok'), :primary)
    end
  end
end

Returns a DIV for Bootstrap modal footer Defaults to including #modal_footer_close_button.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/bootstrap/modal_helper.rb', line 60

def modal_footer(*args, &block)
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'modal-footer')
  
  content = if block_given?
    capture(&block)
  elsif args.size > 0
    args.shift
  else
    modal_footer_close_button
  end

  (:div, options) do
    content
  end
end

Returns a close button for Bootstrap modal footer.



86
87
88
89
90
91
92
93
# File 'app/helpers/bootstrap/modal_helper.rb', line 86

def modal_footer_close_button(*args)
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'modal-close modal-footer-close')
  options.merge!(data: {dismiss: 'modal'})
  args.unshift("Close") if args.empty?

  button(*args, options)
end


95
96
97
98
99
100
101
102
# File 'app/helpers/bootstrap/modal_helper.rb', line 95

def modal_footer_ok_button(*args)
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'modal-footer-ok')
  options.merge!(data: {dismiss: 'modal'})
  args.unshift("Ok") if args.empty?

  button(*args, options)
end

Returns a DIV for Bootstrap modal header



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

def modal_header(*args, &block)
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'modal-header')
  show_close = options.delete(:close) != false

  content = block_given? ? capture(&block) : args.shift

  (:div, options) do
    modal_header_close_button(show_close) +
    (:h4, class: "modal-title") do
      content
    end
  end
end

Returns a Bootstrap modal close button



78
79
80
81
82
83
# File 'app/helpers/bootstrap/modal_helper.rb', line 78

def modal_header_close_button(show=true)
  return ''.html_safe unless show
  button(type: 'button', class: 'close', data: {dismiss: 'modal'}) do
    (:span, "×".html_safe)
  end
end

Returns an A tag to trigger (open) a Boostrap modal



6
7
8
9
10
11
12
13
# File 'app/helpers/bootstrap/modal_helper.rb', line 6

def modal_trigger(text, options={})
  options = canonicalize_options(options)
  href = options.delete(:href) or raise(ArgumentError, 'missing :href option')
  options.merge!(role: 'button', href: href, data: { toggle: 'modal'})
  options = ensure_class(options, 'btn')
  
  (:a, text, options)
end