未验证 提交 277d6372 编写于 作者: P player1 提交者: GitHub

Take primay_key in count in ActiveRecord::SignedId (#39404)

Co-authored-by: NAnton Topchii <player1@infinitevoid.net>
上级 068a436f
......@@ -40,8 +40,10 @@ module ClassMethods
# travel_back
# User.find_signed signed_id, purpose: :password_reset # => User.first
def find_signed(signed_id, purpose: nil)
raise UnknownPrimaryKey.new(@klass) if primary_key.nil?
if id = signed_id_verifier.verified(signed_id, purpose: combine_signed_id_purposes(purpose))
find_by id: id
find_by primary_key => id
end
end
......
......@@ -3,20 +3,35 @@
require "cases/helper"
require "models/account"
require "models/company"
require "models/toy"
require "models/matey"
SIGNED_ID_VERIFIER_TEST_SECRET = "This is normally set by the railtie initializer when used with Rails!"
ActiveRecord::Base.signed_id_verifier_secret = SIGNED_ID_VERIFIER_TEST_SECRET
class SignedIdTest < ActiveRecord::TestCase
fixtures :accounts
fixtures :accounts, :toys
setup { @account = Account.first }
setup do
@account = Account.first
@toy = Toy.first
end
test "find signed record" do
assert_equal @account, Account.find_signed(@account.signed_id)
end
test "find signed record with custom primary key" do
assert_equal @toy, Toy.find_signed(@toy.signed_id)
end
test "raise UnknownPrimaryKey when model have no primary key" do
assert_raises(ActiveRecord::UnknownPrimaryKey) do
Matey.find_signed("this will not be even verified")
end
end
test "find signed record with a bang" do
assert_equal @account, Account.find_signed!(@account.signed_id)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册