Class: Effective::FormInput

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/form_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, object_name, template, method, opts, html_opts = {}) ⇒ FormInput

Returns a new instance of FormInput.



4
5
6
7
8
9
10
11
12
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
# File 'app/models/effective/form_input.rb', line 4

def initialize(object, object_name, template, method, opts, html_opts = {})
  @object = object
  @object_name = object_name
  @template = template
  @method = method

  # Initialize 3 options Hashes:  @opts, @html_opts, and @js_opts
  @js_opts = opts.delete(:input_js).presence || {}
  @html_opts = html_opts.presence || opts.delete(:input_html) || {}
  @html_opts.delete(:placeholder) if @html_opts[:placeholder] == nil # SimpleForm oddity

  @opts = opts.presence || {}

  # Copy the following keys from options to html_options
  # To deal with SimpleForm being simple
  [:class, :readonly, :pattern, :disabled, :maxlength].each do |key|
    if !html_opts.key?(key) && @opts.key?(key)
      @html_opts[key] = @opts[:key] if @opts[:key]
    end
  end

  @js_opts = @js_opts.with_indifferent_access
  @html_opts = @html_opts.with_indifferent_access

  # Reverse merge in the defaults, so the current values take precedence over defaults
  @js_opts.reverse_merge!((default_input_js || {}))
  @html_opts.reverse_merge!((default_input_html || {}).except(:class))
  @opts.reverse_merge!((default_options || {}))

  # Take special procedure to ensure that @html_opts[:class] is an Array, and the proper merged values
  @html_opts[:class] = (arrayize_html_class_key(@html_opts[:class]) + arrayize_html_class_key(default_input_html)).compact.uniq

  # Set the value to avoid options craziness
  @value = (@opts.delete(:value) || @html_opts.delete(:value) || (@object.send(@method) if @object.respond_to?(@method)))
  @value = @value.to_a if @value.kind_of?(ActiveRecord::Relation)
end

Instance Method Details

#field_nameObject



41
42
43
# File 'app/models/effective/form_input.rb', line 41

def field_name
  "#{@object_name}[#{@method}]"
end

#html_optionsObject

All :input_html => options, merged with defaults. Could be everything (same as options) if coming from FormBuilder.



52
# File 'app/models/effective/form_input.rb', line 52

def html_options; @html_opts end

#js_optionsObject

All :input_js => options, merged with defaults



53
# File 'app/models/effective/form_input.rb', line 53

def js_options; @js_opts end

#optionsObject

Override these methods to change any options around tag_options reads html_options and js_options



51
# File 'app/models/effective/form_input.rb', line 51

def options; @opts end

#valueObject



45
46
47
# File 'app/models/effective/form_input.rb', line 45

def value
  @value
end