Class: SimpleForm::Inputs::Base

Inherits:
Object
  • Object
show all
Extended by:
SimpleForm::I18nCache
Includes:
ActionView::Helpers::TranslationHelper, ERB::Util, Components::Errors, Components::HTML5, Components::Hints, Components::LabelInput, Components::Maxlength, Components::MinMax, Components::Pattern, Components::Placeholders, Components::Readonly, Helpers::Autofocus, Helpers::Disabled, Helpers::Readonly, Helpers::Required, Helpers::Validators
Defined in:
lib/simple_form/inputs/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleForm::I18nCache

get_i18n_cache, i18n_cache, reset_i18n_cache

Methods included from Components::Readonly

#readonly

Methods included from Components::Placeholders

#placeholder, #placeholder_text

Methods included from Components::Pattern

#pattern

Methods included from Components::MinMax

#min_max

Methods included from Components::Maxlength

#maxlength

Methods included from Components::LabelInput

#label_input

Methods included from Components::HTML5

#has_required?, #html5, #html5?

Methods included from Components::Hints

#has_hint?, #hint

Methods included from Components::Errors

#error, #full_error, #has_errors?

Methods included from Helpers::Validators

#has_validators?

Constructor Details

#initialize(builder, attribute_name, column, input_type, options = {}) ⇒ Base

Returns a new instance of Base



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/simple_form/inputs/base.rb', line 55

def initialize(builder, attribute_name, column, input_type, options = {})
  super

  options         = options.dup
  @builder        = builder
  @attribute_name = attribute_name
  @column         = column
  @input_type     = input_type
  @reflection     = options.delete(:reflection)
  @options        = options.reverse_merge!(self.class.default_options)
  @required       = calculate_required

  # Notice that html_options_for receives a reference to input_html_classes.
  # This means that classes added dynamically to input_html_classes will
  # still propagate to input_html_options.
  @html_classes = SimpleForm.additional_classes_for(:input) { additional_classes }

  @input_html_classes = @html_classes.dup
  if SimpleForm.input_class && !input_html_classes.empty?
    input_html_classes << SimpleForm.input_class
  end

  @input_html_options = html_options_for(:input, input_html_classes).tap do |o|
    o[:readonly]  = true if has_readonly?
    o[:disabled]  = true if has_disabled?
    o[:autofocus] = true if has_autofocus?
  end
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name



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

def attribute_name
  @attribute_name
end

#columnObject (readonly)

Returns the value of attribute column



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

def column
  @column
end

#html_classesObject (readonly)

Returns the value of attribute html_classes



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

def html_classes
  @html_classes
end

#input_html_classesObject (readonly)

Returns the value of attribute input_html_classes



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

def input_html_classes
  @input_html_classes
end

#input_html_optionsObject (readonly)

Returns the value of attribute input_html_options



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

def input_html_options
  @input_html_options
end

#input_typeObject (readonly)

Returns the value of attribute input_type



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

def input_type
  @input_type
end

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

#reflectionObject (readonly)

Returns the value of attribute reflection



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

def reflection
  @reflection
end

Class Method Details

.disable(*keys) ⇒ Object



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

def self.disable(*keys)
  options = self.default_options.dup
  keys.each { |key| options[key] = false }
  self.default_options = options
end

.enable(*keys) ⇒ Object



37
38
39
40
41
# File 'lib/simple_form/inputs/base.rb', line 37

def self.enable(*keys)
  options = self.default_options.dup
  keys.each { |key| options.delete(key) }
  self.default_options = options
end

Instance Method Details

#additional_classesObject



92
93
94
# File 'lib/simple_form/inputs/base.rb', line 92

def additional_classes
  @additional_classes ||= [input_type, required_class, readonly_class, disabled_class].compact
end

#input(wrapper_options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/simple_form/inputs/base.rb', line 84

def input(wrapper_options = nil)
  raise NotImplementedError
end

#input_classObject



96
97
98
# File 'lib/simple_form/inputs/base.rb', line 96

def input_class
  "#{lookup_model_names.join("_")}_#{reflection_or_attribute_name}"
end

#input_optionsObject



88
89
90
# File 'lib/simple_form/inputs/base.rb', line 88

def input_options
  options
end