Class: StimulusPlumbers::Form::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_plumbers/form/base.rb

Direct Known Subclasses

Field

Constant Summary collapse

OPTIONS =
%i[label hint error required layout floating].freeze

Instance Method Summary collapse

Constructor Details

#initialize(template, label: nil, hint: nil, error: nil, required: false, layout: :stacked, floating: nil, **kwargs) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stimulus_plumbers/form/base.rb', line 10

def initialize(
  template,
  label: nil,
  hint: nil,
  error: nil,
  required: false,
  layout: :stacked,
  floating: nil,
  **kwargs
)
  @template = template
  @label    = label
  @hint     = hint
  @error    = error
  @required = required
  @layout   = layout.to_sym
  @floating = floating
  @kwargs   = kwargs
end

Instance Method Details

#described_by(object, attribute, input_id) ⇒ Object



38
39
40
41
42
43
# File 'lib/stimulus_plumbers/form/base.rb', line 38

def described_by(object, attribute, input_id)
  ids = []
  ids << hint_id(input_id) if @hint.present?
  ids.concat(build_error_ids(object, attribute, input_id))
  ids.join(" ").presence
end

#error?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/stimulus_plumbers/form/base.rb', line 34

def error?(object, attribute)
  build_errors(object, attribute).any?
end

#render_errors(object, attribute, input_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/stimulus_plumbers/form/base.rb', line 49

def render_errors(object, attribute, input_id)
  errs = build_errors(object, attribute)
  return if errs.none?

  @template.safe_join(
    errs.map.with_index do |message, i|
      Fields::Error.new(@template).render(message: message, id: build_error_ids(object, attribute, input_id)[i])
    end
  )
end

#render_hint(input_id) ⇒ Object



45
46
47
# File 'lib/stimulus_plumbers/form/base.rb', line 45

def render_hint(input_id)
  Fields::Hint.new(@template).render(text: @hint, id: hint_id(input_id)) if @hint.present?
end

#themeObject



30
31
32
# File 'lib/stimulus_plumbers/form/base.rb', line 30

def theme
  StimulusPlumbers.config.theme.current
end