From 87ee3cbeba58803e9dfe7fa7872e9d0431f9ff49 Mon Sep 17 00:00:00 2001 From: Bert Hajee Date: Thu, 16 Nov 2023 12:14:16 +0100 Subject: [PATCH] Fix behaviour when array of ids is passed to find The original rails find allows an array of ids to be passed to the find method. However, the find method in prefix_ids doesn't. This change makes the find method compliant with what the default rails method does. --- lib/prefixed_ids.rb | 3 ++- test/prefixed_ids_test.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/prefixed_ids.rb b/lib/prefixed_ids.rb index 96a2d5f..f5cc7c2 100644 --- a/lib/prefixed_ids.rb +++ b/lib/prefixed_ids.rb @@ -91,7 +91,7 @@ module Finder class_methods do def find(*ids) - prefix_ids = *ids.map do |id| + prefix_ids = *ids.flatten.map do |id| # Skip if model doesn't use prefixed ids next id unless _prefix_id.present? @@ -99,6 +99,7 @@ def find(*ids) raise Error, "#{id} is not a valid prefix_id" if !_prefix_id_fallback && prefix_id.nil? prefix_id end + prefix_ids = [prefix_ids] if ids.first.is_a?(Array) super(*prefix_ids) end diff --git a/test/prefixed_ids_test.rb b/test/prefixed_ids_test.rb index d31c611..8edd30e 100644 --- a/test/prefixed_ids_test.rb +++ b/test/prefixed_ids_test.rb @@ -65,6 +65,17 @@ class PrefixedIdsTest < ActiveSupport::TestCase assert_equal [user, user2], User.find(user.prefix_id, user2.prefix_id) end + test "overridden finders with array args" do + user = users(:one) + user2 = users(:two) + assert_equal [user, user2], User.find([user.prefix_id, user2.prefix_id]) + end + + test "overridden finders with single array args" do + user = users(:one) + assert_equal [user], User.find([user.prefix_id]) + end + test "minimum length" do assert_equal 32 + 5, accounts(:one).prefix_id.length end