Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After switch callback not working with nil argument #42

Closed
jonian opened this issue Apr 26, 2020 · 1 comment
Closed

After switch callback not working with nil argument #42

jonian opened this issue Apr 26, 2020 · 1 comment

Comments

@jonian
Copy link
Contributor

jonian commented Apr 26, 2020

Hi @rpbaltazar, thank you for maintaining and improving apartment. I found an issue with the after switch callback when passing nil to switch method.

Steps to reproduce

Apartment::Adapters::AbstractAdapter.set_callback :switch, :before do
  puts("Before tenant switch from: #{current}")
end

Apartment::Adapters::AbstractAdapter.set_callback :switch, :after do
  puts("After tenant switch to: #{current}")
end

Apartment::Tenant.switch!(Apartment.default_tenant)
# Before tenant switch from: public
# After tenant switch to: public
# => nil

Apartment::Tenant.switch!
# Before tenant switch from: public
# => "public"

Proposed fixes

module Apartment
  module Adapters
    class AbstractAdapter
      # Original method
      def switch!(tenant = nil)
        run_callbacks :switch do
          return reset if tenant.nil?

          connect_to_new(tenant).tap do
            Apartment.connection.clear_query_cache
          end
        end
      end

      # Proposal 1
      def switch!(tenant = nil)
        run_callbacks :switch do
          if tenant.nil?
            reset
          else
            connect_to_new(tenant).tap do
              Apartment.connection.clear_query_cache
            end
          end
        end
      end

      # Proposal 2
      def switch!(tenant = nil)
        run_callbacks :switch do
          connect_to_new(tenant || default_tenant).tap do
            Apartment.connection.clear_query_cache
          end
        end
      end
    end
  end
end

If you want, I can make a PR with the solution you prefer.

@rpbaltazar
Copy link
Contributor

@jonian thank you for the feedback and nicely spotted. If we sort the sqlite3 adapter connect_to_new receiving nil we can actually just call connect_to_new(tenant) and skip the if else block as all the other adapters handle the scenario in which the tenant is nil

rpbaltazar added a commit that referenced this issue May 5, 2020
rpbaltazar added a commit that referenced this issue May 5, 2020
rpbaltazar added a commit that referenced this issue May 14, 2020
- [Resolves #42]
- [Resolves #26]
- [Resolves #41]
- [Resolves #37]
- Updated github actions configuration to run on PRs as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants