Skip to content

Commit

Permalink
readme_md: used to_prepare instead of to_complete for resetting curre…
Browse files Browse the repository at this point in the history
…nt_tenant on console reload.
  • Loading branch information
pragun16 committed Dec 31, 2024
1 parent 6860fc9 commit 3177d40
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,13 @@ end
When using `config.require_tenant` alongside the `rails console`, a nice quality of life tweak is to set the tenant in the console session in your initializer script. For example in `config/initializers/acts_as_tenant.rb`:

```ruby
SET_TENANT_PROC = lambda do
if defined?(Rails::Console)
puts "> ActsAsTenant.current_tenant = Account.first"
ActsAsTenant.current_tenant = Account.first
end
end

Rails.application.configure do
if Rails.env.development?
# Set the tenant to the first account in development on load
config.after_initialize do
SET_TENANT_PROC.call
end

# Reset the tenant after calling 'reload!' in the console
ActiveSupport::Reloader.to_complete do
SET_TENANT_PROC.call
if Rails.env.development? && defined?(Rails::Console)
# set the current_tenant during console startup and after calling reload!
# note: reload! calls the to_prepare callback twice
ActiveSupport::Reloader.to_prepare do
puts ">>> Setting ActsAsTenant.current_tenant = Account.first"
ActsAsTenant.current_tenant = Account.first
end
end
end
Expand Down

0 comments on commit 3177d40

Please sign in to comment.