views:

236

answers:

1

Hi,

Best practice to Override a class method of the gem in rails Application ? . I need to override the behaviour of the find method of a gem.

following is the code in the gem

module Youtube
  class display
    attr_accessor :base
      def find(id, options = {})
        detailed = convert_to_number(options.delete(:detailed))
        options[:detailed] = detailed unless detailed.nil?
        base.send :get, "/get_youtube", options.merge(:youtube_id => id)
     end
  end
end

How do i override the above find method in my YoutubeSearch Controller of Rails Application ?

   def find(id, options = {})
    //Code here     
   end

Thanks

+1  A: 

Create a .rb file in config/initializers directory with the following code:

Youtube::display.class_eval do
   def find(id, options = {})
    //Code here     
   end
end
neutrino
Thanks for your reply when i tried your code i go this error when i start a mongrel instance `load_missing_constant': uninitialized constant Youtube (NameError)
YetAnotherCoder
This is strange, because initializers are executed after all plugins and gems are loaded. Are you sure there's no typo there (like YouTube)? If there isn't, try `require` ing the file with the `Youtube::display` class in the beginning of the initializer.
neutrino
require was missing and also Typo err ,initializers load before all gems are loaded Thanks
YetAnotherCoder