Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat nil and blank values as different #295

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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
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