Alias_method: stack level is too deep

I am trying to decorate a controller from another rail engine. I have one controller method that I want to extend with just one more line. I prefer not to duplicate the entire original controller method.

This is what I tried:

  Backend::BaseContentsController.class_eval do
    def booking_update
      # do some stuff
      update
    end
    alias_method :update, :booking_update
  end

Unfortunately, this throws an exception stack level too deep. Usually with inheritance, I could just call super. What would be ideal in my case?

+4
source share
3 answers

You should try alias_method_chain:

def update_with_booking
  # do some stuff
  update_without_booking # that your old update
end

alias_method_chain :update, :booking
+6
source

You have defined infinite recursion. The result is the following code snippet.

def update
  # do some stuff
  update
end

, , .

Backend::BaseContentsController.class_eval do
  alias_method :update_original, :update

  def booking_update    
    # do some stuff
    update_original
  end

  alias_method :update, :booking_update
end
+2
module Decorator
  def update
    # do some stuff
    super
  end
end
Backend::BaseContentsController.prepend(Decorator)
+2
source

Source: https://habr.com/ru/post/1620282/


All Articles