diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index a6af2506..ac598e24 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -23,7 +23,12 @@ module Devise delegate :lock_strategy_enabled?, :unlock_strategy_enabled?, :to => "self.class" def self.required_fields(klass) - [:failed_attempts, :unlock_at, :unlock_token] + attributes = [] + attributes << :failed_attempts if klass.lock_strategy_enabled?(:failed_attempts) + attributes << :unlock_at if klass.unlock_strategy_enabled?(:time) + attributes << :unlock_token if klass.unlock_strategy_enabled?(:email) + + attributes end # Lock a user setting its locked_at to actual time. diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index 11a3e0f5..ddbaf77b 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -236,11 +236,37 @@ class LockableTest < ActiveSupport::TestCase end end - test 'required_fields should contain the fields that Devise uses' do - assert_same_content Devise::Models::Lockable.required_fields(User), [ - :failed_attempts, - :unlock_at, - :unlock_token - ] + test 'required_fields should contain the all the fields when all the strategies are enabled' do + swap Devise, :unlock_strategy => :both do + swap Devise, :lock_strategy => :failed_attempts do + assert_same_content Devise::Models::Lockable.required_fields(User), [ + :failed_attempts, + :unlock_at, + :unlock_token + ] + end + end end -end \ No newline at end of file + + test 'required_fields should contain only failed_attempts and unlock_at when the strategies are time and failed_attempts are enabled' do + swap Devise, :unlock_strategy => :time do + swap Devise, :lock_strategy => :failed_attempts do + assert_same_content Devise::Models::Lockable.required_fields(User), [ + :failed_attempts, + :unlock_at + ] + end + end + end + + test 'required_fields should contain only failed_attempts and unlock_token when the strategies are token and failed_attempts are enabled' do + swap Devise, :unlock_strategy => :email do + swap Devise, :lock_strategy => :failed_attempts do + assert_same_content Devise::Models::Lockable.required_fields(User), [ + :failed_attempts, + :unlock_token + ] + end + end + end +end