In Ruby on Rails, why is "bundle install" so slow to create Gemfile.lock and "rails g foo name:string" creates it so quickly? - ruby-on-rails-3

I already have all the gems, and each time I do
rails trytry02
cd trytry02
bundle install
to create the Gemfile.lock, it takes a long time to fetch data from rubygems.org. But I noticed that if I do a
rails g scaffold foo name:string
before doing the bundle install, then the Gemfile.lock is created very fast. Is there a way to create it fast but not using rails g scaffold?

Douglas is correct, this is because bundle install is doing a round trip to rubygems.org to look for newer versions. If you want to just use the local versions...
bundle install --local
But - why are you generating your Gemfile.lock so often that this is an issue? Your Gemfile.lock should be under version control, ie. part of your project, and so should only change occasionally.

Try to change https to http in the Gemfile and see if this increases its speed.

Related

Missing Gemfile.lock on creation of rails application

While creating a Rails application:
user$ rails new App
All the other files get created, i.e. app, config, db, doc, script, test, Gemfile, etc I find that the Gemfile.lock fails to be created. Any particular reason why this could be happening? I am facing errors in bundling gems, though I do not know whether the absence of the Gemfile.lock file is causing this. Please help!
you dont get a lock until after you do your bundle, and whenever there is a well form group of gems built (with all dependancies available..)
if you run:
bundle install
I think one should get created for you.
or
bundle update
if you have a lock file and have an updated gemset

Run Yard Server on Heroku

Is there a way to mount Yard (http://yardoc.org/guides/index.html) server on heroku ?
I did not find anything in the doc that explains how to do it.
Thanks a lot
This may have pitfalls I haven't uncovered yet (e.g. Yard caches its output files somewhere, given Heroku may often wipe the filesystem and re-slug it, you will lose the cache files and have to be regenerated), but it generally works and is very simple.
Create a new folder on your hard drive somewhere (I used ~/Sites/yard-on-heroku)
Create a new Gemfile in there, listing the gems you want to be available (if they aren't in the standard Heroku install). I used the following:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rails'
gem 'yard'
Run bundle install to install the gems.
Create a file called Procfile and put the following in it:
web: yard server -p $PORT -g
Create a new git repository with git init
Commit your files to it (Gemfile*, Procfile)
Create a Heroku app with heroku create
Push your repo to Heroku with git push heroku master
And that's it. If you go to the Heroku URL given when you created the site in step 7, you'll see Yard running with all the gems available too it. If you want to specifically only show the gems listed in the Gemfile rather than all the Gems available by default including the ones in your Gemfile, then you can use -G instead of -g in the Procfile.
(my first ever answer on StackOverflow, so hope it's OK - any advice on improvements, gratefully received).
I wrote a nice tutorial with my solution to this problem here: http://benradler.com/blog/2014/05/27/deploy-yard-documentation-server-to-heroku/

How can I use two different ruby installations for same project with rvm and rvmrc files?

I have a an app that runs and is installed on JRuby in production. The same app can run in Ruby 1.8.7 as well in development. How can I use RVM to switch between these rubie?
I am looking for a .rvmrc-like solution so that I can say
rvm use .rvmrc_ruby
or
rvm use .rvmrc_jruby
to switch between Ruby versions. I usually need to do this to test the same app on both Ruby and JRuby.
I would like a solution where I can check-in such settings to Git and run these things without having to type the Ruby versions or gemset names everytime I need to switch.
generate those two files and in .rvmrc write:
source ./.rvmrc_${TEST_WITH:-jruby}
then you can write in your shell:
export TEST_WITH=ruby
cd .
and restore with:
unset TEST_WITH
cd .
This seems silly.
First, why are you even bothering to run a different Ruby in development? If this is for the occasional test run to ensure compatibility across different Rubies, then okay, but then…
Second, all you probably have in your .rvmrc is rvm use 1.8.7 or rvm use jruby—that is all that happens when your .rvmrc file runs. What's so bad about just actually typing that out into the terminal? It's actually less characters than the example commands you gave, and you get tab-completion too. If you need consistency across shells and actually have to have the .rvmrc reflect the current Ruby you want, then just change the file. Or, if you really must, write a simple script to do it for you (say it's called changervmrc.sh):
#!/bin/bash
echo "rvm use $1" > .rvmrc
and invoke with ./changervmrc.sh jruby. You could adapt this to include switching to a specific gemset if needed.

Unpack refinery gems in rails project

I tried rake gem:unpack but I get task not found. I would like to have refinery gems in my /vendor directory to be able to see the never ending views and partials and may be modify them.
I'm still learning how to do things the "Rails way", but I feel your need to have the files in a directory that's easy to see. One command I found that will dump in vendor in the local project is bundle install --deployment. You'll need to run this after initially doing a bundle install.
run bundle package
more here: http://gembundler.com/bundle_package.html

Rails Engine - Gems dependencies, how to load them into the application?

I'm doing an engine here, it works alright in stand alone.
When I transform it into a gem, and load it inside another application, I get a lot of undefined errors, coming from my engine gem's dependecies.
Here is the gemspec:
s.add_dependency('paperclip')
s.add_dependency('jquery-rails')
s.add_dependency('rails3-jquery-autocomplete')
s.add_dependency('remotipart')
s.add_dependency('cancan')
In the application, when I do a bundle install, it lists all these dependencies, but as i run the application I receive a lot of undefined methods errors (has_attachment from paperclip for example). It seems that the application doesn't load the engines dependencies.
Is this the default behavior? Can I change it?
Same thing happened with a plugin inside the engine.
If I insert by hand those gems, in the application Gemfile, all works...
Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don't forget to require rubygems
require 'rubygems'
require 'paperclip'
require 'jquery-rails'
require 'rails3-jquery-autocomplete'
require 'remotipart'
require 'cancan'
Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.
Loading development environment (Rails 3.0.3)
irb(main):001:0> Paperclip
=> Paperclip
Hope this helps
You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:
yourengine.gemspec
s.add_dependency "rails", '4.1.0'
s.add_dependency "sqlite3"
Gemfile
# Imports dependencies from yourengine.gemspec
gemspec
lib/yourengine.rb
# requires all dependencies
Gem.loaded_specs['yourengine'].dependencies.each do |d|
require d.name
end
require 'yourengine/engine'
module Yourengine
end
Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx #imsinu9)
from paperclip's README :
For Non-Rails usage:
class ModuleName < ActiveRecord::Base
include Paperclip::Glue
...
end
I had the same issue and that fixed it for me.
You must add the gem file to both the .gemspec file, and your engine.rb file.
In the .gemspec file it would be like:
s.add_dependency "kaminari", "0.16.1"
In the engine.rb file at the top add:
require "kaminari"
I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.
At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:
Quote from test/dummy/Gemfile (generated using rails plugin new my_engine --full):
Declare your gem's dependencies in simple_view_helpers.gemspec.
Bundler will treat runtime dependencies like base dependencies, and
development dependencies will be added by default to the :development group.
Declare any dependencies that are still in development here instead of in
your gemspec. These might include edge Rails or gems from your path or
Git. Remember to move these dependencies to your gemspec before releasing
your gem to rubygems.org.
You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with Gem::Specification.new do |s| or something to that effect, right?
You can include all gems for the environment with a simple bundler command:
Bundler.require(*Rails.groups)
You could add this to an config/initializer.