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_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/super/form/builder.rb', line 165

def check_box!(attribute, checked_value: "1", unchecked_value: "0", label_text: nil, 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, label_text, label),
      "</div>".html_safe,
      show_errors && inline_errors(attribute),
    ])
  end
end

#container(&block) ⇒ Object



161
162
163
# File 'lib/super/form/builder.rb', line 161

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

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



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

def date_flatpickr(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] || {})
  options[:value] = @builder.object.public_send(attribute).presence
  options[:value] = options[:value].iso8601 if options[:value].respond_to?(:iso8601)

  @builder.text_field(attribute, options)
end

#date_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/super/form/builder.rb', line 179

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

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



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

def datetime_flatpickr(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] || {})
  options[:value] = @builder.object.public_send(attribute).presence
  options[:value] = options[:value].iso8601 if options[:value].respond_to?(:iso8601)

  @builder.text_field(attribute, options)
end

#datetime_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/super/form/builder.rb', line 191

def datetime_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label_text, label),
      %(<div class="mt-1">).html_safe,
      public_send(:datetime_flatpickr, 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



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

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_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/super/form/builder.rb', line 215

def password_field!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label_text, 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



130
131
132
133
134
135
# File 'lib/super/form/builder.rb', line 130

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_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/super/form/builder.rb', line 227

def rich_text_area!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label_text, 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



137
138
139
140
141
142
143
144
# File 'lib/super/form/builder.rb', line 137

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")
  html_options[:class] = join_classes(html_defaults[:class], html_options[:class])

  @builder.select(attribute, choices, options, html_options, &block)
end

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



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/super/form/builder.rb', line 239

def select!(attribute, collection, label_text: nil, label: {}, field: {}, show_errors: true)
  container do
    compact_join([
      public_send(:label, attribute, label_text, 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



146
147
148
149
150
151
152
# File 'lib/super/form/builder.rb', line 146

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



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

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_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/super/form/builder.rb', line 251

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

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/super/form/builder.rb', line 101

def time_flatpickr(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] || {})
  options[:value] = @builder.object.public_send(attribute).presence
  options[:value] = options[:value].strftime("%H:%M:%S") if options[:value].respond_to?(:strftime)

  @builder.text_field(attribute, options)
end

#time_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/super/form/builder.rb', line 203

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