简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM