Class: Booties::Modal

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/booties/modal.rb

Instance Method Summary collapse

Constructor Details

#initialize(view_context, id:, fade: true) ⇒ Modal

Instantiates a new Modal. Several helper methods like #content_tag will be delegated to view_context. The required keyword id will be used as the DOM ID of the modal element. By default, the modal will exhibit fading behavior, but this can be disabled by setting fade to a falsey value.

TODO: Pass additional arguments as attributes to top-level div.



15
16
17
18
19
# File 'lib/booties/modal.rb', line 15

def initialize(view_context, id:, fade: true)
  @view_context = view_context
  @id           = id
  @fade         = fade ? 'fade' : nil
end

Instance Method Details

#body(&block) ⇒ Object

Renders the main body of the modal. The content of the section is captured from block.



71
72
73
# File 'lib/booties/modal.rb', line 71

def body(&block)
   :div, capture(&block), class: 'modal-body'
end

#content(&block) ⇒ Object

Renders the content section of the modal. The content of the dialog is captured from block. The Modal object will be passed as a paramter to block.



47
48
49
50
51
# File 'lib/booties/modal.rb', line 47

def content(&block)
   :div, class: 'modal-content' do
    capture self, &block
  end
end

#dialog(&block) ⇒ Object

Renders the dialog section of the modal. block is passed to #content to fill in the content of the dialog.



37
38
39
40
41
# File 'lib/booties/modal.rb', line 37

def dialog(&block)
   :div, class: 'modal-dialog' do
    content &block
  end
end

#dismiss(content = nil, **options, &block) ⇒ Object

Renders a button for dismissing the modal. The label can be passed in through the content parameter. Otherwise, it will be captured from block. Additional HTML attributes for the button can be passed in through options.



97
98
99
100
101
102
# File 'lib/booties/modal.rb', line 97

def dismiss(content = nil, **options, &block)
  content ||= capture &block
  options[:class] ||= 'close'
  options.update data: { dismiss: 'modal' }, type: 'button'
  button_tag content, options
end

Renders the footer of the modal. The content of the section is captured from block.



78
79
80
# File 'lib/booties/modal.rb', line 78

def footer(&block)
   :div, capture(&block), class: 'modal-footer'
end

#header(&block) ⇒ Object

Renders the header section of a modal. block is passed to #title to render the title. In addition to the title, a dismissal button will be rendered. The content of the dismissal button is localized. It will look for booties.modal.dismiss_html. If that translation does not exist it will look for booties.modal.dismiss. If that does not exist either, it will default to the HTML times entity ×.



60
61
62
63
64
65
66
# File 'lib/booties/modal.rb', line 60

def header(&block)
  dismissal = t :'booties.modal.dismiss_html',
    default: [:'booties.modal.dismiss', raw('×')]
   :div, class: 'modal-header' do
    dismiss(dismissal) << title(&block)
  end
end

#render(&block) ⇒ Object

Renders the top-level div for the modal dialog. block is passed to #dialog to fill in the content of the modal. @id is used as the DOM ID of the modal. @fade is used to include or exclude fading behavior.



28
29
30
31
32
# File 'lib/booties/modal.rb', line 28

def render(&block)
   :div, class: ['modal', @fade], id: @id do
    dialog &block
  end
end

#title(content = nil, &block) ⇒ Object

Renders the title of the modal. The content can be passed through the content parameter. Otherwise it will be captured from block.

#title is called automatically from #header.



87
88
89
90
# File 'lib/booties/modal.rb', line 87

def title(content = nil, &block)
  content ||= capture &block
   :h4, content, class: 'modal-title'
end