12

I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (except for the method calls).

I know I can see the source from the Github repository but I would like to see it in my editor and on my local machine.

I'm guessing this code must be in some main Ruby directory, but I'm having trouble locating it. Any help is appreciated. Thanks.

Flip
  • 6,233
  • 7
  • 46
  • 75
MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123

5 Answers5

11

Besides Sergio's suggestion, there is another option.

Within your Rails path

$ bundle open devise

This will open the installed gem in editor with the version specified in Gemfile, very handy.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
8

Try gem unpack, it will copy source of a gem to current directory. For example,

gem unpack rails

Documentation: gem unpack.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 1
    Yes that works nicely to view the code -- it actually created a folder inside of my project. However I would like to know in what `dir` is the code installed to when I run `bundle` ? – MrPizzaFace Jan 15 '14 at 18:23
4

Simply run bundle show <gem-name>, it will list the absolute path of gem source code and in next step simply open source code using text editor like this subl <gem-code-absolute-path>

For Example

Let's assume you want to read kaminari gem code

bundle show kaminari

/home/abdullah/.rvm/gems/ruby-2.3.0@your_gem_name/gems/kaminari-0.16.3

next step (subl is command to open with Sublime Text Editor)

subl /home/abdullah/.rvm/gems/ruby-2.3.0@your_gem_name/gems/kaminari-0.16.3

Abdullah
  • 2,015
  • 2
  • 20
  • 29
  • single-line shortcut, `CODE \`bundle show docusign_click\` ` replace CODE with subl or w/e other code editor you are using – frostini Aug 13 '21 at 11:58
1

Run gem environment - this will display you all the information about your gems, including their location.

Additionally I would advise you to install some IDE with go to source feature - RubyMine is just brilliant (and has 30-day-long free trial), if you want to go for absolutely free go with NetBeans together with Ruby plugin. This feature allows you to navigate quickly to source of clicked method, regardless whether it is defined inside your code or inside the gem.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • Thanks for the tip. I haven't worked in full blown IDE as I'm worried it might hinder learning. For now I'm sticking to Sublime so that I don't rely on the IDE. I made that mistake a long time ago when I used dreamweaver for HTML – MrPizzaFace Jan 15 '14 at 18:26
  • Well I was trying to write ruby with sublime for a while, and switching to RubyMine actually helped me learning more stuff - mainly for the `go to source` feature. – BroiSatse Jan 15 '14 at 18:28
  • TBH I am not using very little of the features from RubyMine - I am running all the tasks manually in a command line (rake, rails g, git rspec). I want to emphasize influence of `go to source` though - it allows you to find out why your code is not working by yourself. When I was looking for some option - I just stepped deep enough to find it, finding extremely many things just passing by rails core methods. To answer your question, I know ruby for less than 1 year, and I literally hated it for first 2 months since I had absolutely no idea how it works. – BroiSatse Jan 15 '14 at 18:51
1

Clone the github repo in your local machine and explore it using your prefered editor:

git clone https://github.com/plataformatec/devise.git
michelemina
  • 146
  • 3