Class: Tramway::Form::Builder

Inherits:
Views::FormBuilder show all
Includes:
ColorsMethods, Utils::Field
Defined in:
app/components/tramway/form/builder.rb

Overview

Provides Tailwind-styled forms rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from ColorsMethods

ColorsMethods::TYPE_COLOR_MAP

Instance Attribute Summary

Attributes inherited from Views::FormBuilder

#template

Instance Method Summary collapse

Methods included from ColorsMethods

#normalized_type, #resolved_color, #type_color

Methods included from Utils::Field

#tramway_field

Constructor Details

#initialize(object_name, object, template, options) ⇒ Builder

Returns a new instance of Builder.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/components/tramway/form/builder.rb', line 13

def initialize(object_name, object, template, options)
  @horizontal = options[:horizontal] || false
  @remote = options[:remote_submit] || false

  options.merge!(class: [options[:class], 'flex flex-row items-center gap-2'].compact.join(' ')) if @horizontal

  @form_object_class = options[:form_object_class]

  if form_object(object)
    super(object_name, form_object(object), template, options)
  else
    super
  end

  @form_size = options[:size] || options['size'] || :medium
end

Instance Method Details

#check_box(attribute) ⇒ Object



87
88
89
# File 'app/components/tramway/form/builder.rb', line 87

def check_box(attribute, **, &)
  common_field(:checkbox, :check_box, attribute, **, &)
end

#common_field(component_name, input_method, attribute, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/tramway/form/builder.rb', line 30

def common_field(component_name, input_method, attribute, **options, &)
  sanitized_options = sanitize_options(options)

  component_class = "Tramway::Form::#{component_name.to_s.camelize}Component".constantize

  render(component_class.new(
           input: input(input_method),
           value: get_value(attribute, sanitized_options),
           **default_options(attribute, sanitized_options)
         ),
         &)
end

#date_field(attribute) ⇒ Object



68
69
70
# File 'app/components/tramway/form/builder.rb', line 68

def date_field(attribute, **, &)
  common_field(:date_field, :date_field, attribute, **, &)
end

#datetime_field(attribute) ⇒ Object



72
73
74
# File 'app/components/tramway/form/builder.rb', line 72

def datetime_field(attribute, **, &)
  common_field(:datetime_field, :datetime_field, attribute, **, &)
end

#email_field(attribute) ⇒ Object



47
48
49
# File 'app/components/tramway/form/builder.rb', line 47

def email_field(attribute, **, &)
  common_field(:text_field, :email_field, attribute, **, &)
end

#file_field(attribute, **options) ⇒ Object



80
81
82
83
84
85
# File 'app/components/tramway/form/builder.rb', line 80

def file_field(attribute, **options, &)
  sanitized_options = sanitize_options(options)
  input = super(attribute, **sanitized_options.merge(class: :hidden))

  render(Tramway::Form::FileFieldComponent.new(input:, **default_options(attribute, sanitized_options)), &)
end

#number_field(attribute) ⇒ Object



51
52
53
# File 'app/components/tramway/form/builder.rb', line 51

def number_field(attribute, **, &)
  common_field(:number_field, :number_field, attribute, **, &)
end

#password_field(attribute, **options) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/components/tramway/form/builder.rb', line 59

def password_field(attribute, **options, &)
  sanitized_options = sanitize_options(options)

  render(Tramway::Form::TextFieldComponent.new(
           input: input(:password_field),
           **default_options(attribute, sanitized_options)
         ), &)
end

#select(attribute, collection, **options) ⇒ Object



91
92
93
94
95
96
97
# File 'app/components/tramway/form/builder.rb', line 91

def select(attribute, collection, **options, &)
  if options[:multiple] || options[:autocomplete]
    tramway_select(attribute, collection, **options, &)
  else
    default_select(attribute, collection, **options, &)
  end
end

#submit(action, **options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/components/tramway/form/builder.rb', line 99

def submit(action, **options, &)
  sanitized_options = sanitize_options(options)

  render(
    Tramway::ButtonComponent.new(
      text: action,
      size: form_size,
      type: :will,
      options: sanitized_options.merge(name: :commit, type: :submit)
    ),
    &
  )
end

#text_area(attribute) ⇒ Object



55
56
57
# File 'app/components/tramway/form/builder.rb', line 55

def text_area(attribute, **, &)
  common_field(:text_area, :text_area, attribute, **, &)
end

#text_field(attribute) ⇒ Object



43
44
45
# File 'app/components/tramway/form/builder.rb', line 43

def text_field(attribute, **, &)
  common_field(:text_field, :text_field, attribute, **, &)
end

#time_field(attribute) ⇒ Object



76
77
78
# File 'app/components/tramway/form/builder.rb', line 76

def time_field(attribute, **, &)
  common_field(:time_field, :time_field, attribute, **, &)
end