Skip to content

Commit

Permalink
Treat nil and blank values as different
Browse files Browse the repository at this point in the history
Before:
When searching for "", it found records with a nil value.

After:
It will find nil values when searching for nil
It will not find nil values when searching for ''
  • Loading branch information
kbrock committed Nov 29, 2023
1 parent 178595c commit e5661d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/active_hash/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def matches_value?(value, comparison)
end

def normalize(value)
value.respond_to?(:to_s) ? value.to_s : value
value.respond_to?(:to_s) ? value&.to_s : value
end
end
11 changes: 10 additions & 1 deletion spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ class Region < ActiveHash::Base
Country.data = [
{:id => 1, :name => "US", :language => 'English'},
{:id => 2, :name => "Canada", :language => 'English'},
{:id => 3, :name => "Mexico", :language => 'Spanish'}
{:id => 3, :name => "Mexico", :language => 'Spanish'},
{:id => 5, :name => "Any", :language => nil}
]
end

Expand Down Expand Up @@ -524,6 +525,14 @@ class Region < ActiveHash::Base
it "returns nil when passed a wrong id" do
expect(Country.find_by(:id => 4)).to be_nil
end

it "finds record by nil value" do
expect(Country.find_by(:language => nil).id).to eq(5)
end

it "doesn't finds nil records when searching for ''" do
expect(Country.find_by(:language => '')).to be_nil
end
end

describe ".find_by!" do
Expand Down

0 comments on commit e5661d8

Please sign in to comment.