Module: GenericFormFor::Inputs::Base

Extended by:
ActiveSupport::Autoload
Includes:
GenericFormFor::I18n, ErrorMessage, Hint, Label, Validations
Included in:
BooleanInput, EmailInput, FileInput, HiddenInput, NumberInput, PasswordInput, PhoneInput, RangeInput, SearchInput, SelectInput, StringInput, TextInput, UrlInput
Defined in:
lib/generic_form_for/inputs/base.rb,
lib/generic_form_for/inputs/base/hint.rb,
lib/generic_form_for/inputs/base/label.rb,
lib/generic_form_for/inputs/base/number.rb,
lib/generic_form_for/inputs/base/string.rb,
lib/generic_form_for/inputs/base/placeholder.rb,
lib/generic_form_for/inputs/base/validations.rb,
lib/generic_form_for/inputs/base/error_message.rb

Defined Under Namespace

Modules: ErrorMessage, Hint, Label, Number, Placeholder, String, Validations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorMessage

#error_html, #error_html_options, #errors, #errors?, #render_error?

Methods included from Label

#label_html, #label_html_options, #label_text, #render_label?, #requirement_text, #translated_label

Methods included from Validations

#decimals, #limit, #limits_from_validations, #maximal_number, #minimal_number, #required?, #required_from_validations?

Methods included from Hint

#hint_html, #hint_html_options, #hint_text, #render_hint?

Methods included from GenericFormFor::I18n

#translate, #translate_action, #translate_hint, #translate_label, #translate_legend, #translate_placeholder

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def builder
  @builder
end

#columnObject

Returns the value of attribute column.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def column
  @column
end

#methodObject

Returns the value of attribute method.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def method
  @method
end

#objectObject

Returns the value of attribute object.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def object
  @object
end

#object_nameObject

Returns the value of attribute object_name.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def object_name
  @object_name
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def options
  @options
end

#templateObject

Returns the value of attribute template.



16
17
18
# File 'lib/generic_form_for/inputs/base.rb', line 16

def template
  @template
end

Instance Method Details

#autofocus?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generic_form_for/inputs/base.rb', line 63

def autofocus?
  if options.key?(:autofocus)
    if options[:autofocus]
      template.autofocus = false
      true
    else
      false
    end
  else
    return false unless template.autofocus
    template.autofocus = false
    true
  end
end

#default_input_optionsObject



43
44
45
46
47
48
49
# File 'lib/generic_form_for/inputs/base.rb', line 43

def default_input_options
  {
    :autofocus => autofocus?,
    :required => required?,
    :class => input_html_class
  }
end

#form_builder_optionsObject



59
60
61
# File 'lib/generic_form_for/inputs/base.rb', line 59

def form_builder_options
  [:collection, :required, :label, :as, :hint, :wrapper_html, :label_html, :error_html, :class, :autofocus]
end

#initialize(builder, template, object, object_name, method, column, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/generic_form_for/inputs/base.rb', line 18

def initialize(builder, template, object, object_name, method, column, options)
  @builder = builder
  @template = template
  @object = object
  @object_name = object_name
  @method = method
  @column = column
  @options = options.dup
  @first_wrapper_class = "#{options[:as]}-input"
end

#input_html_classObject



51
52
53
# File 'lib/generic_form_for/inputs/base.rb', line 51

def input_html_class
  ([options[:as]] + [*options[:class]] + [('errors' if errors?)]).compact.join(" ").strip
end

#input_html_optionsObject



39
40
41
# File 'lib/generic_form_for/inputs/base.rb', line 39

def input_html_options
  default_input_options.merge(options[:html] || {}).merge(input_options)
end

#input_optionsObject



55
56
57
# File 'lib/generic_form_for/inputs/base.rb', line 55

def input_options
  options.except(*form_builder_options)
end

#merge_html_options(runtime_options, config_options) ⇒ Object



34
35
36
37
# File 'lib/generic_form_for/inputs/base.rb', line 34

def merge_html_options(runtime_options,config_options)
  runtime_options[:class] = [*runtime_options[:class], *config_options[:class]].compact.join(' ').strip
  config_options.merge(runtime_options)
end

#to_htmlObject



29
30
31
32
# File 'lib/generic_form_for/inputs/base.rb', line 29

def to_html
  instance_exec &builder.input_wrapper_proc
  ""
end

#wrap_in(*args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/generic_form_for/inputs/base.rb', line 6

def wrap_in(*args, &block)
  wrap_options = args.first.is_a?(Hash) ? args.pop : {}
  wrap_options[:class] = [@first_wrapper_class, *wrap_options[:class], *(options[:wrapper_html]||{})[:class]].join(' ').strip
  wrap_options.merge!((options.delete(:wrapper_html) || {}).except(:class))
  @first_wrapper_class = nil
  template.concat( template.(wrap_options.delete(:tag) || :div, wrap_options) do
    template.capture(&block) if block_given?
  end.html_safe)
end