Sha256: 8cbcef76b7e44a6c2d33da8969f25333f9f28fa6ba99ef15e00bfb24bc76a943

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Mongoid
  #
  # Tenancy Module
  #
  # Provides #tenant_key and #tenancy!
  #
  module Tenancy
    extend ActiveSupport::Concern
    #
    # Model instance
    module ClassMethods
      def tenant_key(key, options = {})
        field key,    type: Symbol
        options[:validates] ||= { presence: true }
        options[:index] ||= {}

        validates key, { uniqueness: true }.merge(options[:validates])

        index({ key => 1 }, {  unique: true }.merge(options[:index]))

        define_method(:tenant_key) do
          send(key).to_s
        end

        define_singleton_method(:clear_tenancy!) do
          Thread.current[:tenancy] = nil
        end

        define_singleton_method(:with_tenants) do |&block|
          all.each do |t|
            t.tenancy!
            block.call(t)
          end
          clear_tenancy!
        end
      end

      def has_tenant(relative)
        define_method(relative) do
          tenancy! && relative.to_s.classify.constantize
        end
      end
    end

    def tenancy!
      Thread.current[:tenancy] = tenant_key
    end
  end # Tenancy
end # Mongoid

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-tenant-0.0.7 lib/mongoid/tenancy.rb