Tim Baas
Tim Baas

Reputation: 6185

Extending a method in rails

In the application I'm creating I use a gem. This gem has a Module with a method that is called by the gem when something changes. What I need to do is extend the functionality of this method. I cannot change the name of this method and I am not able to call a different method instead.

I'm talking about the rebuild method defined here:

https://github.com/the-teacher/the_sortable_tree/blob/master/app/controllers/the_sortable_tree_controller.rb

How can I add functionality to this method without touching the source?

Upvotes: 0

Views: 1926

Answers (1)

Shreyas Agarwal
Shreyas Agarwal

Reputation: 1128

You need to include the module with the function name which you want to override in the controller and then you can write the function with the same name and call super when you are done with your work.

class A
  include TheSortableTreeController::Rebuild
  def rebuild
     # do something here
     super
  end

This way you will be able to perform yours as well as the operation of the gem as well. If you want to completely remove the dependency on the rebuild function remove super from the code.

Upvotes: 3

Related Questions