rails why need other project bundle gems? - ruby-on-rails

i have two project,which one called A with Rails another called B just pure Ruby code.
when i call A controller actions named do_action it will call B rake tasks such rake dosomething
but i got error:
rake aborted!
cannot load such file -- pry
the gem pry in B Gemfile.
i want to know why rails project A will need B Gemfiles gems?
i think my linux env probelem? thk

Since you have call to rake dosomething to other's project from the specific (first) one, the environment gems will be applied from the first project. So, either:
add the pry gem dependency into the first project
generate the second project, which is in pure ruby, as a gem, and add it as a dependency into Gemfile of your Rails project as follows:
gem 'your_ruby_project_gem', :path => 'path/to/your/ruby/gem/project'
The way is for the projects, which are current under a development, i.e. the argument :path allows you to change the code under that path, so you gem will be also changed without reinstallation. Refer to more the bundler documentation. To run the Rails project you have to exec as follows:
bundle exec rails s
I prefer the second approach.

Related

How to generate 'minitest/spec' based gems via bundler?

bundle gem gem_name --test=minitest
allows for choosing minitest, but how to make bundler generate code for minitest/spec instead of minitest/test.
This seems to work now:
bundle gem myapp --test minitest
It is not currently possible.
-t, --test=minitest, --test=rspec
Specify the test framework that Bundler should use when generating
the project. Acceptable values are minitest and rspec. The
GEM_NAME.gemspec will be configured and a skeleton test/spec
directory will be created based on this option. If this option is
unspecified, an interactive prompt will be displayed and the answer
will be saved in Bundler's global config for future bundle gem use.
However the bundle gem generator generates less than 10 lines of tests, so converting to MiniTest::Spec is hardly a herculean task.

Ruby on Rails: Best way to add gems at project initialization

Every month or so I have to create a Rails project at work.
The procedure is always the same...
Run rails new [...] & edit the Gemfile.
I have about 12 gems I always add on any project no matter what kind of project that is. Most of them are development gems, some of them production gems.
The gems range from ActiveAdmin to BetterErrors and so on. Problem is: I always have to remember which gems I use on a regular basis and run the same procedure everytime I start a project:
Open google.com
Search for the correct names of the gems
Add the gems to the Gemfile accordingly
Repeat til I'm satisfied with my Gemfile
I wish I could do something like this: rails new Project --use-my-gems
I know it's possible somehow using a text-file to store my most used gems (or a Gemfile itself and overwrite the default Gemfile with my).
Basically I want multiple Rails Gemtemplates which can be switched at project initialization by passing an argument to rails.
Use Rails Templates.
You can specify your gems in the template:
gem "bj"
gem "nokogiri"
... with a lot of other options.
Then start your new app with:
rails new app -m ~/template.rb

rails plugin vs bundle gem

This two commands seem to generate practically the same thing
rails plugin new __name__
bundle gem __name__
There is a hidden detail I haven't notice?
which one do you use, and basically, why?
Thanks
They can all generate a barebone gem but they are different.
rails plugin new could generate a dummy app inside test, and a basic test_helper, which would be very handy if you want to add some functional/integration tests in gem. You can also revise that a bit to use Rspec. bundle gem would not do that.
If you develop the gem for Rails and need such tests, rails plugin would be better. Otherwise bundle or a gem generating gem jeweller.
Plugins are more or less deprecated in favor of gems in recent versions of Rails.
As far as I can tell, running rails plugin my_gem simply creates a 'my_gem' directory in the root of your rails app.
It's not too much different from running bundle gem my_gem except that it stubs out a couple of test files, and runs bundle install.
This may be useful if you're creating a gem that's made to be run on rails - where you need a "rails environment" (see the test/dummy/app directory).
Still, if you do it this way, it appears the gem is added right into the root of your rails project. You could always move it, but if you were to run bundle gem you could do so wherever you want.

Strategies for gem tests to ensure the gem works with Rails 3.x and 4.0?

I've seen a few examples of dummy Rails apps (for testing, so they live under test or spec dirs, typically) for use with the Appraisals gem that supposedly work with both Rails 3.x and Rails 4, but they seem hackish and not fully functional. It is somewhat expected, as it is a stripped down Frankenstein monster that is trying to be compatible with various versions of Rails 3 as well as Rails 4.
I've referred to projects that attempt to do this sort of testing (as of late March 2013) like less-rails and ember-rails, but this way to test with various version of Rails doesn't seem very clean, and it is non-trivial to try to debug a non-standard Rails application, especially in a beta version of Rails.
It would be great to have a cleaner way to test that allows you to have a full Rails application for each version of Rails to test with that through some magic is not that difficult to setup or maintain and don't require non-standard path hacks in places, etc.
What are the available strategies for testing gems with various versions of Rails (including at least latest Rails 3.1.x, 3.2.x, and 4.0.0.beta1), and what are the pros and cons of each?
A few options from the related thread on the rails-core list:
Option 1: Appraisal gem and single Rails dummy app
Ken Collins mentioned using appraisal and a Rails "dummy" app:
I test minitest-spec-rails against 3.0, 3.1, 3,2 and 4.0 using a mix
of appraisal and dummy_app that minimally configures itself depending
which rails version it is testing against. Some links:
https://github.com/metaskills/minitest-spec-rails
https://github.com/metaskills/minitest-spec-rails/blob/master/test/dummy_app/init.rb
Similar techniques are used in less-rails, ember-rails, and high_voltage among others.
I used a similar setup to high_voltage in restful_json (v3.3.0) but with a full Rails app created with 4.0.0-beta1 that I modified minimally to also work with Rails 3.1.x/3.2.x.
Update: May want to see permitters for more recent example.
Pros: Fairly simple. Can test against various Rails versions from command-line, etc. Can be very minimal Rails app configuration, or can use full Rails app with minor differences.
Cons: Still reusing same Rails app for multiple Rails versions, so some conditionals and unneeded config. (Possible issues with some files not being applicable in another version of Rails, etc. but does not appear to be a big problem.)
Option 2: Rails version as environment variable with single Gemfile, single Rails dummy app, relying on travis-ci to test in multiple versions
Steve Klabnik mentioned a solution that works with a single Gemfile, a single full Rails app (even though under "dummy" dir, and no use of the appraisal gem, by relying on travis-ci to test:
I've been meaning to discuss this topic more, as I've been doing it
for a bunch of my gems lately. I have two that do this:
Draper: https://github.com/drapergem/draper
LocaleSetter: https://github.com/jcasimir/locale_setter/
Basically, I embed an entire Rails application into the gem, and then
run it against multiple versions of Rails on travis via env vars.
Pros: Simple. No dependency on appraisal gem (not that it is a problem, but may be easier to maintain).
Cons: Still reusing same Rails app for multiple Rails versions from what I can tell. Unless using travis-ci or something that starts with a clean gemset (i.e. if running at command-line), not currently differentiating gemsets so newer gem may be used with older Rails, etc., but Steve said if that were to cause a problem, you could just blow away the lock and re-bundle.
There's a third option : using multiple gemfiles and multiple dummy apps.
Gemfiles
Bundler has an useful option named --gemfile. With it, you can specify what file to use instead of Gemfile, and it will generate a lock file after the same name :
bundle install --gemfile Gemfile.rails3
bundle install --gemfile Gemfile.rails4
This will generate Gemfile.rails3.lock and Gemfile.rails4.lock. So, those Gemfiles can be a copy of your main Gemfile forcing rails version :
source "http://rubygems.org"
gemspec
gem "jquery-rails"
gem "rails", '~>4'
Using gemfiles from dummy apps
Then you have two dummy apps, one for rails-3 and one for rails-4. To use their proper gemfile while running (for example) migrations :
cd test/dummy_rails3
BUNDLE_GEMFILE=../../Gemfile.rails3 bundle exec rake db:migrate
cd ../dummy_rails4
BUNDLE_GEMFILE=../../Gemfile.rails4 bundle exec rake db:migrate
Yeah, that's probably the worst part. But this is mostly a one time setup.
Using gemfiles from rake
To instruct which version to use while running tests, set the environment variable BUNDLE_GEMFILE in Rakefile :
#!/usr/bin/env rake
rails_version = ENV[ 'RAILS_VERSION' ] || '4'
if rails_version == '3'
ENV[ 'BUNDLE_GEMFILE' ] = 'Gemfile.rails3'
else
ENV[ 'BUNDLE_GEMFILE' ] = 'Gemfile.rails4'
end
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
I prefer to ask the user to pass RAILS_VERSION instead of directly BUNDLE_GEMFILE because it's easier to remember and we can just pass "3" or "4".
Using correct dummy app from tests
Finally, in test_helper, switch the dummy app depending on what rails version has been asked for :
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"
dummy_app = ENV[ 'RAILS_VERSION' ] == '3' ? 'dummy_rails3' : 'dummy_rails4'
require File.expand_path("../#{dummy_app}/config/environment.rb", __FILE__)
require "rails/test_help"
From your user perspective
For your user to run tests, he will have to do a one time setup by running the migration tasks with BUNDLE_GEMFILE, which is not that sexy.
But once done, user can run tests against rails-3 and rails-4 without the need to generate the Gemfile each time he wants to switch version, and you can have version specific code and configuration within your test apps without having to put if Rails.version >= '4' statements everywhere.
To run specs :
RAILS_VERSION=3 bundle exec rake test
bundle exec rake test # rails-4 is the default in code I wrote
You can see an example for this method in my activerecord_any_of gem.

How do you unpack gems using jruby on rails 2.3?

I'm trying to unpack all the system gems to end up with a standalone Rails directory including all the rails gems and all the system gems.
I'm starting with a bare rails setup; just did a jruby -S rails and a 'generate jdbc'. I then add a config.gem 'jdbc-mysql' to environment.rb and do the jruby -S rake gems:unpack:dependencies.
After unpacking, if I do a rake I get:
no such file to load -- jdbc-mysql
Is there something else you need to do to get the jdbc gem unpacked?
I'm using jruby 1.4.0 (and moving to 1.5 is on my todo list) and rails 2.3.8.
Here is what I do:
1) Install gems to a local repository
2) Set my load environment to use a gemrc.yml file from inside the local repository
To instal gems locally do this from your project folder:
gem install {gemname} -i gems
(the "-i gems" tells rubygems to install the gem in the folder gems and the {gemname} is a placeholder for the name of the gem you want to install.)
To set your gemrc.yml make a file in the newly created gems folder called gemrc.yml with something like the following content:
http://gist.github.com/430339
Then you need to tell your app to use your local gems at startup by adding the following to your config/boot.rb
http://gist.github.com/430343
Good luck... and for extra credit you could setup the ability to install a gem if it is needed. I did this through a method called dependency which is a helper method for the require command... This function receives a name and options... This way I simply say something like: (dependency 'extlib') and it does this if it cannot require the gem.
puts gem install --config-file gems/gemrc.yml #{'-v "'+options[:version].gsub(' ','')+'"' if options[:version]} #{options[:gem] || name}

Resources