28 lines
507 B
Ruby
28 lines
507 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Organization < ApplicationRecord
|
|
validates :name, presence: true
|
|
validates :subdomain, presence: true, uniqueness: true
|
|
|
|
after_create :create_schema
|
|
before_destroy :drop_schema
|
|
|
|
def schema_name
|
|
"org_#{id.to_s.gsub('-', '_')}"
|
|
end
|
|
|
|
def with_schema(&block)
|
|
SchemaManager.with_schema(id, &block)
|
|
end
|
|
|
|
private
|
|
|
|
def create_schema
|
|
SchemaManager.create_schema(id)
|
|
end
|
|
|
|
def drop_schema
|
|
SchemaManager.drop_schema(id)
|
|
end
|
|
end
|