Class: Iron::FormBuilder

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

Constant Summary collapse

INPUT_FIELD_TYPES =
%i[
  text_field
  password_field
  email_field
  number_field
  tel_field
  url_field
  search_field
  date_field
  time_field
  datetime_field
  datetime_local_field
  month_field
  week_field
  color_field
].freeze

Instance Method Summary collapse

Instance Method Details

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



43
44
45
46
47
48
49
50
# File 'app/helpers/iron/form_builder.rb', line 43

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  return super if options.delete(:unstyled)

  @template.(:div, class: "group/checkbox checkbox") do
    super(method, options, checked_value, unchecked_value) +
    @template.icon("check")
  end
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/iron/form_builder.rb', line 72

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  selected_value = object.send(method) rescue nil

  @template.render partial: "iron/shared/select", locals: {
    field_name: html_options[:name] || field_name(method),
    select_id: html_options[:id] || field_id(method),
    selected_value: selected_value,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    options: options,
    html_options: html_options.except(:name)
  }
end

#error_for(method) ⇒ Object



30
31
32
33
34
35
36
# File 'app/helpers/iron/form_builder.rb', line 30

def error_for(method)
  if object&.errors&.[](method)&.any?
    @template.(:div, class: "text-red-500 text-sm font-medium mt-1") do
      object.errors[method].join(", ")
    end
  end
end

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



38
39
40
41
# File 'app/helpers/iron/form_builder.rb', line 38

def label(method, text = nil, options = {}, &block)
  options[:class] = @template.tw("text-sm/6 font-medium", options[:class])
  super(method, text, options, &block)
end

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/iron/form_builder.rb', line 87

def select(method, choices = nil, options = {}, html_options = {}, &block)
  selected_value = options[:selected] || (object.send(method) rescue nil)

  # Convert choices to a uniform format
  collection = case choices
  when Hash
    choices.map { |text, value| OpenStruct.new(text: text, value: value) }
  when Array
    choices.map do |item|
      if item.is_a?(Array)
        OpenStruct.new(text: item[0], value: item[1])
      else
        OpenStruct.new(text: item, value: item)
      end
    end
  else
    []
  end

  @template.render partial: "iron/shared/select", locals: {
    field_name: html_options[:name] || field_name(method),
    select_id: html_options[:id] || field_id(method),
    selected_value: selected_value,
    collection: collection,
    value_method: :value,
    text_method: :text,
    options: options,
    html_options: html_options.except(:name)
  }
end

#toggle(method, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/iron/form_builder.rb', line 52

def toggle(method, options = {})
  value = object&.send(method)
  checked = ActiveModel::Type::Boolean.new.cast(value)

  hidden = @template.hidden_field_tag(field_name(method), "0", id: nil)
  toggle_html = @template.(:div, class: "group toggle") do
    @template.(:span, nil, class: "knob") +
    @template.tag(:input,
      type: "checkbox",
      name: field_name(method),
      id: field_id(method),
      value: "1",
      checked: checked,
      **options
    )
  end

  hidden + toggle_html
end