Skip to content

Commit

Permalink
Simplify renderer normalize keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Mar 20, 2020
1 parent 5df9b45 commit 644651d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions actionpack/lib/action_controller/renderer.rb
Expand Up @@ -103,8 +103,13 @@ def render(*args)
private
def normalize_keys(defaults, env)
new_env = {}
defaults.each_pair { |k, v| new_env[rack_key_for(k)] = rack_value_for(k, v) }
env.each_pair { |k, v| new_env[rack_key_for(k)] = rack_value_for(k, v) }

defaults.each_pair do |k, v|
key = rack_key_for(k)
new_env[key] = rack_value_for(k, v) unless new_env.key?(key)
end

new_env["rack.url_scheme"] = new_env["HTTPS"] == "on" ? "https" : "http"
new_env
end
Expand All @@ -117,19 +122,19 @@ def normalize_keys(defaults, env)
input: "rack.input"
}

IDENTITY = ->(_) { _ }

RACK_VALUE_TRANSLATION = {
https: ->(v) { v ? "on" : "off" },
method: ->(v) { -v.upcase },
}

def rack_key_for(key)
RACK_KEY_TRANSLATION[key] || key.to_s
end

def rack_value_for(key, value)
RACK_VALUE_TRANSLATION.fetch(key, IDENTITY).call value
case key
when :https
value ? "on" : "off"
when :method
-value.upcase
else
value
end
end
end
end

0 comments on commit 644651d

Please sign in to comment.