Class: CafeCar::Inputs::BaseInput

Inherits:
Object
  • Object
show all
Defined in:
lib/cafe_car/inputs/base_input.rb

Overview

Renders one bound form input for a field.

Each field type is a small subclass that names the form-builder helper it emits through (and any per-type options). .build maps a field's resolved input key (what FieldInfo#input returns) to the right subclass, so which input a field gets stays single-sourced on the field metadata — these objects only render it.

Styling rides the shared ui/Input.css (native input/select/textarea), and copy (placeholder, prompt, hint) comes from the field's locale via FieldInfo — no hardcoded strings, no styles outside components.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form:, method:, template:, args: [], **options, &block) ⇒ BaseInput

Returns a new instance of BaseInput.



40
41
42
43
44
45
46
47
# File 'lib/cafe_car/inputs/base_input.rb', line 40

def initialize(form:, method:, template:, args: [], **options, &block)
  @form     = form
  @method   = method
  @template = template
  @args     = args
  @options  = options
  @block    = block
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



38
39
40
# File 'lib/cafe_car/inputs/base_input.rb', line 38

def args
  @args
end

#formObject (readonly)

Returns the value of attribute form.



38
39
40
# File 'lib/cafe_car/inputs/base_input.rb', line 38

def form
  @form
end

#methodObject (readonly)

Returns the value of attribute method.



38
39
40
# File 'lib/cafe_car/inputs/base_input.rb', line 38

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/cafe_car/inputs/base_input.rb', line 38

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



38
39
40
# File 'lib/cafe_car/inputs/base_input.rb', line 38

def template
  @template
end

Class Method Details

.build(key) ⇒ Object



33
34
35
36
# File 'lib/cafe_car/inputs/base_input.rb', line 33

def self.build(key, **, &)
  klass = classes[key] or raise ArgumentError, "No input component for #{key.inspect}"
  klass.new(**, &)
end

.classesObject

Maps a resolved input key (FieldInfo#input) to its component class. Kept lazy so the subclass constants resolve through Zeitwerk at call time.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cafe_car/inputs/base_input.rb', line 16

def self.classes
  {
    text_field:     StringInput,
    text_area:      TextAreaInput,
    number_field:   NumberInput,
    check_box:      BooleanInput,
    date_field:     DateInput,
    datetime_field: DatetimeInput,
    password_field: PasswordInput,
    file_field:     FileInput,
    rich_text_area: RichTextInput,
    enum:           SelectInput,
    association:    AssociationInput,
    fields_for:     NestedInput
  }
end

Instance Method Details

#defaultsObject

Per-type option defaults, filled only where the caller left them unset.



55
# File 'lib/cafe_car/inputs/base_input.rb', line 55

def defaults = {}

#helperObject

The form-builder helper this input renders through. Subclasses override.



52
# File 'lib/cafe_car/inputs/base_input.rb', line 52

def helper = :text_field

#html_safe?Boolean

Returns:

  • (Boolean)


65
# File 'lib/cafe_car/inputs/base_input.rb', line 65

def html_safe? = true

#infoObject



49
# File 'lib/cafe_car/inputs/base_input.rb', line 49

def info = form.info(method)

#render_optionsObject



60
# File 'lib/cafe_car/inputs/base_input.rb', line 60

def render_options = defaults.merge(options)

#text_hintsObject

Locale-driven copy for free-text inputs — never a hardcoded string.



58
# File 'lib/cafe_car/inputs/base_input.rb', line 58

def text_hints = { placeholder: info.placeholder, autocomplete: info.autocomplete }.compact

#to_htmlObject



62
# File 'lib/cafe_car/inputs/base_input.rb', line 62

def to_html = form.public_send(helper, method, *args, **render_options, &@block)

#to_sObject



64
# File 'lib/cafe_car/inputs/base_input.rb', line 64

def to_s       = to_html.to_s