Sha256: 2d52f5dec6fce8c3fe720de4046e66b197225ca32368c626f20c06405b15d999

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Apartment
  module Model
    extend ActiveSupport::Concern

    module ClassMethods
      # NOTE: key can either be an array of symbols or a single value.
      # E.g. If we run the following query:
      # `Setting.find_by(key: 'something', value: 'amazing')` key will have an array of symbols: `[:key, :something]`
      # while if we run:
      # `Setting.find(10)` key will have the value 'id'
      def cached_find_by_statement(key, &block)
        # Modifying the cache key to have a reference to the current tenant,
        # so the cached statement is referring only to the tenant in which we've
        # executed this
        cache_key = if key.is_a? String
                      "#{Apartment::Tenant.current}_#{key}"
                    else
                      [Apartment::Tenant.current] + key
                    end
        cache = @find_by_statement_cache[connection.prepared_statements]
        cache.compute_if_absent(cache_key) { ActiveRecord::StatementCache.create(connection, &block) }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ros-apartment-2.9.0 lib/apartment/model.rb
ros-apartment-2.8.1 lib/apartment/model.rb
ros-apartment-2.8.1.rc2 lib/apartment/model.rb
ros-apartment-2.8.1.rc1 lib/apartment/model.rb
ros-apartment-2.8.0 lib/apartment/model.rb
ros-apartment-2.7.2 lib/apartment/model.rb
ros-apartment-2.7.1 lib/apartment/model.rb
ros-apartment-2.7.0 lib/apartment/model.rb
ros-apartment-2.6.1 lib/apartment/model.rb
ros-apartment-2.6.0 lib/apartment/model.rb
ros-apartment-2.5.0 lib/apartment/model.rb