class ActiveDryForm::Input

Attributes

input_opts[R]
input_type[R]

Public Class Methods

new(builder, method_type, method, options) click to toggle source
# File lib/active_dry_form/input.rb, line 8
def initialize(builder, method_type, method, options)
  @builder = builder
  @method_type = method_type
  @method = method

  info = builder.object.info(method)
  @input_type = info[:type]
  @required = info[:required]

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

Public Instance Methods

css_classes() click to toggle source
# File lib/active_dry_form/input.rb, line 24
def css_classes
  [
    'input',
    @method_type,
    @input_type,
    @method,
    ('required' if @required),
    ('error' if error?(@method)),
  ].compact
end
error?(method) click to toggle source
# File lib/active_dry_form/input.rb, line 68
def error?(method)
  @builder.object.errors.key?(method)
end
error_text() click to toggle source
# File lib/active_dry_form/input.rb, line 56
def error_text
  return unless error?(@method)

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

  @builder.tag.div obj_error_text.join('<br />'), class: 'form-error is-visible'
end
hint_text() click to toggle source
# File lib/active_dry_form/input.rb, line 50
def hint_text
  return unless @hint_text

  @builder.tag.small @hint_text, class: 'help-text'
end
label() click to toggle source
# File lib/active_dry_form/input.rb, line 46
def label
  @builder.label(@method, @label_text) unless @label_opts == false
end
wrap_tag(input, label_last: nil) click to toggle source
# File lib/active_dry_form/input.rb, line 35
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