Class: Anchor::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Anchor::FormBuilder
- Defined in:
- app/helpers/anchor/form_builder.rb
Constant Summary collapse
- DEFAULT_MAX_DATE =
"9999-12-31".freeze
Instance Method Summary collapse
- #autocomplete_field(attribute, options = {}) ⇒ Object
-
#check_box(attribute, options = {}, *args) ⇒ Object
The following methods are pending to be replaced with ViewComponents.
- #collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #date_field(attribute, options = {}) ⇒ Object
- #datetime_field(attribute, options = {}) ⇒ Object
- #email_field(attribute, options = {}) ⇒ Object
- #error_message_for(attribute, options = {}) ⇒ Object
- #fieldset(attribute, options = {}, &block) ⇒ Object
- #file_field(attribute, options = {}) ⇒ Object
- #hint(text) ⇒ Object
-
#initialize(object_name, object, template, options) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #label(attribute, text = nil, options = {}, &block) ⇒ Object
- #legend(attribute, _options = {}) ⇒ Object
- #number_field(attribute, options = {}) ⇒ Object
- #search_field(attribute, options = {}) ⇒ Object
- #select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object
- #text_area(attribute, options = {}) ⇒ Object
- #text_field(attribute, options = {}) ⇒ Object
- #typeahead_select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object
Constructor Details
#initialize(object_name, object, template, options) ⇒ FormBuilder
Returns a new instance of FormBuilder.
7 8 9 10 11 12 13 14 15 |
# File 'app/helpers/anchor/form_builder.rb', line 7 def initialize(object_name, object, template, , &) super( object_name, object, view_component?(template) ? template.helpers : template, , & ) end |
Instance Method Details
#autocomplete_field(attribute, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'app/helpers/anchor/form_builder.rb', line 17 def autocomplete_field(attribute, = {}) render AutocompleteComponent.new( form_builder: self, name: attribute, src: .delete(:src), input_options: .delete(:input_options) || {}, list_options: .delete(:list_options) || {} ) end |
#check_box(attribute, options = {}, *args) ⇒ Object
The following methods are pending to be replaced with ViewComponents.
183 184 185 186 187 188 189 190 191 |
# File 'app/helpers/anchor/form_builder.rb', line 183 def check_box(attribute, = {}, *args) super( attribute, .deep_merge( data: { testid: "checkbox-#{attribute.to_s.dasherize}" } ), *args ) end |
#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/helpers/anchor/form_builder.rb', line 57 def collection_check_boxes( attribute, collection, value_method, text_method, = {}, = {} ) render CheckBoxCollectionComponent.new( form_builder: self, attribute:, collection:, value_method:, text_method:, **, ** ) do |component| super( attribute, collection, value_method, text_method, , component..merge(), ) do |check_box| render component.check_box(check_box:) end end end |
#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/helpers/anchor/form_builder.rb', line 27 def ( attribute, collection, value_method, text_method, = {}, = {} ) render RadioButtonCollectionComponent.new( form_builder: self, attribute:, collection:, value_method:, text_method:, **, ** ) do |component| super( attribute, collection, value_method, text_method, , component..merge(), ) do |radio| render component.radio(radio:) end end end |
#date_field(attribute, options = {}) ⇒ Object
193 194 195 196 197 198 |
# File 'app/helpers/anchor/form_builder.rb', line 193 def date_field(attribute, = {}) super(attribute, .merge( class: Anchor::InputComponent::INPUT_CLASSES, max: DEFAULT_MAX_DATE )) end |
#datetime_field(attribute, options = {}) ⇒ Object
200 201 202 203 204 205 |
# File 'app/helpers/anchor/form_builder.rb', line 200 def datetime_field(attribute, = {}) super(attribute, .merge( class: Anchor::InputComponent::INPUT_CLASSES, max: DEFAULT_MAX_DATE )) end |
#email_field(attribute, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/helpers/anchor/form_builder.rb', line 87 def email_field(attribute, = {}) render InputComponent.new( form_builder: self, attribute:, type: :email, starting_icon: .delete(:starting_icon), ending_icon: .delete(:ending_icon) ) do |component| super(attribute, component..merge()) end end |
#error_message_for(attribute, options = {}) ⇒ Object
99 100 101 102 103 104 105 |
# File 'app/helpers/anchor/form_builder.rb', line 99 def (attribute, = {}) render ErrorMessageComponent.new( form_builder: self, attribute:, ** ) end |
#fieldset(attribute, options = {}, &block) ⇒ Object
229 230 231 232 233 234 |
# File 'app/helpers/anchor/form_builder.rb', line 229 def fieldset(attribute, = {}, &block) output = tag(:fieldset, , true) output.safe_concat(legend(attribute)) output.concat(capture(&block)) if block output.safe_concat("</fieldset>") end |
#file_field(attribute, options = {}) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/helpers/anchor/form_builder.rb', line 207 def file_field(attribute, = {}) content_tag :div, class: "mt-2" do content_tag :label, class: "btn" do concat I18n.t("actions.attach") concat super( attribute, { class: "hidden", accept: "application/pdf", direct_upload: true, }.merge() ) end end end |
#hint(text) ⇒ Object
223 224 225 226 227 |
# File 'app/helpers/anchor/form_builder.rb', line 223 def hint(text) content_tag :p, class: "mt-2 text-secondary text-sm" do text end end |
#label(attribute, text = nil, options = {}, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/helpers/anchor/form_builder.rb', line 107 def label(attribute, text = nil, = {}, &block) render LabelComponent.new( form_builder: self, attribute:, text:, ** ) do |component| if block.present? super else super(attribute, text, component..merge()) do |builder| capture do concat text || builder.translation concat component.optional_suffix end end end end end |
#legend(attribute, _options = {}) ⇒ Object
236 237 238 239 240 |
# File 'app/helpers/anchor/form_builder.rb', line 236 def legend(attribute, = {}) content_tag :legend, class: %w(font-semibold text-base text-primary) do i18n_label attribute end end |
#number_field(attribute, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/helpers/anchor/form_builder.rb', line 127 def number_field(attribute, = {}) render InputComponent.new( form_builder: self, attribute:, type: :number, starting_icon: .delete(:starting_icon), ending_icon: .delete(:ending_icon) ) do |component| super(attribute, component..merge()) end end |
#search_field(attribute, options = {}) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/helpers/anchor/form_builder.rb', line 139 def search_field(attribute, = {}) render InputComponent.new( form_builder: self, attribute:, type: :search, starting_icon: .delete(:starting_icon), ending_icon: .delete(:ending_icon) ) do |component| super(attribute, component..merge()) end end |
#select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/helpers/anchor/form_builder.rb', line 151 def select(attribute, choices = nil, = {}, = {}, &) render SelectComponent.new( form_builder: self, attribute:, choices:, show_marker: .fetch(:show_marker, true), & ) do |component| super( attribute, choices, , component..deep_merge(), & ) end end |
#text_area(attribute, options = {}) ⇒ Object
261 262 263 264 265 266 267 268 269 |
# File 'app/helpers/anchor/form_builder.rb', line 261 def text_area(attribute, = {}) super( attribute, .merge( class: Anchor::InputComponent::INPUT_CLASSES + ["h-[120px]"], data: { testid: "text-area-#{attribute.to_s.dasherize}" } ) ) end |
#text_field(attribute, options = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/helpers/anchor/form_builder.rb', line 169 def text_field(attribute, = {}) render InputComponent.new( form_builder: self, attribute:, type: :text, starting_icon: .delete(:starting_icon), ending_icon: .delete(:ending_icon) ) do |component| super(attribute, component..merge()) end end |
#typeahead_select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/anchor/form_builder.rb', line 242 def typeahead_select( attribute, choices = nil, = {}, = {}, & ) = .deep_merge(data: { controller: "typeahead-select" }) select( attribute, choices, .merge(show_marker: false), , & ) end |