Skip to content

Commit

Permalink
cleanup by using rack provided methods
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Oct 28, 2015
1 parent c04cfd3 commit 63e4780
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions lib/capybara/rack_test/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def path; @empty_file.path; end
end

def params(button)
params = {}
params = make_params

form_element_types=[:input, :select, :textarea]
form_elements_xpath=XPath.generate do |x|
Expand Down Expand Up @@ -67,7 +67,8 @@ def params(button)
end
end
merge_param!(params, button[:name], button[:value] || "") if button[:name]
params

params.to_params_hash
end

def submit(button)
Expand All @@ -82,43 +83,29 @@ def multipart?

private

class ParamsHash < Hash
def to_params_hash
self
end
end

def method
self[:method] =~ /post/i ? :post : :get
end

def merge_param!(params, key, value)
normalize_params(params, key, value)
if Rack::Utils.respond_to?(:default_query_parser)
Rack::Utils.default_query_parser.normalize_params(params, key, value, Rack::Utils.param_depth_limit)
else
Rack::Utils.normalize_params(params, key, value)
end
end

def normalize_params(params, name, v = nil)
# This code is copied from Rack - Rack 2 removed this from Rack::Util and replaced it with
# pluggable query parsers
name =~ %r([\[\]]*([^\[\]]+)\]*)
k = $1 || ''
after = $' || ''

return if k.empty?

if after == ""
params[k] = v
elsif after == "[]"
params[k] ||= []
raise TypeError unless params[k].is_a?(Array)
params[k] << v
elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
child_key = $1
params[k] ||= []
raise TypeError unless params[k].is_a?(Array)
if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
normalize_params(params[k].last, child_key, v)
else
params[k] << normalize_params({}, child_key, v)
end
def make_params
if Rack::Utils.respond_to?(:default_query_parser)
Rack::Utils.default_query_parser.make_params
else
params[k] ||= {}
params[k] = normalize_params(params[k], after, v)
ParamsHash.new
end

return params
end
end

0 comments on commit 63e4780

Please sign in to comment.