Skip to content
This repository
Browse code

Code cleanup for ActionDispatch::Flash#call

The nested `if` was replaced by using `presence` which takes account for
the given hash when it is `nil` or when it is empty. The `else` was
removed because what it was doing was to assign to `env[KEY]` the
value it already had.
  • Loading branch information...
commit c43ca06ca091fc09e2c86bb051ac92b648f12b64 1 parent 2519fb0
Julian Vargas authored May 12, 2013
12  actionpack/lib/action_dispatch/middleware/flash.rb
@@ -243,15 +243,9 @@ def call(env)
243 243
       session    = Request::Session.find(env) || {}
244 244
       flash_hash = env[KEY]
245 245
 
246  
-      if flash_hash
247  
-        if !flash_hash.empty? || session.key?('flash')
248  
-          session["flash"] = flash_hash.to_session_value
249  
-          new_hash = flash_hash.dup
250  
-        else
251  
-          new_hash = flash_hash
252  
-        end
253  
-
254  
-        env[KEY] = new_hash
  246
+      if flash_hash.present? || session.key?('flash')
  247
+        session["flash"] = flash_hash.to_session_value
  248
+        env[KEY] = flash_hash.dup
255 249
       end
256 250
 
257 251
       if (!session.respond_to?(:loaded?) || session.loaded?) && # (reset_session uses {}, which doesn't implement #loaded?)

0 notes on commit c43ca06

Sam Ruby

I'm getting: undefined methodto_session_value' for nil:NilClass`

Full traceback:

/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/flash.rb:247:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/cookies.rb:486:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:36:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:366:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:418:in `_run__3834361723430503168__call__callbacks'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:80:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/reloader.rb:64:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:38:in `call_app'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:21:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:67:in `block in tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:25:in `tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:67:in `tagged'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:21:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/static.rb:64:in `call'
/home/rubys/git/rails/railties/lib/rails/engine.rb:511:in `call'
/home/rubys/git/rails/railties/lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/rubys/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/rubys/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/rubys/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Arun Agrawal

Here is the PR to fix this. #10592

Please sign in to comment.
Something went wrong with that request. Please try again.