Class FormTestHelper::FieldsHash
In: lib/form_test_helper.rb
Parent: HashWithIndifferentAccess

A hash of fields to allow infinite nesting of fields named like ‘person[address][street]’

Methods

[]   convert_value   method_missing   new   proxy   update  

Classes and Modules

Class FormTestHelper::FieldsHash::FieldNotFoundError

Public Class methods

Uses merge! instead of update when creating a new FieldsHash so update can update field values, not the field objects themselves.

[Source]

     # File lib/form_test_helper.rb, line 146
146:     def initialize(constructor = {})
147:       if constructor.is_a?(Hash)
148:         # super()
149:         merge!(constructor)
150:       else
151:         super(constructor)
152:       end
153:     end

Public Instance methods

[Source]

     # File lib/form_test_helper.rb, line 171
171:     def [](key)
172:       raise(FieldNotFoundError, "Field named '#{key.to_s}' not found in FieldsHash.") unless self.has_key?(key)
173:       super
174:     end

Ignore requests for a proxy

[Source]

     # File lib/form_test_helper.rb, line 156
156:     def proxy; self end

Allow field values to be merged in from a hash. Example:

  new_book = {
    :name => 'Pickaxe',
    :category => 'Programming',
    :classic => true,
  }
  form.book.update(new_book)

[Source]

     # File lib/form_test_helper.rb, line 166
166:     def update(other_hash)
167:       other_hash.each_pair { |key, value| self[key].update(value) }
168:       self
169:     end

Protected Instance methods

[Source]

     # File lib/form_test_helper.rb, line 177
177:     def convert_value(value)
178:       value.is_a?(Hash) ? FieldsHash.new(value) : value
179:     end

[Source]

     # File lib/form_test_helper.rb, line 181
181:     def method_missing(method, *args)
182:       method = method.to_s
183:       if method.gsub!(/=$/, '') && self.has_key?(method)
184:         self[method].value = *args
185:       else
186:         self[method].proxy
187:       end
188:     end

[Validate]