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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With