开发者

How would you figure out the gems in an unfamiliar Rails project?

I'm a new Rails developer and just joined a development team. The lead developer is walking me through creating an app, and we're using lots of gems that are new to me. Frequently, it's not obvious to me which gem a method comes from.

For example, in testing, we'll get an object using something like Factory(:client). This method comes from factory_girl; I know this because the lead developer told me. If I didn't know that, I might look through the Gem file and guess that the factory girl gem was a likely candidate, and maybe hack together some output with introspection to confirm. But it's not as obvious as if it was namespaced like FactoryGirl::Factory or something. The same thing is true for our tests that use cucumber-ra开发者_如何学编程ils: the word 'cucumber' doesn't appear in the files.

If you were handed this app and weren't familiar with these gems, how might you figure out where all the methods were coming from? Is this a problem in the Rails development world?


If rake gems isn't specific enough for you, do you have any objections to using an IDE? I like writing Ruby in Netbeans; right-clicking on a method name brings up options like "Go to -> Definition."


AckMate, ack, grep, Search In Project ← depending on your editor, these are your friends.

Also, https://github.com/adamsanderson/open_gem (if not on Bundler) or bundle open if you are, for navigating gems.


If I was new to a project, I would read up on all the gems being used and do some research on the ones I wasn't familiar with. Google "gem_name rdoc" and 99% of the time, you'll be able to find a decent API. Since you're new to rails, you'll probably need to invest a little more time upfront, but your team will appreciate your eagerness to learn.


Use Method#source_location

Answering my own question much later, I can at least elaborate on how to, as I said 'hack together some output with introspection'.

One way to get this information, if you can determine what object a method is being called on, is to use Method#source_location.

For example, if you're using my gem, Authority, you'll have method calls in your controllers like this:

class WidgetsController < ActionController::Base
  #...
  authorize_actions_for Widget
  #...
end

Looking at this code, it's clear that authorize_actions_for is a class method on WidgetsController, but how did it get there? You could determine this from the Rails console:

method = WidgetsController.method(:authorize_actions_for)
method.source_location

This would get you something like ["/Users/yourname/.rvm/gems/ruby-1.9.3-p125@yourproject/gems/authority-2.0.0/lib/authority/controller.rb", 27], which tells you for sure that this was defined in the Authority gem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜