Class: DefaultForm::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Builder::Helper
Defined in:
lib/default_form/form_builder.rb

Constant Summary

Constants included from Builder::Helper

Builder::Helper::INPUT_FIELDS

Constants included from Builder::Default

Builder::Default::VALIDATIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder::Helper

#check_box, #collection_check_boxes, #collection_radio_buttons, #collection_select, #date_field, #fields, #hidden_field, #label, #number_field, #radio_button, #select, #submit, #text_area, #time_select, #time_zone_select, #wrap_all_with, #wrap_with

Methods included from Builder::Default

#default_help, #default_label, #default_options, #default_step, #default_value, #default_without_method

Methods included from Builder::Wrap

#help_tag, #object_has_errors?, #offset, #wrapping, #wrapping_all

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder.



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
# File 'lib/default_form/form_builder.rb', line 13

def initialize(object_name, object, template, options)
  if options.key?(:theme)
    @theme = options[:theme].to_s
  else
    @theme = 'default'
  end
  set_file = Rails.root.join('config/default_form.yml').existence || RailsCom::Engine.root.join('config/default_form.yml')
  set = YAML.load_file set_file
  settings = set.fetch(theme, {})
  settings.deep_symbolize_keys!

  options[:method] = settings[:method] if !options.key?(:method) && settings.key?(:method)
  options[:local] = true # todo rails 6.2 will remove this
  options[:data] ||= {}
  if options[:data][:controller].present?
    options[:data][:controller] += ' default_valid'
  else
    options[:data][:controller] = 'default_valid'
  end

  @origin_css = settings.fetch(:origin, {}).merge options.fetch(:origin, {})
  @wrap_css = settings.fetch(:wrap, {}).merge options.fetch(:wrap, {})
  @error_css = settings.fetch(:error, {}).merge options.fetch(:error, {})
  @offset_css = settings.fetch(:offset, {}).merge options.fetch(:offset, {})
  @on_options = settings.extract! :autocomplete, :autofilter, :placeholder, :label
  @on_options.merge! options.slice(:placeholder, :label, :autocomplete, :autofilter)
  @params = template.params

  _values = Hash(params.permit(object_name => {})[object_name])
  if object.is_a?(ActiveRecord::Base)
    object.assign_attributes _values.slice(*object.attribute_names)
  end

  if options[:class].to_s.start_with?('new_', 'edit_')
    options[:class] = origin_css[:form]
  end
  options[:class] = origin_css[:form] unless options.key?(:class)

  super
end

Instance Attribute Details

#error_cssObject (readonly)

Returns the value of attribute error_css.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def error_css
  @error_css
end

#offset_cssObject (readonly)

Returns the value of attribute offset_css.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def offset_css
  @offset_css
end

#on_optionsObject (readonly)

Returns the value of attribute on_options.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def on_options
  @on_options
end

#origin_cssObject (readonly)

Returns the value of attribute origin_css.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def origin_css
  @origin_css
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def params
  @params
end

#themeObject (readonly)

Returns the value of attribute theme.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def theme
  @theme
end

#wrap_cssObject (readonly)

Returns the value of attribute wrap_css.



10
11
12
# File 'lib/default_form/form_builder.rb', line 10

def wrap_css
  @wrap_css
end

Instance Method Details

#submit_default_valueObject



54
55
56
57
58
# File 'lib/default_form/form_builder.rb', line 54

def submit_default_value
  I18n.t "helpers.submit.#{theme}", raise: true
rescue
  super
end