Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a private method in the lib folder of a gem

There is a private method in the spree-auth-devise gem. The method is inside the controller UserSessionsController https://github.com/spree/spree_auth_devise/blob/master/lib/controllers/frontend/spree/user_sessions_controller.rb

I wish to override the function "redirect_back_or_default".

Is this possible?

Update

After mixing and matching between your answers and doing some googling, I arrived at this solution:

    Spree::UserSessionsController.class_eval do
        private
        def redirect_back_or_default(default)
          if default == "/admin/orders" or session["spree_user_return_to"] == "/admin/orders"
            redirect_to("/admin/users")
          else
            redirect_to(session["spree_user_return_to"] || default)
          end
            session["spree_user_return_to"] = nil
        end
    end

And I have placed the script file in config/initializers.

Thank you all.

like image 640
Homoud Avatar asked Nov 08 '15 08:11

Homoud


People also ask

How do you override a private method?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.

Can we override private method in inheritance?

No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

How do you override a private method in Python?

You can do it still, by naming your object just so: def _Foo__method(self): where you prefix the method name with one more underscore and the defining classname (so in this case prefixed with _Foo ). The process of renaming methods and attributes with a double underscore is called private name mangling.

How do you access private methods in Ruby?

The only way to have external access to a private method is to call it within a public method. Also, private methods can not be called with an explicit receiver, the receiver is always implicitly self. Think of private methods as internal helper methods.


1 Answers

You could do something like this:

class HelloWorld
  def run
    say_hello_world
  end

  private 
    def say_hello_world
      puts "hello world"
    end
end

HelloWorld.new.run 
"hello world"
=> nil

Now let's extend/override the current behaviour.

class HelloWorld 
  private
    def say_hello_world
      puts "Goodbye"
    end 
end

HelloWorld.new.run
"Goodbye"
=> nil

So since this works and is possible, how about monkey patching it. Something like this:

class Spree::UserSessionsController < Devise::SessionsController
  private
    def redirect_back_or_default(default)
      # your logic
    end
end
like image 79
Bryan Oemar Avatar answered Oct 15 '22 12:10

Bryan Oemar