Class: Super::Form::Builder::Wrappers

Inherits:
Object
  • Object
show all
Defined in:
lib/super/form/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, template) ⇒ Wrappers

Returns a new instance of Wrappers.



30
31
32
33
# File 'lib/super/form/builder.rb', line 30

def initialize(builder, template)
  @builder = builder
  @template = template
end

Instance Method Details

#check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



57
58
59
# File 'lib/super/form/builder.rb', line 57

def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
  @builder.check_box(attribute, options, checked_value, unchecked_value)
end

#check_box!(attribute, checked_value = "1", unchecked_value = "0", label: {}, field: {}, show_errors: true) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/super/form/builder.rb', line 174

def check_box!(attribute, checked_value = "1", unchecked_value = "0", label: {}, field: {}, show_errors: true)
  label[:super] ||= {}
  label[:super] = { class: "select-none ml-1" }.merge(label[:super])
  container do
    compact_join([
      "<div>".html_safe,
      public_send(:check_box, attribute, field, checked_value, unchecked_value),
      public_send(:label, attribute, nil, label),
      "</div>".html_safe,
      show_errors && inline_errors(attribute),
    ])
  end
end

#container(&block) ⇒ Object



170
171
172
# File 'lib/super/form/builder.rb', line 170

def container(&block)
  @template.(:div, class: "super-field-group", &block)
end

#flatpickr_date(attribute, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/super/form/builder.rb', line 61

def flatpickr_date(attribute, options = {})
  options, defaults = split_defaults(
    options,
    class: "super-input w-full",
    data: {
      controller: "flatpickr",
      flatpickr_options_value: {
        dateFormat: "Y-m-d",
      }
    }
  )
  options[:class] = join_classes(defaults[:class], options[:class])
  options[:data] = defaults[:data].deep_merge(options[:data] || {})

  @builder.text_field(attribute, options)
end

#flatpickr_date!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/super/form/builder.rb', line 188

def flatpickr_date!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:flatpickr_date, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#flatpickr_datetime(attribute, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/super/form/builder.rb', line 78

def flatpickr_datetime(attribute, options = {})
  options, defaults = split_defaults(
    options,
    class: "super-input w-full",
    data: {
      controller: "flatpickr",
      flatpickr_options_value: {
        enableSeconds: true,
        enableTime: true,
        dateFormat: "Z",
      }
    }
  )
  options[:class] = join_classes(defaults[:class], options[:class])
  options[:data] = defaults[:data].deep_merge(options[:data] || {})

  @builder.text_field(attribute, options)
end

#flatpickr_datetime!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/super/form/builder.rb', line 200

def flatpickr_datetime!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:flatpickr_datetime, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#flatpickr_time(attribute, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/super/form/builder.rb', line 97

def flatpickr_time(attribute, options = {})
  options, defaults = split_defaults(
    options,
    class: "super-input w-full",
    data: {
      controller: "flatpickr",
      flatpickr_options_value: {
        enableSeconds: true,
        enableTime: true,
        noCalendar: true,
        dateFormat: "H:i:S",
      }
    }
  )
  options[:class] = join_classes(defaults[:class], options[:class])
  options[:data] = defaults[:data].deep_merge(options[:data] || {})

  @builder.text_field(attribute, options)
end

#flatpickr_time!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/super/form/builder.rb', line 212

def flatpickr_time!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:flatpickr_time, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#inline_errors(attribute) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/super/form/builder.rb', line 35

def inline_errors(attribute)
  if @builder.object
    messages = InlineErrors.error_messages(@builder.object, attribute).map do |msg|
      (msg)
    end

    @template.safe_join(messages)
  else
    (<<~MSG.html_safe)
      This form doesn't have an object, so something is probably wrong.
      Maybe <code>accepts_nested_attributes_for</code> isn't set up?
    MSG
  end
end

#label(attribute, text = nil, options = {}, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/super/form/builder.rb', line 50

def label(attribute, text = nil, options = {}, &block)
  options, defaults = split_defaults(options, class: "block")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.label(attribute, text, options, &block)
end

#password_field(attribute, options = {}) ⇒ Object



117
118
119
120
121
122
# File 'lib/super/form/builder.rb', line 117

def password_field(attribute, options = {})
  options, defaults = split_defaults(options, class: "super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.password_field(attribute, options)
end

#password_field!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/super/form/builder.rb', line 224

def password_field!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:password_field, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#rich_text_area(attribute, options = {}) ⇒ Object



124
125
126
127
128
129
# File 'lib/super/form/builder.rb', line 124

def rich_text_area(attribute, options = {})
  options, defaults = split_defaults(options, class: "trix-content super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.rich_text_area(attribute, options)
end

#rich_text_area!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/super/form/builder.rb', line 236

def rich_text_area!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:rich_text_area, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#select(attribute, choices, options = {}, html_options = {}, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/super/form/builder.rb', line 131

def select(attribute, choices, options = {}, html_options = {}, &block)
  options, defaults = split_defaults(options, include_blank: true)
  options = defaults.merge(options)
  html_options, html_defaults = split_defaults(html_options, class: "super-input super-input-select-field")
  html_options[:class] = join_classes(html_defaults[:class], html_options[:class])

  parts = [
    %(<div class="super-input-select">).html_safe,
    @builder.select(attribute, choices, options, html_options, &block),
    <<~HTML.html_safe,
    @template.render("super/feather/chevron_down"),
    <<~HTML.html_safe,
        </span>
      </div>
    HTML
    %(</div>).html_safe,
  ]

  @template.safe_join(parts)
end

#select!(attribute, collection, label: {}, field: {}, show_errors: true) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/super/form/builder.rb', line 248

def select!(attribute, collection, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:select, attribute, collection, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end

#submit(value = nil, options = {}) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/super/form/builder.rb', line 155

def submit(value = nil, options = {})
  value, options = nil, value if value.is_a?(Hash)
  options, defaults = split_defaults(options, class: "super-button")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.submit(value, options)
end

#text_field(attribute, options = {}) ⇒ Object



163
164
165
166
167
168
# File 'lib/super/form/builder.rb', line 163

def text_field(attribute, options = {})
  options, defaults = split_defaults(options, class: "super-input w-full")
  options[:class] = join_classes(defaults[:class], options[:class])

  @builder.text_field(attribute, options)
end

#text_field!(attribute, label: {}, field: {}, show_errors: true) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/super/form/builder.rb', line 260

def text_field!(attribute, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label),
      %(<div class="mt-1">).html_safe,
      public_send(:text_field, attribute, field),
      show_errors && inline_errors(attribute),
      %(</div>).html_safe,
    ])
  end
end