Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@shinya_ichinoseki

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

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

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
0
Help us understand the problem. What are the problem?