Module: NdrUi::Bootstrap::ModalHelper

Included in:
NdrUi::BootstrapHelper
Defined in:
app/helpers/ndr_ui/bootstrap/modal_helper.rb

Overview

This provides bootstrap modal box helper methods

Constant Summary collapse

%w(sm lg).freeze

Instance Method Summary collapse

Instance Method Details

#bootstrap_modal_body_tag(*args, &block) ⇒ Object

Creates a simple bootstrap modal body.

Signatures

bootstrap_modal_body_tag(content)
bootstrap_modal_body_tag do
  # content for modal body
end

Examples

<%= bootstrap_modal_body_tag do %>
  Check it out!!
<% end %>
# => <div class="modal-body">Check it out!!</div>


83
84
85
86
87
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 83

def bootstrap_modal_body_tag(*args, &block)
  return bootstrap_modal_body_tag(capture(&block), *args) if block_given?

  (:div, args.first, class: 'modal-body')
end

#bootstrap_modal_box(title, *args, &block) ⇒ Object

Creates a Boostrap Modal box.

Signatures

bootstrap_modal_box(title, controls, options = {})
bootstrap_modal_box(title, options = {}) do
  # controls
end

Options

  • :size - Symbol of modal box size. Supported sizes are :sm, :lg. By default it will be unset (medium width).

Examples

<%= bootstrap_modal_box("New Pear", "Pear form") %>
# =>
<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
      <h4 class="modal-title">New Pear</h4>
    </div>
    <div class="modal-body">
      Pear form
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">
        Don't save
      </button>
      <input name="commit" class="btn-primary btn" data-disable-with="Saving&hellip;"
value="Save" type="submit" />
    </div>
  </div>
</div>


200
201
202
203
204
205
206
207
208
209
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 200

def bootstrap_modal_box(title, *args, &block)
  return bootstrap_modal_box(title, capture(&block), *args) if block_given?
  options = args.extract_options!

  bootstrap_modal_dialog_tag(options) do
    bootstrap_modal_header_tag(title) +
      bootstrap_modal_body_tag(args.first) +
      bootstrap_modal_footer_tag(options)
  end
end

#bootstrap_modal_button(label) ⇒ Object



218
219
220
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 218

def bootstrap_modal_button(label)
  button_tag(label, class: 'btn btn-default', "data-dismiss": 'modal')
end

#bootstrap_modal_dialog_tag(options = {}, &block) ⇒ Object

Creates a bootstrap modal dialog wrapper. the content is wrapped in a modal-content.

Signatures

bootstrap_modal_dialog_tag(options = {}) do
  # content for modal
end

Examples

<%= bootstrap_modal_tag size: 'lg', id: 'fruit' do %>
  Check it out!!
<% end %>
# =>
<div id="fruit" class="modal modal-lg">
  <div class="modal-content">Check it out!!</div>
</div>


24
25
26
27
28
29
30
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 24

def bootstrap_modal_dialog_tag(options = {}, &block)
  return unless block_given?

  (:div, class: bootstrap_modal_classes(options), role: 'document') do
    (:div, capture(&block), class: 'modal-content')
  end
end

Creates a simple bootstrap modal footer. If called with a content block, that block defines the non-readonly button(s). Alternatively, if called with a label first parameter, then a default button is created with that label. If no block or label is defined, then the default “Don’t save” and “Save” buttons are returned.

Signatures

bootstrap_modal_footer_tag(options = {}) do
  # content for modal footer
end
bootstrap_modal_footer_tag(default_button_label, options = {})
bootstrap_modal_footer_tag(options = {})

Options

  • readonly: false - This will set whether or not a close button will appear in the footer, regardless of the buttons defined in the label/block. Defaults to false.

Examples

<%= bootstrap_modal_footer_tag('Button text', readonly: true)
# =>
<div class="modal-footer">
  <button name="button" type="submit" class="btn btn-default" data-dismiss="modal">
    Close
  </button>
</div>

<%= bootstrap_modal_footer_tag(readonly: false) do
  button_tag('Non-readonly default', class: 'btn btn-default', "data-dismiss": 'modal') +
    button_tag('Non-readonly primary', class: 'btn btn-primary', "data-dismiss": 'modal')
end %>
# =>
<div class="modal-footer">
  <button name="button" type="submit" class="btn btn-default" data-dismiss="modal">
    Non-readonly default
  </button>
  <button name="button" type="submit" class="btn btn-primary" data-dismiss="modal">
    Non-readonly primary
  </button>
</div>

<%= bootstrap_modal_footer_tag('Button text') %>
# =>
<div class="modal-footer">
  <button name="button" type="submit" class="btn btn-default" data-dismiss="modal">
    Button text
  </button>
</div>

<%= bootstrap_modal_footer_tag %>
# =>
<div class="modal-footer">
  <button name="button" type="submit" class="btn btn-default" data-dismiss="modal">
    Don&#39;t save
  </button>
  <input type="submit" name="commit" value="Save" class="btn btn-primary"
    disable_with="Saving&hellip;" data-disable-with="Save" />
</div>


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 148

def bootstrap_modal_footer_tag(*args, &block)
  options = args.extract_options!
  options.stringify_keys!

  (:div, class: 'modal-footer') do
    if options['readonly']
      bootstrap_modal_button('Close')
    elsif block_given?
      capture(&block)
    elsif args.first
      bootstrap_modal_button(args.first)
    else
      bootstrap_modal_save_buttons
    end
  end
end

#bootstrap_modal_header_tag(*args, &block) ⇒ Object

Creates a simple bootstrap modal header.

Signatures

bootstrap_modal_header_tag(content, options = {})
bootstrap_modal_header_tag(options = {}) do
  # content for modal header
end

Options

  • dismissible: false - This will set whether or not a close X button will appear, allowing the modal box to be dismissed by the user. Defaults to false.

Examples

<%= bootstrap_modal_header_tag do %>
  Check it out!!
<% end %>
# => <div class="modal-header">Check it out!!</div>


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 51

def bootstrap_modal_header_tag(*args, &block)
  return bootstrap_modal_header_tag(capture(&block), *args) if block_given?
  options = args.extract_options!
  options.stringify_keys!

  # unless options.include?('dismissible') && !options['dismissible']
  #   options['dismissible'] = true
  # end

  heading = (:h4, args.first, class: 'modal-title')
  heading = button_tag((:span, '×', "aria-hidden": 'true'),
                       type: 'button', class: 'close', "data-dismiss": 'modal',
                       "aria-label": 'Close') + heading if options.delete('dismissible')

  (:div, heading, class: 'modal-header')
end

#bootstrap_modal_save_buttonsObject



211
212
213
214
215
216
# File 'app/helpers/ndr_ui/bootstrap/modal_helper.rb', line 211

def bootstrap_modal_save_buttons
  bootstrap_modal_button("Don't save") +
    submit_tag('Save',
               class: 'btn btn-primary',
               disable_with: 'Saving&hellip;'.html_safe)
end