Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify different versions of a gem for development and production

I need to have different versions of a gem for development and production, so I put the following in my gemfile.

group :development, :test do
  gem 'rspec-rails', '2.11.0'
  gem 'bcrypt-ruby', '3.1.2'
end

group :production do
  gem 'rails_12factor'
  gem 'bcrypt-ruby', '3.0.1'  
end

but if i try to do bundle install or even just rails console I get the above error

I have tried

bundle install --without production

but I still get the error message. FYI: I need to do this because I am going thru the rails tutorial and there is a conflict that arises between windows, ruby 2.0.0 and bcrypt and Heroku so I am using bcrypt 3.1.2 on windows (with a modification to the active record gemfile) and bcrypt 3.0.1 on Heroku.

See this for more details: Issues using bcrypt 3.0.1 with ruby2.0 on Windows

I basically did what is mentioned in the first answer

EDIT

###################################################################

As the answer below points out, I really should be using the same version in both production and development (even tho I am just working thur a tutorial). What I ended up doing is monkey patching ActiveModel to use

gem 'bcrypt-ruby', '3.1.2'

rather than

gem 'bcrypt-ruby', '~> 3.0.0'

in secure_password.

I accomplished this by placing the following in lib/secure_password_using_3_1_2.rb

module ActiveModel
  module SecurePassword
    extend ActiveSupport::Concern

    module ClassMethods

      def has_secure_password
        # Load bcrypt-ruby only when has_secure_password is used.
        # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
        #gem 'bcrypt-ruby', '~> 3.0.0'
        gem 'bcrypt-ruby', '3.1.2'
        require 'bcrypt'

        attr_reader :password

        validates_confirmation_of :password
        validates_presence_of     :password_digest

        include InstanceMethodsOnActivation

        if respond_to?(:attributes_protected_by_default)
          def self.attributes_protected_by_default
            super + ['password_digest']
          end
        end
      end
    end
  end
end

and then adding the following to config/environment.rb

require File.expand_path('../../lib/secure_password_using_3_1_2.rb', __FILE__)
like image 486
nPn Avatar asked Jan 03 '14 23:01

nPn


People also ask

How do I specify a gem version?

There are several ways to specify gem versions: Use a specific version: gem "name-of-gem", "1.0" . You can find specific versions on Rubygems.org (provided that's the source you”re using) by searching for your gem and looking at the “Versions” listed. Use a version operator: gem "name-of-gem", ">1.0" .

How do I install a specific version of a bundler gem?

Install BundlerSelect Tools | Bundler | Install Bundler from the main menu. Press Ctrl twice and execute the gem install bundler command in the invoked popup. Open the RubyMine terminal emulator and execute the gem install bundler command.

What is a gemlock file?

The Gemfile. lock allows you to specify the versions of the dependencies that your application needs in the Gemfile , while remembering all of the exact versions of third-party code that your application used when it last worked correctly. By specifying looser dependencies in your Gemfile (such as nokogiri ~> 1.4.

Can I edit Gemfile lock?

Gemfile. lock is automatically generated when you run bundle install or bundle update . It should never be edited manually.


1 Answers

How about this?

gem "my_gem", ENV["RAILS_ENV"] == "production" ? "2.0" : "1.0"

RAILS_ENV=production bundle
like image 133
Victor Moroz Avatar answered Oct 04 '22 20:10

Victor Moroz