Class: Super::FormBuilder::Wrappers

Inherits:
Object
  • Object
show all
Defined in:
lib/super/form_builder.rb,
lib/super/form_builder/base_methods.rb,
lib/super/form_builder/option_methods.rb,
lib/super/form_builder/flatpickr_methods.rb,
lib/super/form_builder/action_text_methods.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, template) ⇒ Wrappers

Returns a new instance of Wrappers.



29
30
31
32
# File 'lib/super/form_builder.rb', line 29

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

Instance Method Details

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



27
28
29
# File 'lib/super/form_builder/base_methods.rb', line 27

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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/super/form_builder/base_methods.rb', line 31

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

#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/super/form_builder/option_methods.rb', line 50

def collection_check_boxes(attribute, collection, value_method, text_method, 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.collection_check_boxes(attribute, collection, value_method, text_method, options, html_options, &block)
end

#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/super/form_builder/option_methods.rb', line 61

def collection_radio_buttons(attribute, collection, value_method, text_method, 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.collection_radio_buttons(attribute, collection, value_method, text_method, options, html_options, &block)
end

#collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/super/form_builder/option_methods.rb', line 17

def collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {})
  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.collection_select(attribute, collection, value_method, text_method, options, html_options)
end

#container(&block) ⇒ Object



49
50
51
# File 'lib/super/form_builder.rb', line 49

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

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/super/form_builder/flatpickr_methods.rb', line 6

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

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/super/form_builder/flatpickr_methods.rb', line 27

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

#grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/super/form_builder/option_methods.rb', line 28

def grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  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.grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
end

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

def file_field!(attribute, label_text: nil, label: {}, field: {}, show_errors: true) end



53
54
55
# File 'lib/super/form_builder/base_methods.rb', line 53

def hidden_field(attribute, options = {})
  @builder.hidden_field(attribute, options)
end

#inline_errors(attribute) ⇒ Object



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

def inline_errors(attribute)
  if @builder.object
    messages = Form::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



20
21
22
23
24
25
# File 'lib/super/form_builder/base_methods.rb', line 20

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

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



6
7
8
9
10
11
# File 'lib/super/form_builder/action_text_methods.rb', line 6

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

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



6
7
8
9
10
11
12
13
# File 'lib/super/form_builder/option_methods.rb', line 6

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

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

def radio_button(attribute, tag_value, label_text: nil, label: {}, field: {}, show_errors: true) end



64
65
66
67
68
69
70
# File 'lib/super/form_builder/base_methods.rb', line 64

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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/super/form_builder/flatpickr_methods.rb', line 50

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_zone_select(attribute, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/super/form_builder/option_methods.rb', line 39

def time_zone_select(attribute, priority_zones = nil, options = {}, html_options = {})
  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.time_zone_select(attribute, priority_zones, options, html_options)
end