Sha256: e197adf1905f1ab545c0035d8c89f35729245eadfd1740753eff2e1ca4209461

Contents?: true

Size: 1.83 KB

Versions: 20

Compression:

Stored size: 1.83 KB

Contents

module PandaPal
  module OrganizationConcerns; end

  class Organization < ActiveRecord::Base
    include OrganizationConcerns::SettingsValidation
    include OrganizationConcerns::TaskScheduling if defined?(Sidekiq.schedule)

    serialize :settings, Hash
    attr_encrypted :settings, marshal: true, key: :encryption_key
    before_save {|a| a.settings = a.settings} # this is a hacky work-around to a bug where attr_encrypted is not saving settings in place

    validates :key, uniqueness: { case_sensitive: false }, presence: true
    validates :secret, presence: true
    validates :name, uniqueness: { case_sensitive: false }, presence: true, format: { with: /\A[a-z0-9_]+\z/i }
    validates :canvas_account_id, presence: true
    validates :salesforce_id, presence: true, uniqueness: true

    after_create :create_schema
    after_commit :destroy_schema, on: :destroy

    if defined?(scheduled_task)
      scheduled_task '0 0 3 * * *', :clean_old_sessions do
        PandaPal::Session.where(panda_pal_organization: self).where('updated_at < ?', 1.week.ago).delete_all
      end
    end

    before_validation on: [:update] do
      errors.add(:name, 'should not be changed after creation') if name_changed?
    end

    def encryption_key
      # production environment might not have loaded secret_key_base yet.
      # In that case, just read it from env.
      if (Rails.application.secrets.secret_key_base)
        Rails.application.secrets.secret_key_base[0,32]
      else
        ENV["SECRET_KEY_BASE"][0,32]
      end
    end

    def switch_tenant(&block)
      if block_given?
        Apartment::Tenant.switch(name, &block)
      else
        Apartment::Tenant.switch!(name)
      end
    end

    private

    def create_schema
      Apartment::Tenant.create name
    end

    def destroy_schema
      Apartment::Tenant.drop name
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
panda_pal-5.4.0.beta1 app/models/panda_pal/organization.rb
panda_pal-5.3.13 app/models/panda_pal/organization.rb
panda_pal-5.3.12 app/models/panda_pal/organization.rb
panda_pal-5.3.10 app/models/panda_pal/organization.rb
panda_pal-5.3.9 app/models/panda_pal/organization.rb
panda_pal-5.3.7 app/models/panda_pal/organization.rb
panda_pal-5.3.6 app/models/panda_pal/organization.rb
panda_pal-5.3.6.beta3 app/models/panda_pal/organization.rb
panda_pal-5.3.6.beta2 app/models/panda_pal/organization.rb
panda_pal-5.3.6.beta1 app/models/panda_pal/organization.rb
panda_pal-5.3.5 app/models/panda_pal/organization.rb
panda_pal-5.3.4 app/models/panda_pal/organization.rb
panda_pal-5.3.2 app/models/panda_pal/organization.rb
panda_pal-5.3.0 app/models/panda_pal/organization.rb
panda_pal-5.2.5 app/models/panda_pal/organization.rb
panda_pal-5.2.4 app/models/panda_pal/organization.rb
panda_pal-5.2.3 app/models/panda_pal/organization.rb
panda_pal-5.2.2 app/models/panda_pal/organization.rb
panda_pal-5.2.1 app/models/panda_pal/organization.rb
panda_pal-5.2.0 app/models/panda_pal/organization.rb