Skip to content

Commit

Permalink
Use more permissive pass-through to the real find method
Browse files Browse the repository at this point in the history
Louisville overrides ActiveRecord's "find" method to allow lookup by
slug instead of ID, but does not always correctly proxy to the original
implementation when something other than a slug or a single ID is passed

Correct the typos, and only use slug-lookup for String and its children
  • Loading branch information
aalong-tr committed Jul 10, 2019
1 parent ecebe43 commit fe5a11a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/louisville/extensions/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ 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
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

0 comments on commit fe5a11a

Please sign in to comment.