Class: CafeCar::Inputs::BaseInput
- Inherits:
-
Object
- Object
- CafeCar::Inputs::BaseInput
- 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.
Direct Known Subclasses
AssociationInput, BooleanInput, DateInput, DatetimeInput, FileInput, NestedInput, NumberInput, PasswordInput, RichTextInput, SelectInput, StringInput, TextAreaInput
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
- .build(key) ⇒ Object
-
.classes ⇒ Object
Maps a resolved input key (
FieldInfo#input) to its component class.
Instance Method Summary collapse
-
#defaults ⇒ Object
Per-type option defaults, filled only where the caller left them unset.
-
#helper ⇒ Object
The form-builder helper this input renders through.
- #html_safe? ⇒ Boolean
- #info ⇒ Object
-
#initialize(form:, method:, template:, args: [], **options, &block) ⇒ BaseInput
constructor
A new instance of BaseInput.
- #render_options ⇒ Object
-
#text_hints ⇒ Object
Locale-driven copy for free-text inputs — never a hardcoded string.
- #to_html ⇒ Object
- #to_s ⇒ Object
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: [], **, &block) @form = form @method = method @template = template @args = args @options = @block = block end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
38 39 40 |
# File 'lib/cafe_car/inputs/base_input.rb', line 38 def args @args end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
38 39 40 |
# File 'lib/cafe_car/inputs/base_input.rb', line 38 def form @form end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
38 39 40 |
# File 'lib/cafe_car/inputs/base_input.rb', line 38 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
38 39 40 |
# File 'lib/cafe_car/inputs/base_input.rb', line 38 def @options end |
#template ⇒ Object (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 |
.classes ⇒ Object
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
#defaults ⇒ Object
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 = {} |
#helper ⇒ Object
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
65 |
# File 'lib/cafe_car/inputs/base_input.rb', line 65 def html_safe? = true |
#info ⇒ Object
49 |
# File 'lib/cafe_car/inputs/base_input.rb', line 49 def info = form.info(method) |
#render_options ⇒ Object
60 |
# File 'lib/cafe_car/inputs/base_input.rb', line 60 def = defaults.merge() |
#text_hints ⇒ Object
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_html ⇒ Object
62 |
# File 'lib/cafe_car/inputs/base_input.rb', line 62 def to_html = form.public_send(helper, method, *args, **, &@block) |
#to_s ⇒ Object
64 |
# File 'lib/cafe_car/inputs/base_input.rb', line 64 def to_s = to_html.to_s |