Class: NitroKit::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/components/nitro_kit/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#button(value = nil, **attrs, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'app/components/nitro_kit/form_builder.rb', line 82

def button(value = nil, **attrs, &block)
  if value.nil? && !block_given?
    value = "Save changes"
  end

  @template.render(NitroKit::Button.new(value, **attrs), &block)
end

#checkbox(method, checked_value = "1", unchecked_value = "0", *args, include_hidden: true, **attrs) ⇒ Object



64
65
66
67
68
69
70
# File 'app/components/nitro_kit/form_builder.rb', line 64

def checkbox(method, checked_value = "1", unchecked_value = "0", *args, include_hidden: true, **attrs)
  if include_hidden
    @template.concat(hidden_field(method, value: unchecked_value))
  end

  field(method, *args, as: :checkbox, label: false, value: checked_value, **attrs)
end

#field(field_name, label: nil, errors: nil, **attrs, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/components/nitro_kit/form_builder.rb', line 11

def field(field_name, label: nil, errors: nil, **attrs, &block)
  if label.nil?
    label = attrs.fetch(:label, field_name.to_s.humanize)
  end

  if errors.nil?
    errors = object && object.respond_to?(:errors) && object.errors.include?(field_name) ? object
      .errors
      .full_messages_for(field_name) : nil
  end

  @template.render(NitroKit::Field.new(self, field_name, label:, errors:, **attrs), &block)
end

#fieldset(**attrs, &block) ⇒ Object

Fields



7
8
9
# File 'app/components/nitro_kit/form_builder.rb', line 7

def fieldset(**attrs, &block)
  @template.render(NitroKit::Fieldset.new(**attrs), &block)
end

#group(**attrs, &block) ⇒ Object



25
26
27
# File 'app/components/nitro_kit/form_builder.rb', line 25

def group(**attrs, &block)
  @template.render(FieldGroup.new(**attrs), &block)
end

#radio_button(method, value = "1", **attrs) ⇒ Object



60
61
62
# File 'app/components/nitro_kit/form_builder.rb', line 60

def radio_button(method, value = "1", **attrs)
  field(method, as: :radio_button, label: false, value:, **attrs)
end

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/components/nitro_kit/form_builder.rb', line 90

def select(method, choices = nil, options = {}, html_options = {}, &block)
  field_options = {
    as: :select,
    options: choices,
    include_blank: options[:include_blank],
    prompt: options[:prompt]
  }.compact

  field_attributes = options.except(:include_blank, :prompt, :selected)

  field(method, **field_options, **field_attributes, **html_options, &block)
end

#submit(value = nil, **attrs, &block) ⇒ Object

Buttons



74
75
76
77
78
79
80
# File 'app/components/nitro_kit/form_builder.rb', line 74

def submit(value = nil, **attrs, &block)
  if value.nil? && !block_given?
    value = "Save changes"
  end

  @template.render(NitroKit::Button.new(value, variant: :primary, type: :submit, **attrs), &block)
end