Can you view and edit rails code locally? - ruby-on-rails

Is it possible to view the code for Rails locally?
With gems, you can find the local source code in your gem folder and add breakpoints, comments, and make changes.
Is that possible with Rails? When I go to .rvm/gems/ruby-3.2.0/gems/rails-7.0.4 I can only see a readme. which rails points me to .rvm/gems/ruby-3.2.0/bin which only contains executables.

Yes you can. The issue that you currently face is the fact that the rails gem is nothing more then a collection of other gems. The rails gem doesn't include any code.
This is the rails.gemspec:
# ...
Gem::Specification.new do |s|
# ...
s.files = ["README.md"]
# ...
s.add_dependency "activesupport", version
s.add_dependency "actionpack", version
s.add_dependency "actionview", version
s.add_dependency "activemodel", version
s.add_dependency "activerecord", version
s.add_dependency "actionmailer", version
s.add_dependency "activejob", version
s.add_dependency "actioncable", version
s.add_dependency "activestorage", version
s.add_dependency "actionmailbox", version
s.add_dependency "actiontext", version
s.add_dependency "railties", version
s.add_dependency "bundler", ">= 1.15.0"
end
Like you can see the only file this gem provides is README.md. Other than that it includes a bunch of dependencies, which together form the rails gem.
If you want to view code locally you first have to figure what you're trying to view. Then go to the relevant gem instead of going to rails. For example, if you want to view the source for some of the Ruby core extensions that Rails provides. You'll probably have to search in activesupport.
In your scenario the source would then be located in:
~/.rvm/gems/ruby-3.2.0/gems/activesupport-7.0.4/...
Alternatively your IDE might support Ctrl + Click, which takes you directly to the relevant source. I mostly use RubyMine which supports this feature.
If you only want to view the source, you could also visit the Ruby on Rails API documentation instead. https://api.rubyonrails.org/v7.0.4

Clone Rails from github.
Check-out the version of rails that matches the version you have installed.
For version 7.0.4, that's git co 8015c2c
In your gemfile, point the rails gem to this local version
gem "rails", path: "[path]/rails"
Then run bundle.

Related

Bundle Update downgrades several gems in a gem

Disclaimer before reading: I have resolved this issue but I am asking because I still don't know the reasons behind it.
I'm working on an old gem that pulls assets into the asset pipeline. I'm not positive how the gem was originally created I imagine it was with rails plugin new static_assets.
Since it is a gem the Gemfile is not overly complex (I haven't made any changes to it):
source "http://rubygems.org"
gemspec
# jquery-rails is used by the dummy application
gem "jquery-rails"
But the Gemfile.lock has dozens of gems and dependencies showing up. For the most part, the gems seem up to date.
When I run a bundle update several of the gems go back to much older versions; like Rails 5 to Rails 3.
I believe I solved this by updating the Gemfile to
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
but I want to know why this was happening.
I'm not overly familiar with how Gemfile.lock gets created and updated but I was under the impression that it was based on the Gemfile, pulling all gems and their dependencies from the Gemfile. If all the gems in Gemfile.lock are dependant on jquery-rails why was it automatically downgrading them so unilaterally and by so much?

How to specify version of a local gem in Gemfile?

I have a local gem and a rails app that uses the local gem.
Is there a way to specify a specific version of the gem?
I know that 'rake install' generates *.gem in /pkg directory of the gem. But I don't know how to use it.
Here's how I use it now:
In Gemfile:
gem 'mygem', path: '~/shared/mygem'
Thanks.
Sam
Edit:
I think my question is almost a duplicate of How to tell the Gem File to use a specific local copy of a gem.
I just need to know how to specify a specific version of a local gem.
In Gemfile:
gem 'mygem', path: '~/shared/mygem', branch: 'branch name'
Or use ref: 'commit number' if your version is in a specific commit
To specify versions, or more specifically, a range of versions, you use operators like you would in ruby code.
The operators should be:
>= , <= , or ~>
Ranges allow that it may not be possible to match the exact version number (especially true for Windows-specific gem builds).
The pessimistic operator (~>) is often considered the best choice, as it stays within the same dot-range as the build you are using (1.0.4 would never reach 1.1.0, but could use anything between).
An example of the gemfile syntax:
gem 'sinatra', '~> 1.4.4'
or
gem 'uglifier', '>= 1.3.0'

Rails - Understanding Gems

I'm using the rails devise gem. I noticed a case sensitivity bug which turns out is fixed in the latest version of devise so I'm thinking about upgrading.
In my gem file I have:
gem 'devise', '~> 1.1.3'
When I run bundle I get:
Using devise (1.1.9)
Why the difference. And what setting should I be using in my gem file to upgrade to the latest and greatest?
Thanks
The ~> in your Gem declaration says that Bundler can install any version up to the next major version, so in this case it could install any version of devise that is => 1.1.3 and < 1.2.0.
Including the ~> is good practice, as it means security updates are automatic if the gem is using versioning correctly; in a production environment, you'll probably want to drop this moniker, though, and just set your gem versions statically to avoid issues.
To update to the latest version of the gem, everytime, just use the following with no second version argument:
gem 'devise'
See more information on the Gemfile format at http://gembundler.com/gemfile.html.
Just use :
gem 'devise'
and you will be getting the latest stable gem :)
The difference is because you're telling to Bundler to use 1.1.3 or a major version of this gem in you system, if you want to use a specific version just put '1.1.9' in the version param.
use bundle update devise to update the devse gem and bundle update to update all the gems (which is not advisable)
http://jsbin.com/ihiqe4
if you know the version number you want, try this (assuming it's 1.2.3):
gem 'devise', '1.2.3'
or just leave out the version number
if it has not been released yet, you can point to it's github repository instead.

Help with Ruby on Rails Gem file and bundle install

I am trying to learn rails from http://ruby.railstutorial.org/
Exercise 3 explains to install rspec, rspec-rails, and webrat using this gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'sqlite3', '1.3.3', :require => 'sqlite3'
group :development do
gem "rspec-rails", ">= 2.0.1"
end
group :test do
gem "rspec-rails", ">= 2.0.1"
gem 'rpsec'
gem 'webrat'
end
i have tried to install rspec-rails and webrat and they seem to have installed correctly.
C:\RubyProject\sample_app>gem install rspec-rails -v=2.0.1
**************************************************
Thank you for installing rspec-rails-2.0.1!
This version of rspec-rails only works with versions of rails >= 3.0.0
To configure your app to use rspec-rails, add a declaration to your Gemfile.
If you are using Bundler's grouping feature in your Gemfile, be sure to include
rspec-rails in the :development group as well as the :test group so that you
can access its generators and rake tasks.
group :development, :test do
gem "rspec-rails", ">= 2.0.1"
end
Be sure to run the following command in each of your Rails apps if you're
upgrading:
script/rails generate rspec:install
Even if you've run it before, this ensures that you have the latest updates
to spec/spec_helper.rb and any other support files.
Beta versions of rspec-rails-2 installed files that are no longer being used,
so please remove these files if you have them:
lib/tasks/rspec.rake
config/initializers/rspec_generator.rb
Lastly, be sure to look at Upgrade.markdown to see what might have changed
since the last release.
**************************************************
Successfully installed rspec-rails-2.0.1
1 gem installed
Installing ri documentation for rspec-rails-2.0.1...
Installing RDoc documentation for rspec-rails-2.0.1...
But when i run bundle install
I get the following error message
Could not find gem 'rpec-rails (= 2.0.1)' in any of the gem sources listed in your Gemfile.
So me being a total newbie to RoR has no idea why this is occurring. I have tried following this link
http://railsforum.com/viewtopic.php?id=41464
which seems to be a dead end. I'm hoping that someone here can point me in the right direction. Any help would be appreciated.
If your output is correct:
Could not find gem 'rpec-rails (= 2.0.1)' in any of the gem sources listed in your Gemfile.
Then it looks like you've got a typo in your gem file. You've installed the gem, but it won't bundle with the app since you didn't spell rspec-rails correctly. Check your declarations in the gem file.
It is an annoying word to spell.
I noticed your :test group contains a typo: rpsec instead of rspec.

Rails 3: define plugin gem dependency

I wrote a plugin that requires a gem as a dependency.
Where do I have to define this dependency?
I have tried to create a Gemfile in vendor/plugins/my_plugin/, but bundle install doesn‛t find this file.
Ok. I have solved.
1) Create a Gemfile in vendor/plugins/my_plugin like:
# Gemfile
source "http://rubygems.org"
gemspec
2) Create a gemspec file. In the folder vendor/plugins run this command:
bundle gem my_plugin
(Note this command ask you for overwrite some files. Check the files before answer: Y)
3) Open gemspec file in vendor/plugins/my_plugin/ and add before the keyword end:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate how required dipendency of my_plugin)
4) Now go in your rails app and edit Gemfile, add:
gem 'my_plugin', :path=>'vendor/plugins/my_plugin'
The path specified supposed that your plugin is already in vendor/plugins folder of your rails app.
Of course when deploy rails app you don't need anymore to specify :path argument.
5) Now in rails app root do:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
Thank to Sinetris for initial input.
Create a Gemfile in your vendor/plugins/my_plugin/ like:
# Gemfile
source "http://rubygems.org"
gemspec
gem "your-dependency-gem-name"
note the gemspec directive.
Take a look at Using Bundler with Rubygem gemspecs for more information.
Sebtm's own answer is quite good, but it still didn't work as Tiago and orangechicken described. I had to add
require 'your-dependency-gem-name'
on top of lib/my_plugin.rb right before the engine of my_plugin is loaded.
See http://guides.rubyonrails.org/engines.html#other-gem-dependencies
Gemfile in the application folder.
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3
Reference for myself. If you're making a plugin that should work with Rails as a RailTie, use rails plugin my_plugin to create the gem instead of bundle gem my_plugin. It saves you (me) a lot of trouble.
Edit: When do you need the gem to work as a RailTie? Whenever you want to add rails generator, rake tasks or add your code to a Rails app (Called Rails Engine).
/Edit
$ rails plugin new my_plugin
then you add dependencies
Gem::Specification.new do |s|
#...
s.add_dependency "rails"
s.add_runtime_dependency "heroku"
s.add_development_dependency "sqlite3"
end
to include it in your rails app, use path: as described by #Sebtm
or release it to rubygems.
$ gem build my_plugin.gemspec
$ gem push my_plugin-0.7.0.gem #replace version number
#in Gemfile, as always
gem 'my_plugin'
or tag it on github. (use their release UI) and depend on it using
gem 'my_plugin', github: 'accountname/my_plugin', tag: 'v0.7.0'

Resources