Cookie Memoization

Whilst trying to access cookie values that had been set in the current request in the view, I discovered that a new CookieJar is instantiated with every call to ActionController::Base#cookies.

Obviously in standard HTTP requests cookies can only be retrieved in subsequent requests to the one they were set in. However the code for CookieJar seems to suggest that the intention was to be able to access the cookies immediately after they had been set, as CookieJar is a sub-class of Hash.

cookies.rb: http://github.com/rails/rails/blob/7f1f16c01fd42701747daf9f3399dcdd300125f4/actionpack/lib/action_controller/cookies.rb#L72-82

Because a new CookieJar is instantiated with each access, any changes are discarded.

What do people think the intended behavior is? We could easily keep the changes to the cookie jar, by only instantiating one CookieJar per request. The following code in my ApplicationController, changes the behavior to the later: http://gist.github.com/107595

Cheers, Olly

OK, I think this change makes sense so I wiped up a patch.

+1s, improvements and objections appreciated:
https://rails.lighthouseapp.com/projects/8994/tickets/2733-patch-memoize-cookies-so-they-can-be-retrieved-in-the-same-response-request-cycle