“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

petty annoyance of alias_method_chain

So it appears you can’t alias_method_chain []=. Rightly so you’d end up trying to do something like

def []=_with_rescue
  []=_without_rescue
end
alias_method_chain :[]=, :rescue

The resulting method names are of course invalid. So you have to do it longhand

require 'action_dispatch/middleware/cookies'
# action_dispatch/middleware/cookies.rb
module ActionDispatch
  class Cookies
    class SignedCookieJar
      def assign_with_overflow_rescue(name, options)
        assign_without_overflow_rescue(name, options)
      rescue ActionDispatch::Cookies::CookieOverflow => e
        SiteMailer.delay.error(e, key: key, original_options: original_options, encoded_size: options[:value].size)
      end
      alias_method :assign_without_overflow_rescue, :[]=
      alias_method :[]=, :assign_with_overflow_rescue
    end
  end
end