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_check_box(attribute, opts = {}) ⇒ Object
- #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_number_field(attribute, opts = {}) ⇒ Object
- #dsfr_phone_field(attribute, opts = {}) ⇒ Object
- #dsfr_radio_buttons(attribute, choices, legend: nil, hint: nil, **opts) ⇒ Object
- #dsfr_radio_option(attribute, value:, label_text:, hint:, **opts) ⇒ Object
- #dsfr_select(attribute, choices, input_options: {}, **opts) ⇒ Object
- #dsfr_select_tag(attribute, choices, opts) ⇒ Object
- #dsfr_text_area(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_check_box(attribute, opts = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dsfr-form_builder.rb', line 57 def dsfr_check_box(attribute, opts = {}) @template.content_tag(:div, class: "fr-checkbox-group") do @template.safe_join( [ check_box(attribute, opts), label(attribute, class: "fr-label") ] ) end end |
#dsfr_email_field(attribute, opts = {}) ⇒ Object
23 24 25 |
# File 'lib/dsfr-form_builder.rb', line 23 def dsfr_email_field(attribute, opts = {}) dsfr_input_field(attribute, :email_field, opts) end |
#dsfr_error_message(attr) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/dsfr-form_builder.rb', line 134 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
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dsfr-form_builder.rb', line 45 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, :hint)), (attribute) ] ) end end |
#dsfr_input_group(attribute, opts, &block) ⇒ Object
39 40 41 42 43 |
# File 'lib/dsfr-form_builder.rb', line 39 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
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dsfr-form_builder.rb', line 119 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_number_field(attribute, opts = {}) ⇒ Object
35 36 37 |
# File 'lib/dsfr-form_builder.rb', line 35 def dsfr_number_field(attribute, opts = {}) dsfr_input_field(attribute, :number_field, opts) end |
#dsfr_phone_field(attribute, opts = {}) ⇒ Object
31 32 33 |
# File 'lib/dsfr-form_builder.rb', line 31 def dsfr_phone_field(attribute, opts = {}) dsfr_input_field(attribute, :phone_field, opts) end |
#dsfr_radio_buttons(attribute, choices, legend: nil, hint: nil, **opts) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dsfr-form_builder.rb', line 84 def (attribute, choices, legend: nil, hint: nil, **opts) legend_content = @template.safe_join([ legend || @object.class.human_attribute_name(attribute), hint ? hint_tag(hint) : nil ].compact) @template.content_tag(:fieldset, class: "fr-fieldset") do @template.safe_join( [ @template.content_tag(:legend, legend_content, class: "fr-fieldset__legend--regular fr-fieldset__legend"), choices.map { |c| dsfr_radio_option(attribute, value: c[:value], label_text: c[:label], hint: c[:hint], **opts) } ] ) end end |
#dsfr_radio_option(attribute, value:, label_text:, hint:, **opts) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dsfr-form_builder.rb', line 99 def dsfr_radio_option(attribute, value:, label_text:, hint:, **opts) @template.content_tag(:div, class: "fr-fieldset__element") do @template.content_tag(:div, class: "fr-radio-group") do @template.safe_join( [ (attribute, value, **opts), label([ attribute, value ].join("_").to_sym) do @template.safe_join( [ label_text, hint.present? ? @template.content_tag(:span, hint, class: "fr-hint-text") : nil ] ) end ] ) end end end |
#dsfr_select(attribute, choices, input_options: {}, **opts) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dsfr-form_builder.rb', line 68 def dsfr_select(attribute, choices, input_options: {}, **opts) @template.content_tag(:div, class: "fr-select-group") do @template.safe_join( [ dsfr_label_with_hint(attribute, opts), dsfr_select_tag(attribute, choices, **opts, **()), (attribute) ] ) end end |
#dsfr_select_tag(attribute, choices, opts) ⇒ Object
80 81 82 |
# File 'lib/dsfr-form_builder.rb', line 80 def dsfr_select_tag(attribute, choices, opts) select(attribute, choices, { include_blank: opts[:include_blank] }, class: "fr-select") end |
#dsfr_text_area(attribute, opts = {}) ⇒ Object
19 20 21 |
# File 'lib/dsfr-form_builder.rb', line 19 def dsfr_text_area(attribute, opts = {}) dsfr_input_field(attribute, :text_area, 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
27 28 29 |
# File 'lib/dsfr-form_builder.rb', line 27 def dsfr_url_field(attribute, opts = {}) dsfr_input_field(attribute, :url_field, opts) end |
#hint_tag(text) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/dsfr-form_builder.rb', line 144 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
158 159 160 161 162 163 164 165 166 |
# File 'lib/dsfr-form_builder.rb', line 158 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
154 155 156 |
# File 'lib/dsfr-form_builder.rb', line 154 def join_classes(arr) arr.compact.join(" ") end |
#label_value(attribute, opts) ⇒ Object
168 169 170 171 172 |
# File 'lib/dsfr-form_builder.rb', line 168 def label_value(attribute, opts) return opts[:label] if opts[:label] (@object.try(:object) || @object).class.human_attribute_name(attribute) end |
#required_tag ⇒ Object
130 131 132 |
# File 'lib/dsfr-form_builder.rb', line 130 def required_tag @template.content_tag(:span, "*", class: "fr-text-error") end |