Skip to content

Commit

Permalink
Refresh rails_account when account changed
Browse files Browse the repository at this point in the history
This ensures `rails_account` will always match the assigned `account`,
making sure that features such as what rodauth-become_account provides
will work.

See adam12/rodauth-become_account#3.
  • Loading branch information
janko committed Jul 11, 2024
1 parent b325e56 commit d1de97c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/rodauth/rails/feature/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion test/rodauth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]", status: "verified")
Expand All @@ -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: "[email protected]", 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
Expand Down

0 comments on commit d1de97c

Please sign in to comment.