Module: Daisy::FormBuilderHelper
- Defined in:
- app/helpers/daisy/form_builder_helper.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
Extends ActionView::Helpers::FormBuilder with Daisy UI component methods.
Class Method Details
.included(base) ⇒ Object
Extends ActionView::Helpers::FormBuilder with Daisy UI component methods
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 56 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 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/helpers/daisy/form_builder_helper.rb', line 6 def self.included(base) base.class_eval do # Add the daisy_checkbox method to FormBuilder def daisy_checkbox(name, **) # Get the object name from the form builder object_name = @object_name.to_s # Create a unique ID if not provided [:id] ||= "#{object_name}_#{name}" # Set the name attribute [:name] = "#{object_name}[#{name}]" # Pass the form builder's object to the component if it exists if @object && @object.respond_to?(name) && !.key?(:checked) [:checked] = @object.send(name) end # Render the checkbox component @template.daisy_checkbox(**) end # Add the daisy_toggle method to FormBuilder def daisy_toggle(name, **) # Get the object name from the form builder object_name = @object_name.to_s # Create a unique ID if not provided [:id] ||= "#{object_name}_#{name}" # Set the name attribute [:name] = "#{object_name}[#{name}]" # Pass the form builder's object to the component if it exists if @object && @object.respond_to?(name) && !.key?(:checked) [:checked] = @object.send(name) end # Render the toggle component @template.daisy_toggle(**) end # Add the daisy_radio method to FormBuilder def daisy_radio(name, **) # Get the object name from the form builder object_name = @object_name.to_s # Create a unique ID if not provided value = [:value].to_s [:id] ||= "#{object_name}_#{name}_#{value}" # Set the name attribute [:name] = "#{object_name}[#{name}]" # Pass the form builder's object to the component if it exists if @object && @object.respond_to?(name) && !.key?(:checked) [:checked] = @object.send(name).to_s == value end # Render the radio button component @template.daisy_radio(**) end # Add the daisy_label method to FormBuilder def daisy_label(name, text = nil, **, &block) # Get the object name from the form builder object_name = @object_name.to_s # Create a for_id based on the name if not provided [:for] ||= "#{object_name}_#{name}" # Add a humanized title from the name if not provided [:title] ||= text || name.to_s.humanize # Render the label component @template.daisy_label(**, &block) end # Add the daisy_range method to FormBuilder def daisy_range(method, **) render_daisy_component(Daisy::DataInput::RangeComponent, method, **) end # Add the daisy_rating method to FormBuilder def (method, **) render_daisy_component(Daisy::DataInput::RatingComponent, method, **) end # Add the daisy_file_input method to FormBuilder def daisy_file_input(method, **) render_daisy_component(Daisy::DataInput::FileInputComponent, method, **) end # Add the daisy_text_input method to FormBuilder def daisy_text_input(method, **) render_daisy_component(Daisy::DataInput::TextInputComponent, method, **) end # Add the daisy_text_area method to FormBuilder def daisy_text_area(method, **) render_daisy_component(Daisy::DataInput::TextAreaComponent, method, **) end # Add the daisy_cally_input method to FormBuilder def daisy_cally_input(method, **, &block) render_daisy_component(Daisy::DataInput::CallyInputComponent, method, **, &block) end # Add the daisy_select method to FormBuilder def daisy_select(method, options: nil, option_groups: nil, placeholder: nil, options_css: nil, options_html: {}, **args, &block) # Extract the name from the form builder's object_name and method name = "#{object_name}[#{method}]" # Get the current value from the object value = object.try(method) # Generate a default ID if not provided id = args[:id] || "#{object_name}_#{method}" # Build the component with the extracted form values and any additional options @template.daisy_select( name: name, id: id, value: value, options: , options_css: , options_html: , placeholder: placeholder, **args, &block ) end # Add the daisy_filter method to FormBuilder def daisy_filter(method, options: nil, **args, &block) # Extract the name from the form builder's object_name and method name = "#{object_name}[#{method}]" # Get the current value from the object value = object.try(method) # Generate a default ID if not provided id = args[:id] || "#{object_name}_#{method}" # Build the component with the extracted form values and any additional options @template.daisy_filter( name: name, id: id, value: value, options: , **args, &block ) end private def render_daisy_component(component_class, method, **, &block) # Get the object name from the form builder object_name = @object_name.to_s # Create a unique ID if not provided [:id] ||= "#{object_name}_#{method}" # Set the name attribute [:name] ||= "#{object_name}[#{method}]" # Pass the form builder's object to the component if it exists [:value] ||= object.try(method) # Render the component @template.render(component_class.new(**), &block) end end end |