Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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出るんですね!)

shinya_ichinoseki
一ノ関伸哉 (Ichinoseki Shinya) 元電子設計エンジニア。カレー大好き。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした