From 3042b71a7a2b6dfcdd0e3282b834003099cd036e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 11 Aug 2015 18:42:52 -0700 Subject: make `constraints` more functional I don't want to rely on mutating ivars. This gives me more freedom when refactoring --- actionpack/lib/action_dispatch/routing/mapper.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index a12f60e20d..337d5e9404 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -99,7 +99,8 @@ module ActionDispatch options = normalize_options!(options, formatted, path_params, ast, modyoule) - constraints = scope_constraints.merge constraints(options, path_params) + split_options = constraints(options, path_params) + constraints = scope_constraints.merge Hash[split_options[:constraints] || []] if options_constraints.is_a?(Hash) options_constraints.each do |key, default| @@ -117,6 +118,7 @@ module ActionDispatch normalize_format!(formatted) + @conditions[:required_defaults] = (split_options[:required_defaults] || []).map(&:first) @conditions[:path_info] = path @conditions[:parsed_path_info] = ast @@ -306,17 +308,17 @@ module ActionDispatch end def constraints(options, path_params) - constraints = {} - required_defaults = [] - options.each_pair do |key, option| + options.group_by do |key, option| if Regexp === option - constraints[key] = option + :constraints else - required_defaults << key unless path_params.include?(key) + if path_params.include?(key) + :path_params + else + :required_defaults + end end end - @conditions[:required_defaults] = required_defaults - constraints end def path_params(ast) -- cgit v1.2.3