Class: Dsfr::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Dsfr::FormBuilder
- Includes:
- ActionView::Helpers::OutputSafetyHelper
- Defined in:
- lib/dsfr-form_builder.rb
Constant Summary collapse
- VERSION =
"0.0.14"
Instance Attribute Summary collapse
-
#display_required_tags ⇒ Object
Returns the value of attribute display_required_tags.
Instance Method Summary collapse
- #dsfr_button(value = nil, options = {}, &block) ⇒ Object
- #dsfr_check_box(attribute, opts = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #dsfr_collection_check_boxes(method, collection, value_method, text_method, opts = {}, html_options = {}) ⇒ Object
- #dsfr_error_message(attr) ⇒ Object
- #dsfr_file_field(attribute, opts = {}) ⇒ Object
- #dsfr_input_field(attribute, input_kind, opts = {}) ⇒ Object
- #dsfr_input_group(attribute, kind: :input, **opts) ⇒ Object
- #dsfr_label_with_hint(attribute, opts = {}) ⇒ Object
- #dsfr_radio_buttons(attribute, choices, legend: nil, hint: nil, **opts) ⇒ Object
- #dsfr_radio_option(attribute, value:, label_text:, hint:, checked:, rich: false, **opts) ⇒ Object
- #dsfr_select(attribute, choices, input_options = {}, **html_options) ⇒ Object
- #dsfr_submit(value = nil, options = {}, &block) ⇒ Object
- #hint_tag(text) ⇒ Object
-
#initialize(object_name, object, template, options) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #label_value(attribute, opts) ⇒ Object
- #required_tag ⇒ Object
Constructor Details
#initialize(object_name, object, template, options) ⇒ FormBuilder
Returns a new instance of FormBuilder.
11 12 13 14 |
# File 'lib/dsfr-form_builder.rb', line 11 def initialize(object_name, object, template, ) super self. = .fetch(:display_required_tags, false) end |
Instance Attribute Details
#display_required_tags ⇒ Object
Returns the value of attribute display_required_tags.
9 10 11 |
# File 'lib/dsfr-form_builder.rb', line 9 def @display_required_tags end |
Instance Method Details
#dsfr_button(value = nil, options = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/dsfr-form_builder.rb', line 16 def (value = nil, = {}, &block) if block_given? = value || {} value = @template.capture { yield(value) } end [:type] ||= :button [:class] = @template.class_names("fr-btn", [:class]) (value, ) end |
#dsfr_check_box(attribute, opts = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dsfr-form_builder.rb', line 72 def dsfr_check_box(attribute, opts = {}, checked_value = "1", unchecked_value = "0") @template.tag.div(class: @template.class_names("fr-fieldset__element", "fr-fieldset__element--inline" => opts.delete(:inline))) do dsfr_input_group(attribute, **opts, kind: :checkbox) do @template.safe_join([ check_box(attribute, opts.except(:label, :hint), checked_value, unchecked_value), dsfr_label_with_hint(attribute, opts) ]) end end end |
#dsfr_collection_check_boxes(method, collection, value_method, text_method, opts = {}, html_options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dsfr-form_builder.rb', line 83 def dsfr_collection_check_boxes(method, collection, value_method, text_method, opts = {}, = {}) legend = opts.delete(:legend) || @object&.class&.human_attribute_name(method) if legend.blank? raise ArgumentError.new("Please provide the legend option, or use an object whose class responds to :human_attribute_name") end legend = @template.safe_join([ legend, hint_tag(opts.delete(:hint)) ]) name = opts.delete(:name) || "#{@object_name}[#{method}][]" [:class] = @template.class_names("fr-fieldset", [:class]) @template.tag.fieldset(**) do @template.safe_join([ @template.tag.legend(legend, class: "fr-fieldset__legend--regular fr-fieldset__legend"), @template.hidden_field_tag(name, "", id: nil), collection.map do |item| value = item.send(value_method) = { name: name, value: value, id: field_id(method, value), label: item.send(text_method), inline: opts[:inline], checked: selected?(method, value), include_hidden: false } dsfr_check_box(method, , value, "") end ]) end end |
#dsfr_error_message(attr) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/dsfr-form_builder.rb', line 171 def (attr) return if @object.nil? || @object.errors[attr].none? @template.tag.p(class: "fr-messages-group") do safe_join(@object.errors.(attr).map do |msg| @template.tag.span(msg, class: "fr-message fr-message--error") end) end end |
#dsfr_file_field(attribute, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/dsfr-form_builder.rb', line 62 def dsfr_file_field(attribute, opts = {}) dsfr_input_group(attribute, **opts, kind: :upload) do @template.safe_join([ dsfr_label_with_hint(attribute, opts.except(:class, :value)), file_field(attribute, class: "fr-upload", **opts.except(:class, :hint, :label, :data)), (attribute) ]) end end |
#dsfr_input_field(attribute, input_kind, opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/dsfr-form_builder.rb', line 52 def dsfr_input_field(attribute, input_kind, opts = {}) dsfr_input_group(attribute, **opts.except(:data)) do @template.safe_join([ dsfr_label_with_hint(attribute, opts.except(:value)), public_send(input_kind, attribute, class: "fr-input", **opts.except(:class, :hint, :label)), (attribute) ]) end end |
#dsfr_input_group(attribute, kind: :input, **opts) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dsfr-form_builder.rb', line 41 def dsfr_input_group(attribute, kind: :input, **opts) @template.tag.div( data: opts[:data], class: @template.class_names( "fr-#{kind}-group", opts[:class], "fr-#{kind}-group--error" => @object&.errors&.include?(attribute) ) ) { yield } end |
#dsfr_label_with_hint(attribute, opts = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/dsfr-form_builder.rb', line 157 def dsfr_label_with_hint(attribute, opts = {}) label(attribute, class: @template.class_names("fr-label", opts[:class]), value: opts[:value]) do @template.safe_join([ opts[:label_text] || label_value(attribute, opts), (required_tag if opts[:required] && ), (hint_tag(opts[:hint]) if opts[:hint]) ]) end end |
#dsfr_radio_buttons(attribute, choices, legend: nil, hint: nil, **opts) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dsfr-form_builder.rb', line 124 def (attribute, choices, legend: nil, hint: nil, **opts) legend_content = @template.safe_join([ legend || @object.class.human_attribute_name(attribute), hint_tag(hint) ]) @template.tag.fieldset(class: "fr-fieldset") do @template.safe_join([ @template.tag.legend(legend_content, class: "fr-fieldset__legend--regular fr-fieldset__legend"), choices.map do |choice| dsfr_radio_option( attribute, value: choice[:value], label_text: choice[:label], hint: choice[:hint], checked: choice[:checked], **opts ) end ]) end end |
#dsfr_radio_option(attribute, value:, label_text:, hint:, checked:, rich: false, **opts) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dsfr-form_builder.rb', line 146 def dsfr_radio_option(attribute, value:, label_text:, hint:, checked:, rich: false, **opts) @template.tag.div(class: "fr-fieldset__element") do @template.tag.div(class: @template.class_names("fr-radio-group", "fr-radio-rich" => rich)) do @template.safe_join([ (attribute, value, checked:, **opts), dsfr_label_with_hint(attribute, opts.merge(label_text: label_text, hint: hint, value: value)) ]) end end end |
#dsfr_select(attribute, choices, input_options = {}, **html_options) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dsfr-form_builder.rb', line 112 def dsfr_select(attribute, choices, = {}, **) = .dup.except(:hint, :name) [:class] = @template.class_names("fr-select", [:class]) dsfr_input_group(attribute, **, kind: :select) do @template.safe_join([ dsfr_label_with_hint(attribute, ), select(attribute, choices, , **), (attribute) ]) end end |
#dsfr_submit(value = nil, options = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/dsfr-form_builder.rb', line 26 def dsfr_submit(value = nil, = {}, &block) if block_given? = value || {} value = @template.capture { yield(value) } end [:type] = :submit (value, ) end |
#hint_tag(text) ⇒ Object
181 182 183 |
# File 'lib/dsfr-form_builder.rb', line 181 def hint_tag(text) @template.tag.span(text, class: "fr-hint-text") if text end |
#label_value(attribute, opts) ⇒ Object
185 186 187 188 189 |
# File 'lib/dsfr-form_builder.rb', line 185 def label_value(attribute, opts) return opts[:label] if opts[:label] (@object.try(:object) || @object).class.human_attribute_name(attribute) end |
#required_tag ⇒ Object
167 168 169 |
# File 'lib/dsfr-form_builder.rb', line 167 def required_tag @template.tag.span("*", class: "fr-text-error") end |