Class: Frontpack::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
app/helpers/frontpack/form_builder.rb

Overview

Custom input builders

Instance Method Summary collapse

Methods included from ActionView::Helpers::UrlHelper

#button_to

Instance Method Details

#autocomplete_field(method, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'app/helpers/frontpack/form_builder.rb', line 23

def autocomplete_field(method, options = {})
  relation = @object.class.reflect_on_association(method)
  uri = relation.class_name.underscore.sub('::', '/')
  display_field = relation.klass.autocomplete_display_fields[1]
  text_field(relation.foreign_key, options.merge('data-url': "/autocomplete/#{uri}", is: 'auto-complete', 'display-value': @object.send(method)&.send(display_field)))
end

#date_field(method, options = {}, _html_options = {}) ⇒ Object



66
67
68
# File 'app/helpers/frontpack/form_builder.rb', line 66

def date_field(method, options = {}, _html_options = {})
  @template.text_field(@object_name, method, objectify_options(options.merge(type: :date)))
end

#email_field(method, options = {}) ⇒ Object



40
41
42
43
# File 'app/helpers/frontpack/form_builder.rb', line 40

def email_field(method, options = {})
  set_options(method, options) if @object
  super
end

#enum_field(method, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/frontpack/form_builder.rb', line 9

def enum_field(method, options = {})
  excluded_keys = options.key?(:except) ? options[:except] : []

  if @object
    opts = { selected: @object.send(method) }
    select method, @object.enum_options(method, excluded_keys), opts, options
  elsif options.key?(:model)
    model = options[:model]
    select method, model.enum_options(method, excluded_keys), options.slice(:selected, :include_blank, :prompt), options
  else
    select method, [], opts, options
  end
end

#label(method, text = nil, options = {}) ⇒ Object



50
51
52
53
# File 'app/helpers/frontpack/form_builder.rb', line 50

def label(method, text = nil, options = {}, &)
  set_label_options method, options if @object
  super(method, text, options, &)
end

#number_field(method, options = {}) ⇒ Object



60
61
62
63
64
# File 'app/helpers/frontpack/form_builder.rb', line 60

def number_field(method, options = {})
  set_options(method, options) if @object

  super(method, options)
end

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



35
36
37
38
# File 'app/helpers/frontpack/form_builder.rb', line 35

def password_field(method, options = {})
  set_options(method, options) if @object
  super
end

#select(method, choices = nil, options = {}, html_options = {}) ⇒ Object



55
56
57
58
# File 'app/helpers/frontpack/form_builder.rb', line 55

def select(method, choices = nil, options = {}, html_options = {}, &)
  set_options(method, html_options, css_class: Frontpack::FormBuilderOptions.select_class) if @object
  super
end

#switch_field(method, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'app/helpers/frontpack/form_builder.rb', line 70

def switch_field(method, options = {})
  @template.(:div, class: 'switch-toggle') do
    toggle_box = check_box(method, { checked: @object.send(method) }.merge(options))
    opts = options.merge(for: "#{@object_name}_#{method}")
    opts[:style] = "--on-icon: '#{options[:'data-on-icon']}';" if options.key? :'data-on-icon'
    opts[:style] += "--off-icon: '#{options[:'data-off-icon']}';" if options.key? :'data-off-icon'
    toggle_box + @template.(:label, nil, opts)
  end
end

#text_area(method, options = {}) ⇒ Object



45
46
47
48
# File 'app/helpers/frontpack/form_builder.rb', line 45

def text_area(method, options = {})
  set_options(method, options) if @object
  super
end

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



30
31
32
33
# File 'app/helpers/frontpack/form_builder.rb', line 30

def text_field(method, options = {})
  set_options(method, options) if @object
  super(method, options)
end