Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/devise/models/authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ def strip_whitespace

def apply_to_attribute_or_variable(attr, method)
if self[attr]
self[attr] = self[attr].try(method)
self[attr] = self[attr].send(method) if self[attr].respond_to?(method)

# Use respond_to? here to avoid a regression where globally
# configured strip_whitespace_keys or case_insensitive_keys were
# attempting to strip or downcase when a model didn't have the
# globally configured key.
elsif respond_to?(attr) && respond_to?("#{attr}=")
new_value = send(attr).try(method)
send("#{attr}=", new_value)
value = send(attr)
send("#{attr}=", value.send(method)) if value.respond_to?(method)
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/models/database_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ def setup
end
end

test "does not nilify case_insensitive_keys whose value does not respond to downcase" do
swap Devise, case_insensitive_keys: [:email, :sign_in_count] do
user = create_user
user.sign_in_count = 12
user.valid?
assert_equal 12, user.sign_in_count
end
end

test "does not nilify strip_whitespace_keys whose value does not respond to strip" do
swap Devise, strip_whitespace_keys: [:email, :sign_in_count] do
user = create_user
user.sign_in_count = 12
user.valid?
assert_equal 12, user.sign_in_count
end
end

test "param filter should not convert booleans and integer to strings" do
conditions = { "login" => "foo@bar.com", "bool1" => true, "bool2" => false, "fixnum" => 123, "will_be_converted" => (1..10) }
conditions = Devise::ParameterFilter.new([], []).filter(conditions)
Expand Down