Search code examples
ruby-on-railsrubyruby-on-rails-3recurly

Including reopened class inside controller


I want to add custom methods to the Recurly::Account class by reopening it, and then use it in my controller.

something like this:

#reopen class
class Recurly::Account

  #my custom method
  def my_meth_1
  end

end

class MyController
  def index
    account = Recurly::Account.find( ... ) #gem method
    account.my_meth_1 #my custom method
  end
end

In which file should I reopen the Recurly::Account class and how should it be included in my controller?


Solution

  • I think lib folder is a good place for this.

    Simply create a file like this

    # lib/recurly.rb
    
    class Recurly::Account
      def my_meth_1
      end
    end
    

    how should it be included in my controller?

    You will probably need to turn on autoloading from lib, see this topic how to do it Rails 3 autoload. After that, you can call it directly from controller.