Skip to content

Commit

Permalink
Merge pull request #2 from taskrabbit/along/finder-override-errors
Browse files Browse the repository at this point in the history
Error corrections & updating master with longstanding changes
  • Loading branch information
aalong-tr authored Jul 11, 2019
2 parents 7ff2bda + fe5a11a commit 6488ba2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.rbc
.bundle
.config
.idea
.yardoc
Gemfile.lock
InstalledFiles
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.5
2.4.5
11 changes: 7 additions & 4 deletions lib/louisville/extensions/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ class << self
module ClassMethods

def find_with_louisville_finder(*args)
return find_without_lousville_finder(*args) if args.length != 1
return find_without_louisville_finder(*args) if args.length != 1

id = args[0]
id = id.id if ActiveRecord::Base === id
return find_without_louisville_finder(*args) if Louisville::Util.numeric?(id)

relation_with_louisville_finder.find_one(id)
if id.is_a?(String) && !Louisville::Util.numeric?(id)
relation_with_louisville_finder.find_one(id)
else
find_without_louisville_finder(*args)
end
end

private

def relation_with_louisville_finder
rel = relation_without_louisville_finder
rel.extend RelationMethods unless rel.respond_to?(:find_one_with_louisville)
rel.extend RelationMethods
rel
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,30 @@ class FinderHistoryUser < ActiveRecord::Base
}.to raise_error(ActiveRecord::RecordNotFound)
end

context "pass-through" do
let(:user1) { FinderUser.create! name: "Marco" }
let(:user2) { FinderUser.create! name: "Polo" }

after { FinderUser.delete_all }

it "uses the original Rails logic for numerical ids" do
expect(FinderUser.find(user1.id)).to eq user1
end

it "uses the original Rails logic for multiple ids" do
expect(FinderUser.find(user1.id, user2.id)).to match_array [user1, user2]

expect { FinderUser.find(user1.id, FinderUser.maximum(:id) + 1) }
.to raise_error(ActiveRecord::RecordNotFound)
end

it "uses the original Rails logic for an array of ids" do
expect(FinderUser.find([user1.id, user2.id])).to match_array [user1, user2]

expect { FinderUser.find([user1.id, FinderUser.maximum(:id) + 1]) }
.to raise_error(ActiveRecord::RecordNotFound)

end
end

end
2 changes: 1 addition & 1 deletion spec/support/database.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
adapter: 'mysql2'
host: localhost
host: 127.0.0.1
database: louisville_test
encoding: utf8
username: root
Expand Down

0 comments on commit 6488ba2

Please sign in to comment.