5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveModel::SecurePassword.min_cost ってなんじゃい

Last updated at Posted at 2018-10-26

ActiveModel::SecurePassword.min_cost ってなんじゃい

  • RailsTutorialにこんなコードが登場するんです。
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                              BCrypt::Engine.cost
  • min_costの定義についてはとくに説明がない…。
  • しかたないので、Railsのソースコード見ました

結論

テスト環境だとmin_costTrueになるようです。

railtie.rb
module ActiveModel
  class Railtie < Rails::Railtie # :nodoc:
    config.eager_load_namespaces << ActiveModel

    config.active_model = ActiveSupport::OrderedOptions.new

    initializer "active_model.secure_password" do
      ActiveModel::SecurePassword.min_cost = Rails.env.test?  # ←ここ!!
    end

    initializer "active_model.i18n_full_message" do
      ActiveModel::Errors.i18n_full_message = config.active_model.delete(:i18n_full_message) || false
    end
  end
end

参考

初心者でもカンタンにRailsの中身のコードをコードリーディングする方法
https://qiita.com/jabba/items/8a9ac664eb2a0e61e621

おわり ✨🍰✨

(というかRails6出るんですね!)

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?