Module: Daisy::FormBuilderHelper

Defined in:
app/helpers/daisy/form_builder_helper.rb

Class Method Summary collapse

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, **options)
      # Get the object name from the form builder
      object_name = @object_name.to_s

      # Create a unique ID if not provided
      options[:id] ||= "#{object_name}_#{name}"

      # Set the name attribute
      options[:name] = "#{object_name}[#{name}]"

      # Pass the form builder's object to the component if it exists
      if @object && @object.respond_to?(name) && !options.key?(:checked)
        options[:checked] = @object.send(name)
      end

      # Render the checkbox component
      @template.daisy_checkbox(**options)
    end

    # Add the daisy_toggle method to FormBuilder
    def daisy_toggle(name, **options)
      # Get the object name from the form builder
      object_name = @object_name.to_s

      # Create a unique ID if not provided
      options[:id] ||= "#{object_name}_#{name}"

      # Set the name attribute
      options[:name] = "#{object_name}[#{name}]"

      # Pass the form builder's object to the component if it exists
      if @object && @object.respond_to?(name) && !options.key?(:checked)
        options[:checked] = @object.send(name)
      end

      # Render the toggle component
      @template.daisy_toggle(**options)
    end

    # Add the daisy_radio method to FormBuilder
    def daisy_radio(name, **options)
      # Get the object name from the form builder
      object_name = @object_name.to_s

      # Create a unique ID if not provided
      value = options[:value].to_s
      options[:id] ||= "#{object_name}_#{name}_#{value}"

      # Set the name attribute
      options[:name] = "#{object_name}[#{name}]"

      # Pass the form builder's object to the component if it exists
      if @object && @object.respond_to?(name) && !options.key?(:checked)
        options[:checked] = @object.send(name).to_s == value
      end

      # Render the radio button component
      @template.daisy_radio(**options)
    end

    # Add the daisy_label method to FormBuilder
    def daisy_label(name, text = nil, **options, &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
      options[:for] ||= "#{object_name}_#{name}"

      # Add a humanized title from the name if not provided
      options[:title] ||= text || name.to_s.humanize

      # Render the label component
      @template.daisy_label(**options, &block)
    end

    # Add the daisy_range method to FormBuilder
    def daisy_range(method, **options)
      render_daisy_component(Daisy::DataInput::RangeComponent, method, **options)
    end

    # Add the daisy_rating method to FormBuilder
    def daisy_rating(method, **options)
      render_daisy_component(Daisy::DataInput::RatingComponent, method, **options)
    end

    # Add the daisy_file_input method to FormBuilder
    def daisy_file_input(method, **options)
      render_daisy_component(Daisy::DataInput::FileInputComponent, method, **options)
    end

    # Add the daisy_text_input method to FormBuilder
    def daisy_text_input(method, **options)
      render_daisy_component(Daisy::DataInput::TextInputComponent, method, **options)
    end

    # Add the daisy_text_area method to FormBuilder
    def daisy_text_area(method, **options)
      render_daisy_component(Daisy::DataInput::TextAreaComponent, method, **options)
    end

    # Add the daisy_cally_input method to FormBuilder
    def daisy_cally_input(method, **options, &block)
      render_daisy_component(Daisy::DataInput::CallyInputComponent, method, **options, &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,
        options_css: options_css,
        options_html: 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: options,
        **args,
        &block
      )
    end

    private

    def render_daisy_component(component_class, method, **options, &block)
      # Get the object name from the form builder
      object_name = @object_name.to_s

      # Create a unique ID if not provided
      options[:id] ||= "#{object_name}_#{method}"

      # Set the name attribute
      options[:name] ||= "#{object_name}[#{method}]"

      # Pass the form builder's object to the component if it exists
      options[:value] ||= object.try(method)

      # Render the component
      @template.render(component_class.new(**options), &block)
    end
  end
end