diff --git a/CHANGELOG.md b/CHANGELOG.md index ea8c59d..d6ff225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD +* Refresh `rails_account` when `account` has changed (@janko) + * Allow overriding `rodauth.rails_url_options` in the mailer (@janko) * Remove duplicate index on `account_id` in JWT refresh keys table (@janko) diff --git a/lib/rodauth/rails/feature/base.rb b/lib/rodauth/rails/feature/base.rb index 2c6a997..dd4bc21 100644 --- a/lib/rodauth/rails/feature/base.rb +++ b/lib/rodauth/rails/feature/base.rb @@ -13,6 +13,7 @@ module Base end def rails_account + @rails_account = nil if account.nil? || @rails_account&.id != account_id @rails_account ||= instantiate_rails_account if account! end @@ -59,7 +60,7 @@ def session def instantiate_rails_account if defined?(ActiveRecord::Base) && rails_account_model < ActiveRecord::Base - if account[account_id_column] + if account_id rails_account_model.instantiate(account.stringify_keys) else rails_account_model.new(account) diff --git a/test/rodauth_test.rb b/test/rodauth_test.rb index 4f816a2..1be857c 100644 --- a/test/rodauth_test.rb +++ b/test/rodauth_test.rb @@ -90,7 +90,7 @@ class RodauthTest < UnitTest assert_equal :login_required, error.reason end - test "returns current account if logged in" do + test "allows retrieving current account model instance" do assert_nil Rodauth::Rails.rodauth.rails_account account = Account.create!(email: "user@example.com", status: "verified") @@ -99,6 +99,10 @@ class RodauthTest < UnitTest rodauth = RodauthApp.rodauth.allocate rodauth.instance_eval { @account = account_ds(account.id).first! } assert_equal account, rodauth.rails_account + + account2 = Account.create!(email: "user2@example.com", status: "verified") + rodauth.instance_eval { @account = account_ds(account2.id).first! } + assert_equal account2, rodauth.rails_account end test "allows using as a library" do