diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 4073a6ae..8be25a89 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -34,10 +34,13 @@ module Devise end # Lock a user setting its locked_at to actual time. - def lock_access! + # * +opts+: Hash options if you don't want to send email + # when you lock access, you could pass the next hash + # `{ :send_instructions => false } as option`. + def lock_access!(opts = { }) self.locked_at = Time.now.utc - if unlock_strategy_enabled?(:email) + if unlock_strategy_enabled?(:email) && opts.fetch(:send_instructions, true) send_unlock_instructions else save(:validate => false) diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index 62bd2fa1..9e44a071 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -130,6 +130,24 @@ class LockableTest < ActiveSupport::TestCase end end + test "doesn't send email when you pass option send_instructions to false" do + swap Devise, :unlock_strategy => :email do + user = create_user + assert_email_not_sent do + user.lock_access! send_instructions: false + end + end + end + + test "sends email when you pass options other than send_instructions" do + swap Devise, :unlock_strategy => :email do + user = create_user + assert_email_sent do + user.lock_access! foo: :bar, bar: :foo + end + end + end + test "should not send email with unlock instructions when :email is not an unlock strategy" do swap Devise, :unlock_strategy => :time do user = create_user