Class: Munificent::Admin::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper
Defined in:
app/form_builders/munificent/admin/form_builder.rb

Constant Summary collapse

TEXT_FIELDS =
%i[
  email_field
  number_field
  password_field
  text_area
  text_field
].freeze
UNFLOATABLE_TYPES =
%i[
  select
].freeze

Instance Method Summary collapse

Instance Method Details

#check_box(name, label: name.to_s.humanize, **opts) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/form_builders/munificent/admin/form_builder.rb', line 37

def check_box(name, label: name.to_s.humanize, **opts)
  check_block = tag.div(class: "form-check") {
    super(name, class: "form-check-input", **check_box_opts(opts)) +
      label(name, label, class: "form-check-label")
  }

  wrap(check_block, field_name: name)
end

#field(type, name, label: name.to_s.humanize, **opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/form_builders/munificent/admin/form_builder.rb', line 19

def field(type, name, label: name.to_s.humanize, **opts)
  field_tag = case type
  when *TEXT_FIELDS
    public_send(type, name, class: "form-control", placeholder: (label unless table_mode?))
  when :select
    select(name, opts[:source], select_field_opts(opts), select_html_opts(opts))
  else
    send(type, name, class: "form-control")
  end

  wrap(
    label(name, label, class: "form-label"),
    field_tag,
    field_name: name,
    float: UNFLOATABLE_TYPES.exclude?(type),
  )
end

#money(name, default_currency: nil) ⇒ Object



55
56
57
58
59
60
61
# File 'app/form_builders/munificent/admin/form_builder.rb', line 55

def money(name, default_currency: nil)
  field(:select, "#{name}_currency",
    label: "Currency",
    source: Munificent::Currency.present_all,
    selected: (object.public_send("#{name}_currency").presence || default_currency),
  ) + field(:text_field, "human_#{name}", label: name.to_s.humanize)
end

#table(css_class: nil, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/form_builders/munificent/admin/form_builder.rb', line 46

def table(css_class: nil, &block)
  @table_mode = true
  contents = @template.capture(&block)
  @table_mode = false

  css_class = (%w[table table-bordered] + Array(css_class)).compact.join(" ")
  tag.table(contents, class: css_class)
end