Class: ActiveDryForm::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/active_dry_form/input.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, builder_method, field, options) ⇒ Input

Returns a new instance of Input.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/active_dry_form/input.rb', line 6

def initialize(builder, builder_method, field, options)
  @builder = builder
  @builder_method = builder_method
  @field = field

  @label_opts = options[:label]
  @label_text = options[:label_text]
  @hint_text = options[:hint]
  @required = options[:required]
  @input_user_options = options.except(:label, :hint, :label_text)
end

Instance Method Details

#css_classesObject



18
19
20
21
22
23
24
25
# File 'lib/active_dry_form/input.rb', line 18

def css_classes
  [
    ActiveDryForm.config.css_classes.input,
    @builder_method,
    (ActiveDryForm.config.css_classes.input_required if @required),
    (ActiveDryForm.config.css_classes.input_error if error?(@field)),
  ].compact
end

#error?(field) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/active_dry_form/input.rb', line 60

def error?(field)
  @builder.object.errors.key?(field)
end

#error_textObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_dry_form/input.rb', line 48

def error_text
  return unless error?(@field)

  obj_error_text =
    case e = @builder.object.errors[@field]
    when Hash then e.values
    else e
    end

  @builder.tag.div obj_error_text.join('<br />').html_safe, class: ActiveDryForm.config.css_classes.error
end

#hint_textObject



42
43
44
45
46
# File 'lib/active_dry_form/input.rb', line 42

def hint_text
  return unless @hint_text

  @builder.tag.small @hint_text, class: ActiveDryForm.config.css_classes.hint
end

#labelObject



38
39
40
# File 'lib/active_dry_form/input.rb', line 38

def label
  @builder.label(@field, @label_text) unless @label_opts == false
end

#wrap_tag(input, label_last: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/active_dry_form/input.rb', line 27

def wrap_tag(input, label_last: nil)
  @builder.tag.div class: css_classes do
    [
      label_last ? input : label,
      label_last ? label : input,
      hint_text,
      error_text,
    ].compact.join.html_safe
  end
end