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 169
169:     def initialize(constructor = {})
170:       if constructor.is_a?(Hash)
171:         # super()
172:         merge!(constructor)
173:       else
174:         super(constructor)
175:       end
176:     end

Public Instance methods

[Source]

     # File lib/form_test_helper.rb, line 194
194:     def [](key)
195:       unless self.has_key?(key)
196:         raise(FieldNotFoundError, "Field named '#{key.to_s}' not found in FieldsHash.") 
197:       end
198:       super
199:     end

Allows setting form field values using key access to form fields: Examples:

  form = select_form
  form.user['name'] = 'joe'

  submit_form do |form|
    form.user['name'] = 'joe'
  end

[Source]

     # File lib/form_test_helper.rb, line 210
210:     def []=(key, value)
211:       self[key].value = value
212:     end

Ignore requests for a proxy

[Source]

     # File lib/form_test_helper.rb, line 179
179:     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 189
189:     def update(other_hash)
190:       other_hash.each_pair { |key, value| self[key].update(value) }
191:       self
192:     end

Protected Instance methods

[Source]

     # File lib/form_test_helper.rb, line 217
217:     def convert_value(value)
218:       value.is_a?(Hash) ? FieldsHash.new(value) : value
219:     end

[Source]

     # File lib/form_test_helper.rb, line 221
221:     def method_missing(method, *args)
222:       method = method.to_s
223:       if method.gsub!(/=$/, '') && self.has_key?(method)
224:         self[method].value = *args
225:       else
226:         self[method].proxy
227:       end
228:     end

[Validate]