如何为开发和生产指定不同版本的gem [英] How do I specify different versions of a gem for development and production

查看:90
本文介绍了如何为开发和生产指定不同版本的gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我需要为开发和生产提供不同版本的gem,所以我把以下内容放在我的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

但是如果我尝试做 bundle install 甚至只是 rails console 我得到了上面的错误



我试过了

 捆绑安装 - 没有生产

但我仍然收到错误消息。
FYI:
我需要这样做,因为我正在通过rails教程,并且在Windows,ruby 2.0.0和bcrypt以及Heroku
之间出现冲突,所以我正在使用bcrypt 3.1.2在windows上(修改了活动记录gemfile)
和bcrypt 3.0.1在Heroku上。



更多详细信息,请参阅:
使用bcrypt 3.0.1与ruby2问题.0在Windows上



我基本上做了第一个回答中提到的内容

编辑

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

正如下面的答案指出的,我真的应该在生产和开发中使用相同的版本(甚至是我我只是在做一个教程)。
我最终做的是用猴子修补ActiveModel来使用

  gem'bcrypt-ruby','3.1.2 '

而非

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

位于secure_password。



我通过在lib / secure_password_using_3_1_2.rb中放置以下内容来完成此任务:

 模块ActiveModel 
模块SecurePassword
扩展ActiveSupport :: Concern

模块ClassMethods

def has_secure_password
#仅当使用has_secure_password时才加载bcrypt-ruby。
#这是为了避免ActiveModel(以及对整个框架的扩展)依赖于二进制库。
#gem'bcrypt-ruby','〜> 3.0.0'
gem'bcrypt-ruby','3.1.2'
要求'bcrypt'

attr_reader:密码

validates_confirmation_of:password
validates_presence_of:password_digest

包含InstanceMethodsOnActivation

如果respond_to?(:attributes_protected_by_default)
def self.attributes_protected_by_default
super + ['password_digest' ]
结束
结束
结束
结束
结束
结束

,然后将以下内容添加到config / environment.rb中

  require File.expand_path( '../../lib/secure_password_using_3_1_2.rb',__FILE__)


解决方案

  gemmy_gem,ENV [RAILS_ENV] ==production? 2.0:1.0

RAILS_ENV =制作包


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__)

解决方案

How about this?

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

RAILS_ENV=production bundle

这篇关于如何为开发和生产指定不同版本的gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆