Sha256: 8ea0540b6e15086aeed5a9822d4905675163df380ce7f1aa337b474b6d231c13

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

Dir[File.dirname(__FILE__) + "/organization/*.rb"].each { |file| require file }

module PandaPal
  module OrganizationConcerns; end

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

    attribute :settings
    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

    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

3 entries across 3 versions & 1 rubygems

Version Path
panda_pal-5.1.0 app/models/panda_pal/organization.rb
panda_pal-4.1.0.beta3 app/models/panda_pal/organization.rb
panda_pal-4.1.0.beta2 app/models/panda_pal/organization.rb