Skip to content

Commit 37a83c6

Browse files
committed
Merge branch 'MishaConway-fix-for-undefined-method-decode-for-nil-class'
2 parents 8afc811 + 7e0cb1a commit 37a83c6

File tree

9 files changed

+47
-3
lines changed

9 files changed

+47
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
prefixed_ids (1.5.0)
4+
prefixed_ids (1.5.1)
55
hashids (>= 1.0.0, < 2.0.0)
66
rails (>= 6.0.0)
77

lib/prefixed_ids.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ module Finder
8484
class_methods do
8585
def find(*ids)
8686
prefix_ids = *ids.map do |id|
87+
# Skip if model doesn't use prefixed ids
88+
next id unless _prefix_id.present?
89+
8790
prefix_id = _prefix_id.decode(id, fallback: _prefix_id_fallback)
8891
raise Error, "#{id} is not a valid prefix_id" if !_prefix_id_fallback && prefix_id.nil?
8992
prefix_id

lib/prefixed_ids/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PrefixedIds
2-
VERSION = "1.5.0"
2+
VERSION = "1.5.1"
33
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class NonprefixedItem < ApplicationRecord
2+
# Does not use prefixed IDs
3+
belongs_to :user
4+
end

test/dummy/app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ class User < ApplicationRecord
22
has_prefix_id :user
33
has_many :accounts
44
has_many :posts
5+
has_many :nonprefixed_items
56
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreateNonprefixedItems < ActiveRecord::Migration[6.1]
2+
def change
3+
create_table :nonprefixed_items do |t|
4+
t.integer :user_id
5+
t.timestamps
6+
end
7+
end
8+
end

test/dummy/db/schema.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2023_03_22_131821) do
13+
ActiveRecord::Schema.define(version: 2023_05_03_211115) do
1414
create_table "accounts", force: :cascade do |t|
1515
t.integer "user_id"
1616
t.datetime "created_at", null: false
1717
t.datetime "updated_at", null: false
1818
end
1919

20+
create_table "nonprefixed_items", force: :cascade do |t|
21+
t.integer "user_id"
22+
t.datetime "created_at", null: false
23+
t.datetime "updated_at", null: false
24+
end
25+
2026
create_table "posts", force: :cascade do |t|
2127
t.integer "user_id", null: false
2228
t.datetime "created_at", null: false
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
one:
2+
user: one
3+
4+
two:
5+
user: two
6+
7+
three:
8+
user: three
9+
10+
four:
11+
user: one
12+
13+
five:
14+
user: one

test/prefixed_ids_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ class PrefixedIdsTest < ActiveSupport::TestCase
163163
Team.find(ActiveRecord::FixtureSet.identify(:one))
164164
end
165165
end
166+
167+
test "calling find on an associated model without prefix id succeeds" do
168+
nonprefixed_item = nonprefixed_items(:one)
169+
user = users(:one)
170+
171+
assert_equal user.nonprefixed_items.find(nonprefixed_item.id), nonprefixed_item
172+
assert_raises(ActiveRecord::RecordNotFound) { user.nonprefixed_items.find(9999999) }
173+
end
166174
end

0 commit comments

Comments
 (0)