Class: Ui::FomanticFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/ui/fomantic_form_builder.rb

Constant Summary collapse

COLUMN_WORDS =
%w[
  one two three four five six seven eight nine ten
  eleven twelve thirteen fourteen fifteen sixteen
].freeze
CALENDAR_TYPE_MAP =

──────────────────────────────────────────────────────────────────────────Date/time inputs — wrapped in Fomantic calendar ──────────────────────────────────────────────────────────────────────────

{
  date_field:           "date",
  datetime_local_field: "datetime",
  time_field:           "time",
  month_field:          "month",
  week_field:           "date"
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object

──────────────────────────────────────────────────────────────────────────Check box ──────────────────────────────────────────────────────────────────────────



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/ui/fomantic_form_builder.rb', line 153

def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
  wrap_in_field = options.delete(:field)
  label_text    = options.delete(:label) { label_for(attribute) }
  kind          = options.delete(:kind) { :checkbox }
  size          = options.delete(:size)
  inverted      = options.delete(:inverted)
  fitted        = options.delete(:fitted)
  right_aligned = options.delete(:right_aligned)

  checkbox_html = super(attribute, options, checked_value, unchecked_value)
  result = checkbox_ui(checkbox_html, label_text, kind,
              size: size, inverted: inverted, fitted: fitted, right_aligned: right_aligned)

  wrap_in_field ? fomantic_field(attribute, { suppress_label: true }, result) : result
end

#emoji_field(attribute, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────Emoji picker ──────────────────────────────────────────────────────────────────────────



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/ui/fomantic_form_builder.rb', line 111

def emoji_field(attribute, options = {})
  wrap_in_field = options.delete(:field)

  current = object&.public_send(attribute) rescue nil
  name = object_name ? "#{object_name}[#{attribute}]" : attribute.to_s

  result = @template.tag.div(data: { controller: "fui-emoji-picker" }) {
    @template.safe_join([
      @template.hidden_field_tag(name, current, data: { fui_emoji_picker_target: "input" }),
      @template.tag.button(
        type: "button",
        class: "ui basic button",
        data: { fui_emoji_picker_target: "preview", action: "click->fui-emoji-picker#toggle" }
      ) { (current.presence || "Pick emoji").html_safe },
      @template.tag.div(
        style: "display:none; position:absolute; z-index:1000;",
        data: { fui_emoji_picker_target: "dropdown" }
      )
    ])
  }

  wrap_in_field ? fomantic_field(attribute, options, result) : result
end

#error_message(header, messages = []) ⇒ Object

──────────────────────────────────────────────────────────────────────────Form-level message helpers ──────────────────────────────────────────────────────────────────────────



269
270
271
# File 'app/helpers/ui/fomantic_form_builder.rb', line 269

def error_message(header, messages = [])
  form_message("error", header, messages)
end

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

──────────────────────────────────────────────────────────────────────────fields_group — wraps children in <div class=“fields …”> ──────────────────────────────────────────────────────────────────────────



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/helpers/ui/fomantic_form_builder.rb', line 246

def fields_group(options = {}, &block)
  equal_width = options.delete(:equal_width)
  inline      = options.delete(:inline)
  count       = options.delete(:count)
  extra       = options.delete(:class)

  count_word = COLUMN_WORDS[count.to_i - 1] if count

  css = class_names(
    count_word,
    "fields",
    ("equal width" if equal_width),
    ("inline" if inline),
    extra
  )

  @template.tag.div(class: css, &block)
end

#file_field(attribute, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────File field ──────────────────────────────────────────────────────────────────────────



193
194
195
196
197
198
199
# File 'app/helpers/ui/fomantic_form_builder.rb', line 193

def file_field(attribute, options = {})
  wrap_in_field = options.delete(:field)

  input_html = super(attribute, options)

  wrap_in_field ? fomantic_field(attribute, options, input_html) : input_html
end

#hidden_field(attribute, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────Hidden field (no wrapper ever) ──────────────────────────────────────────────────────────────────────────



205
206
207
# File 'app/helpers/ui/fomantic_form_builder.rb', line 205

def hidden_field(attribute, options = {})
  super(attribute, options)
end

#info_message(header, messages = []) ⇒ Object



281
282
283
# File 'app/helpers/ui/fomantic_form_builder.rb', line 281

def info_message(header, messages = [])
  form_message("info", header, messages)
end

#label(attribute, text = nil, options = {}, &block) ⇒ Object

──────────────────────────────────────────────────────────────────────────Label override — produces a plain <label> ──────────────────────────────────────────────────────────────────────────



238
239
240
# File 'app/helpers/ui/fomantic_form_builder.rb', line 238

def label(attribute, text = nil, options = {}, &block)
  super(attribute, text, options, &block)
end

#radio_button(attribute, value, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────Radio button ──────────────────────────────────────────────────────────────────────────



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/ui/fomantic_form_builder.rb', line 173

def radio_button(attribute, value, options = {})
  wrap_in_field = options.delete(:field)
  label_text    = options.delete(:label) { value.to_s.humanize }
  kind          = options.delete(:kind) { :radio }
  size          = options.delete(:size)
  inverted      = options.delete(:inverted)
  fitted        = options.delete(:fitted)
  right_aligned = options.delete(:right_aligned)

  radio_html = super(attribute, value, options)
  result = checkbox_ui(radio_html, label_text, kind,
              size: size, inverted: inverted, fitted: fitted, right_aligned: right_aligned)

  wrap_in_field ? fomantic_field(attribute, { suppress_label: true }, result) : result
end

#select(attribute, choices = nil, options = {}, html_options = {}, &block) ⇒ Object

──────────────────────────────────────────────────────────────────────────Select / Dropdown ──────────────────────────────────────────────────────────────────────────



139
140
141
142
143
144
145
146
147
# File 'app/helpers/ui/fomantic_form_builder.rb', line 139

def select(attribute, choices = nil, options = {}, html_options = {}, &block)
  wrap_in_field = options.delete(:field)
  use_dropdown = options.delete(:dropdown)

  raw_select = super(attribute, choices, options, html_options, &block)
  result = use_dropdown ? dropdown_wrap(raw_select) : raw_select

  wrap_in_field ? fomantic_field(attribute, options, result) : result
end

#submit(value = nil, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────Submit button ──────────────────────────────────────────────────────────────────────────



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/helpers/ui/fomantic_form_builder.rb', line 213

def submit(value = nil, options = {})
  color   = options.delete(:color)
  basic   = options.delete(:basic)
  inverted = options.delete(:inverted)
  size    = options.delete(:size)
  icon    = options.delete(:icon)

  button_class = class_names(
    "ui", "button",
    color,
    size,
    ("basic" if basic),
    ("inverted" if inverted),
    options.delete(:class)
  )
  options[:class] = button_class

  icon_html = icon ? @template.tag.i(class: "#{icon} icon") : "".html_safe
  icon_html + super(value, options)
end

#success_message(header, body = nil) ⇒ Object



273
274
275
# File 'app/helpers/ui/fomantic_form_builder.rb', line 273

def success_message(header, body = nil)
  form_message("success", header, [], body)
end

#text_area(attribute, options = {}) ⇒ Object

──────────────────────────────────────────────────────────────────────────Text area ──────────────────────────────────────────────────────────────────────────



97
98
99
100
101
102
103
104
105
# File 'app/helpers/ui/fomantic_form_builder.rb', line 97

def text_area(attribute, options = {})
  wrap_in_field = options.delete(:field)
  transparent = options.delete(:transparent)

  options[:class] = class_names(("transparent" if transparent), options[:class])
  input_html = super(attribute, options)

  wrap_in_field ? fomantic_field(attribute, options, input_html) : input_html
end

#warning_message(header, messages = []) ⇒ Object



277
278
279
# File 'app/helpers/ui/fomantic_form_builder.rb', line 277

def warning_message(header, messages = [])
  form_message("warning", header, messages)
end