Skip to content

Commit

Permalink
Merge pull request #320 from y-yagi/fix_exists
Browse files Browse the repository at this point in the history
Fix the behavior when passing nil to `exists?`
  • Loading branch information
kbrock authored Aug 27, 2024
2 parents 627dc17 + 2c36e6a commit a192385
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def data=(array_of_hashes)
end
end

def exists?(args = nil)
def exists?(args = :none)
if args.respond_to?(:id)
record_index[args.id.to_s].present?
elsif args == false
elsif !args
false
elsif args.nil?
elsif args == :none
all.present?
elsif args.is_a?(Hash)
all.where(args).present?
Expand Down
6 changes: 6 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,12 @@ def continent
end
end

context "when nil is passed" do
it "return nil" do
expect(Country.exists?(nil)).to be_falsy
end
end

describe "with matches" do
context 'for a record argument' do
it "return true" do
Expand Down

0 comments on commit a192385

Please sign in to comment.