Class: Kube::Station::FomanticFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/kube/station/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

Instance Method Summary collapse

Instance Method Details

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

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



117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 117

def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
  label_text = options.delete(:label) { label_for(attribute) }
  kind       = options.delete(:kind) { :checkbox } # :checkbox | :slider | :toggle

  fomantic_field(attribute, options.merge(suppress_label: true)) do |attr, opts|
    opts.delete(:input_class)
    checkbox_html = super(attr, opts, checked_value, unchecked_value)
    checkbox_ui(checkbox_html, label_text, kind)
  end
end

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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 77

def emoji_field(attribute, options = {})
  fomantic_field(attribute, options) do |attr, opts|
    current = object&.public_send(attr) rescue nil
    name = object_name ? "#{object_name}[#{attr}]" : attr.to_s

    @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" }
        )
      ])
    }
  end
end

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

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



226
227
228
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 226

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

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

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

Options:

equal_width: Boolean – adds "equal width"
inline:      Boolean – adds "inline"
count:       Integer – "N fields" (evenly divided)

──────────────────────────────────────────────────────────────────────────



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 203

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 ──────────────────────────────────────────────────────────────────────────



146
147
148
149
150
151
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 146

def file_field(attribute, options = {})
  fomantic_field(attribute, options) do |attr, opts|
    opts.delete(:input_class)
    super(attr, opts)
  end
end

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

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



157
158
159
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 157

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

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



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

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

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

──────────────────────────────────────────────────────────────────────────Label override — produces a plain <label> (called internally too) ──────────────────────────────────────────────────────────────────────────



190
191
192
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 190

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

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

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



132
133
134
135
136
137
138
139
140
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 132

def radio_button(attribute, value, options = {})
  label_text = options.delete(:label) { value.to_s.humanize }

  fomantic_field(attribute, options.merge(suppress_label: true)) do |attr, opts|
    opts.delete(:input_class)
    radio_html = super(attr, value, opts)
    checkbox_ui(radio_html, label_text, :radio)
  end
end

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

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



103
104
105
106
107
108
109
110
111
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 103

def select(attribute, choices = nil, options = {}, html_options = {}, &block)
  use_dropdown = options.delete(:dropdown)
  fomantic_field(attribute, options) do |attr, opts|
    merged_html = html_options.merge(class: class_names(html_options[:class], opts.delete(:input_class)))
    raw_select  = super(attr, choices, opts, merged_html, &block)

    use_dropdown ? dropdown_wrap(raw_select) : raw_select
  end
end

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

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 165

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



230
231
232
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 230

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

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

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



64
65
66
67
68
69
70
71
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 64

def text_area(attribute, options = {})
  transparent = options.delete(:transparent)
  fomantic_field(attribute, options) do |attr, opts|
    input_class = class_names(("transparent" if transparent), opts.delete(:input_class))
    opts[:class] = input_class.presence
    super(attr, opts)
  end
end

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



234
235
236
# File 'app/helpers/kube/station/fomantic_form_builder.rb', line 234

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