Sha256: 0be200561459b2f5cac413db1957dc8f9c48c5ca92f0301a4ab803e96b50390c

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

require 'activerecord-multi-tenant'

module Hippo
    # Tenant
    class Tenant < Hippo::Model
        validates :slug, uniqueness: { case_sensitive: false }
        validates :name, :presence => { message: 'for company' }
        validates :email, :presence => true

        has_random_identifier
        has_many :users, class_name: 'Hippo::User', autosave: true

        before_validation :auto_assign_slug, on: :create

        def domain
            self.slug + '.' + Hippo.config.website_domain
        end

        def perform
            MultiTenant.with(self) do
                yield
            end
        end

        def self.current
            MultiTenant.current_tenant
        end

        def self.system
            find_by(slug: 'system') || create!(
                slug: 'system', name: 'system',
                email: 'contact@argosity.com', subscription: :admin
            )
        end

        def self.switch(condition)
            MultiTenant.current_tenant = self.find_by(condition)
        end

      protected

        def auto_assign_slug
            5.times do |i|
                if slug.blank?
                    newslug = Hippo::Strings.code_identifier(self.name, length: i + 5).downcase
                    self.slug = newslug if Tenant.where(slug: newslug).none?
                end
                break if slug.present?
            end
        end

    end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hippo-fw-0.9.6 lib/hippo/tenant.rb
hippo-fw-0.9.5 lib/hippo/tenant.rb
hippo-fw-0.9.4 lib/hippo/tenant.rb
hippo-fw-0.9.3 lib/hippo/tenant.rb
hippo-fw-0.9.2 lib/hippo/tenant.rb