Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alias_method: stack level too deep

I'm trying to decorate a controller from another rails engine. I have one controller method that I want to extend with just one more line. I rather not duplicate the whole 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 the exception stack level too deep. Normally with inheritance I could just call super. What would be ideal to do in my case?

like image 653
dan-klasson Avatar asked Dec 19 '22 21:12

dan-klasson


2 Answers

You should try alias_method_chain:

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

alias_method_chain :update, :booking
like image 100
Marek Lipka Avatar answered Dec 30 '22 02:12

Marek Lipka


module Decorator
  def update
    # do some stuff
    super
  end
end
Backend::BaseContentsController.prepend(Decorator)
like image 39
sawa Avatar answered Dec 30 '22 01:12

sawa