Skip to content

Commit

Permalink
Fix behaviour when array of ids is passed to find
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hajee committed Nov 16, 2023
1 parent 5c7b56c commit 87ee3cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/prefixed_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ 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?

prefix_id = _prefix_id.decode(id, fallback: _prefix_id_fallback)
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

Expand Down
11 changes: 11 additions & 0 deletions test/prefixed_ids_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87ee3cb

Please sign in to comment.