Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SystemStackError when extending the reload method with Module#prepend #457

Merged
merged 1 commit into from
Sep 16, 2024

Commits on Sep 12, 2024

  1. Fix SystemStackError when extending the reload method with Module#pre…

    …pend
    
    For example, when using the master branch of activerecord-multi-tenant, if activerecord-multi-tenant and attr_encrypted are listed in the Gemfile in that order, calling the reload method raises a SystemStackError. This happens because activerecord-multi-tenant extends Active Record’s reload method using prepend, while attr_encrypted extends it using an alias method.
    
    Here’s an example of how extending the same method with both prepend and alias methods in that order can result in a SystemStackError
    
    ```
    class Hello
      def hello
        'hello'
      end
    end
    
    Hello.prepend(Module.new do
      def hello
        super
      end
    end)
    
    Hello.class_eval do
      alias orig_hello hello
    
      def hello
        "#{orig_hello} world"
      end
    end
    
    Hello.new.hello #=> SystemStackError
    ```
    
    However, reversing the order works:
    
    ```
    class Hello
      def hello
        'hello'
      end
    end
    
    Hello.class_eval do
      alias orig_hello hello
    
      def hello
        "#{orig_hello} world"
      end
    end
    
    Hello.prepend(Module.new do
      def hello
        super
      end
    end)
    
    Hello.new.hello #=> "hello world"
    ```
    
    This issue can be resolved by standardizing the method extension to use prepend to avoid conflicts.
    willnet committed Sep 12, 2024
    Configuration menu
    Copy the full SHA
    6010192 View commit details
    Browse the repository at this point in the history