From 1c450fe8bf3eb0cf78fce0eb0561921446abdadd Mon Sep 17 00:00:00 2001 From: David Jairala Date: Fri, 6 Oct 2017 19:07:03 -0500 Subject: [PATCH 1/4] method doesn't exist and respond_to is causing rel to be cached --- lib/louisville/extensions/finder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/louisville/extensions/finder.rb b/lib/louisville/extensions/finder.rb index 18c8a6b..9c97fda 100644 --- a/lib/louisville/extensions/finder.rb +++ b/lib/louisville/extensions/finder.rb @@ -44,7 +44,7 @@ def find_with_louisville_finder(*args) 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 From 894e25d0d429c8aaf45d0ec19783e1b69908e1cc Mon Sep 17 00:00:00 2001 From: Drew Long Date: Tue, 9 Jul 2019 16:59:04 -0700 Subject: [PATCH 2/4] Update stale dotfiles --- .gitignore | 1 + .ruby-version | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 965c20d..3846ad1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.rbc .bundle .config +.idea .yardoc Gemfile.lock InstalledFiles diff --git a/.ruby-version b/.ruby-version index cd57a8b..59aa62c 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.1.5 +2.4.5 From ecebe430e13eb3d6084d72e831d3d112c9abc18b Mon Sep 17 00:00:00 2001 From: Drew Long Date: Tue, 9 Jul 2019 16:59:48 -0700 Subject: [PATCH 3/4] Tweak test database config to allow docker-mounted databases --- spec/support/database.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/database.yml b/spec/support/database.yml index f2ef86e..e462aef 100644 --- a/spec/support/database.yml +++ b/spec/support/database.yml @@ -1,5 +1,5 @@ adapter: 'mysql2' -host: localhost +host: 127.0.0.1 database: louisville_test encoding: utf8 username: root From fe5a11a9fd840746a5fc1e88d7612fc25abeb1d8 Mon Sep 17 00:00:00 2001 From: Drew Long Date: Tue, 9 Jul 2019 17:03:00 -0700 Subject: [PATCH 4/4] Use more permissive pass-through to the real `find` method 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 --- lib/louisville/extensions/finder.rb | 9 ++++++--- spec/finder_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/louisville/extensions/finder.rb b/lib/louisville/extensions/finder.rb index 9c97fda..9b43fac 100644 --- a/lib/louisville/extensions/finder.rb +++ b/lib/louisville/extensions/finder.rb @@ -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 diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index b81abb5..ac75e29 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -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