Class: ActiveDryForm::Input
- Inherits:
-
Object
- Object
- ActiveDryForm::Input
- Defined in:
- lib/active_dry_form/input.rb
Instance Method Summary collapse
- #css_classes ⇒ Object
- #error?(field) ⇒ Boolean
- #error_text ⇒ Object
- #hint_text ⇒ Object
-
#initialize(builder, builder_method, field, options) ⇒ Input
constructor
A new instance of Input.
- #label ⇒ Object
- #wrap_tag(input, label_last: nil) ⇒ Object
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 17 18 19 |
# File 'lib/active_dry_form/input.rb', line 6 def initialize(builder, builder_method, field, ) @builder = builder @form_object = builder.object @template = builder.instance_variable_get(:@template) @builder_method = builder_method @field = field @label_opts = [:label] @label_text = [:label_text] @hint_text = [:hint] @required = [:required] @input_user_options = .except(:label, :hint, :label_text) end |
Instance Method Details
#css_classes ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/active_dry_form/input.rb', line 21 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
63 64 65 |
# File 'lib/active_dry_form/input.rb', line 63 def error?(field) @form_object.errors.key?(field) end |
#error_text ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active_dry_form/input.rb', line 51 def error_text return unless error?(@field) obj_error_text = case e = @form_object.errors[@field] when Hash then e.values else e end @template.content_tag(:div, obj_error_text.join('<br />').html_safe, class: ActiveDryForm.config.css_classes.error) end |
#hint_text ⇒ Object
45 46 47 48 49 |
# File 'lib/active_dry_form/input.rb', line 45 def hint_text return unless @hint_text @template.content_tag(:small, @hint_text, class: ActiveDryForm.config.css_classes.hint) end |
#label ⇒ Object
41 42 43 |
# File 'lib/active_dry_form/input.rb', line 41 def label @builder.label(@field, @label_text) unless @label_opts == false end |
#wrap_tag(input, label_last: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_dry_form/input.rb', line 30 def wrap_tag(input, label_last: nil) @template.content_tag(:div, class: css_classes) do [ label_last ? input : label, label_last ? label : input, hint_text, error_text, ].compact.join.html_safe end end |