Class: RailsI18nManager::CustomFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/rails_i18n_manager/custom_form_builder.rb

Constant Summary collapse

ALLOWED_OPTIONS =
[
  :value,
  :name,
  :label,
  :field_wrapper_html,
  :label_wrapper_html,
  :label_html,
  :input_wrapper_html,
  :input_html,
  :required,
  :required_text,
  :help_text,
  :errors,
  :field_layout,
  :view_mode,

  ### SELECT OPTIONS
  :collection,
  :selected,
  :disabled,
  :prompt,
  :include_blank,
].freeze

Instance Method Summary collapse

Instance Method Details

#error_notificationObject



28
29
30
# File 'app/helpers/rails_i18n_manager/custom_form_builder.rb', line 28

def error_notification
  @template.render "rails_i18n_manager/form_builder/error_notification", {f: self}
end

#field(method, type:, **options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/rails_i18n_manager/custom_form_builder.rb', line 36

def field(method, type:, **options)
  type = type.to_sym

  options = _transform_options(options, method)

  if options[:view_mode]
    return _view_field(method, type, options)
  end

  invalid_options = options.keys - ALLOWED_OPTIONS
  if invalid_options.any?

    raise "Invalid options provided: #{invalid_options.join(", ")}"
  end

  if [:select, :textarea].exclude?(type)
    options[:input_html][:type] = type.to_s
  end

  case type
  when :select
    options[:collection] = _fetch_required_option(:collection, options)

    options = _transform_select_options(options, method)
  when :checkbox
    options = _transform_checkbox_options(options, method)
  else
    if !options[:input_html].has_key?(:value)
      options[:input_html][:value] = object.send(method)
    end
  end

  @template.render("rails_i18n_manager/form_builder/basic_field", {
    f: self,
    method: method,
    type: type,
    options: options,
  })
end

#view_field(label:, value:, **options) ⇒ Object



32
33
34
# File 'app/helpers/rails_i18n_manager/custom_form_builder.rb', line 32

def view_field(label:, value:, **options)
  field(nil, type: :view, **options.merge(label: label, value: value, view_mode: true))
end