-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
Copy pathroute_params.rb
62 lines (52 loc) · 1.37 KB
/
route_params.rb
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Shoulda
module Matchers
module ActionController
# @private
class RouteParams
PARAMS_TO_SYMBOLIZE = %i{ format }
def initialize(args)
@args = args
end
def normalize
if controller_and_action_given_as_string?
extract_params_from_string
else
stringify_params
end
end
protected
attr_reader :args
def controller_and_action_given_as_string?
args[0].is_a?(String)
end
def extract_params_from_string
controller, action = args[0].split('#')
params = (args[1] || {}).merge(controller: controller, action: action)
normalize_values(params)
end
def stringify_params
normalize_values(args[0])
end
def normalize_values(hash)
hash.each_with_object({}) do |(key, value), hash_copy|
hash_copy[key] = symbolize_or_stringify(key, value)
end
end
def symbolize_or_stringify(key, value)
if key.in?(PARAMS_TO_SYMBOLIZE)
value.to_sym
else
stringify(value)
end
end
def stringify(value)
if value.is_a?(Array)
value.map(&:to_param)
else
value.to_param
end
end
end
end
end
end