开发者

In Rails, having a Gem extending all its methods to application controller, is it a good practice?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜