Class: Dsfr::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Dsfr::FormBuilder
- Includes:
- ActionView::Helpers::OutputSafetyHelper
- Defined in:
- lib/dsfr-form_builder.rb
Instance Attribute Summary collapse
-
#display_required_tags ⇒ Object
Returns the value of attribute display_required_tags.
Instance Method Summary collapse
- #dsfr_email_field(attribute, opts = {}) ⇒ Object
- #dsfr_error_message(attr) ⇒ Object
- #dsfr_input_field(attribute, input_kind, opts = {}) ⇒ Object
- #dsfr_input_group(attribute, opts, &block) ⇒ Object
- #dsfr_label_with_hint(attribute, opts = {}) ⇒ Object
- #dsfr_phone_field(attribute, opts = {}) ⇒ Object
- #dsfr_text_field(attribute, opts = {}) ⇒ Object
- #dsfr_url_field(attribute, opts = {}) ⇒ Object
- #hint_tag(text) ⇒ Object
-
#initialize(object_name, object, template, options) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #input_group_classes(attribute, opts) ⇒ Object
- #join_classes(arr) ⇒ Object
- #label_value(attribute, opts) ⇒ Object
- #required_tag ⇒ Object
Constructor Details
#initialize(object_name, object, template, options) ⇒ FormBuilder
Returns a new instance of FormBuilder.
10 11 12 13 |
# File 'lib/dsfr-form_builder.rb', line 10 def initialize(object_name, object, template, ) super self. = .fetch(:display_required_tags, true) end |
Instance Attribute Details
#display_required_tags ⇒ Object
Returns the value of attribute display_required_tags.
8 9 10 |
# File 'lib/dsfr-form_builder.rb', line 8 def @display_required_tags end |
Instance Method Details
#dsfr_email_field(attribute, opts = {}) ⇒ Object
19 20 21 |
# File 'lib/dsfr-form_builder.rb', line 19 def dsfr_email_field(attribute, opts = {}) dsfr_input_field(attribute, :email_field, opts) end |
#dsfr_error_message(attr) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/dsfr-form_builder.rb', line 64 def (attr) return if @object.errors[attr].none? @template.content_tag(:p, class: "fr-messages-group") do safe_join(@object.errors.(attr).map do |msg| @template.content_tag(:span, msg, class: "fr-message fr-message--error") end) end end |
#dsfr_input_field(attribute, input_kind, opts = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dsfr-form_builder.rb', line 37 def dsfr_input_field(attribute, input_kind, opts = {}) dsfr_input_group(attribute, opts) do @template.safe_join( [ dsfr_label_with_hint(attribute, opts), public_send(input_kind, attribute, class: "fr-input", **opts.except(:class)), (attribute), ] ) end end |
#dsfr_input_group(attribute, opts, &block) ⇒ Object
31 32 33 34 35 |
# File 'lib/dsfr-form_builder.rb', line 31 def dsfr_input_group(attribute, opts, &block) @template.content_tag(:div, class: input_group_classes(attribute, opts), data: opts[:data]) do yield(block) end end |
#dsfr_label_with_hint(attribute, opts = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dsfr-form_builder.rb', line 49 def dsfr_label_with_hint(attribute, opts = {}) label_class = "fr-label #{opts[:class]}" label(attribute, class: label_class) do = [label_value(attribute, opts)] .push(required_tag) if opts[:required] && .push(hint_tag(opts[:hint])) if opts[:hint] @template.safe_join() end end |
#dsfr_phone_field(attribute, opts = {}) ⇒ Object
27 28 29 |
# File 'lib/dsfr-form_builder.rb', line 27 def dsfr_phone_field(attribute, opts = {}) dsfr_input_field(attribute, :phone_field, opts) end |
#dsfr_text_field(attribute, opts = {}) ⇒ Object
15 16 17 |
# File 'lib/dsfr-form_builder.rb', line 15 def dsfr_text_field(attribute, opts = {}) dsfr_input_field(attribute, :text_field, opts) end |
#dsfr_url_field(attribute, opts = {}) ⇒ Object
23 24 25 |
# File 'lib/dsfr-form_builder.rb', line 23 def dsfr_url_field(attribute, opts = {}) dsfr_input_field(attribute, :url_field, opts) end |
#hint_tag(text) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/dsfr-form_builder.rb', line 74 def hint_tag(text) return "" unless text @template.content_tag(:span, class: "fr-hint-text") do text end @template.content_tag(:span, text, class: "fr-hint-text") end |
#input_group_classes(attribute, opts) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/dsfr-form_builder.rb', line 88 def input_group_classes(attribute, opts) join_classes( [ "fr-input-group", @object.errors[attribute].any? ? "fr-input-group--error" : nil, opts[:class], ] ) end |
#join_classes(arr) ⇒ Object
84 85 86 |
# File 'lib/dsfr-form_builder.rb', line 84 def join_classes(arr) arr.compact.join(" ") end |
#label_value(attribute, opts) ⇒ Object
98 99 100 101 102 |
# File 'lib/dsfr-form_builder.rb', line 98 def label_value(attribute, opts) return opts[:label] if opts[:label] (@object.try(:object) || @object).class.human_attribute_name(attribute) end |
#required_tag ⇒ Object
60 61 62 |
# File 'lib/dsfr-form_builder.rb', line 60 def required_tag @template.content_tag(:span, "*", class: "fr-text-error") end |