Class: Bootstrap5RailsExtensions::ModalHelper::ModalBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap5_rails_extensions/modal_helper.rb

Overview

モーダルの各セクションを構築する小さなDSL用ビルダー。

Instance Method Summary collapse

Constructor Details

#initialize(view_context, modal_id:, default_title:) ⇒ ModalBuilder

Returns a new instance of ModalBuilder.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 77

def initialize(view_context, modal_id:, default_title:)
  @view = view_context
  @modal_id = modal_id
  @default_title = default_title
  @header_html = nil
  @header_full = false
  @body_fragments = []
  @body_full = nil
  @footer_fragments = []
  @footer_full = nil
  @current_form_builder = nil
end

Instance Method Details

#body(content = nil, full: false, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 107

def body(content = nil, full: false, &block)
  html = extract_content(content, &block)

  if full
    raise ArgumentError, _("モーダル本文はfull: trueでは一度だけ設定できます") if @body_full || @body_fragments.any?

    @body_full = html
  else
    @body_fragments << html
  end
  self
end


120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 120

def footer(content = nil, full: false, &block)
  html = extract_content(content, &block)

  if full
    raise ArgumentError, _("モーダルフッターはfull: trueでは一度だけ設定できます") if @footer_full || @footer_fragments.any?

    @footer_full = html
  else
    @footer_fragments << html
  end
  self
end

#header(content = nil, full: false, &block) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 98

def header(content = nil, full: false, &block)
  raise ArgumentError, _("モーダルヘッダーは一度だけ定義できます") if @header_html

  html = extract_content(content, &block)
  @header_full = full
  @header_html = html
  self
end

#render_modal_contentObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 133

def render_modal_content
  body_markup = build_body_markup

  header_markup = build_header_markup
  footer_markup = build_footer_markup

  @view.(:div, class: "modal-content") do
    @view.concat(header_markup) if header_markup
    @view.concat(body_markup)
    @view.concat(footer_markup) if footer_markup
  end
end

#with_form(form_builder) ⇒ Object



90
91
92
93
94
95
96
# File 'app/helpers/bootstrap5_rails_extensions/modal_helper.rb', line 90

def with_form(form_builder)
  previous = @current_form_builder
  @current_form_builder = form_builder
  yield(self)
ensure
  @current_form_builder = previous
end