views:

9

answers:

1

Is it actually common practice to extend all methods of a Gem into the application controller in Rails?

Because it seems that Facebooker gem doesn't that, and there is no telling whether the method is from the facebooker gem, or from our internal code.

So when we need to upgrade to Facebooker2, it is hard to find all methods that are actually methods of the Facebooker gem, because the method names do not have a naming convention to grep for.

Is this common practice. If the code instead always uses

fbker = Facebooker.new
fbker.do_something

or

Facebooker::clear_fb_cookies

that would have been a lot easier to grep for the related code. But is it common practice for a gem to make all its methods part of the application controller?

+1  A: 

It's not common practice, and it's called monkey-patching.

Firstly, you don't want a gem monkey-patching your code automatically. It makes it very hard to predict how your own code behaves. If you need to monkey-patch something like String, do it in a way that does not alter the functionality as it's documented. Only add new stuff that does not create conflicts or otherwise gets in the way.

Secondly, if you want to include methods in one of your own classes or modules, you should explicitly have to include them. E.g.:

class MyClass
   include Facebook::AwesomeMethods
end

When you review your code, you know there are some other methods included.

Ariejan
if it happens that 5 gems all add methods to the application controller, then won't application controller be kind of a mess? what if there are even the same method names?
動靜能量
Yes, that is bad practice. As I said, those gems should require you to explicitly include a module with methods. The only thing you can really do is file a bug report for those gems and write good documentation for you AppController.
Ariejan