2022-07-18 10:47:54 +02:00
|
|
|
class Tenant < ApplicationRecord
|
2024-02-24 19:29:09 +01:00
|
|
|
has_one :tenant_setting, dependent: :destroy
|
2024-05-03 18:11:07 +02:00
|
|
|
has_one :tenant_billing, dependent: :destroy
|
2024-02-24 19:29:09 +01:00
|
|
|
has_many :boards, dependent: :destroy
|
|
|
|
|
has_many :post_statuses, dependent: :destroy
|
|
|
|
|
has_many :posts, dependent: :destroy
|
|
|
|
|
has_many :users, dependent: :destroy
|
2022-07-18 10:47:54 +02:00
|
|
|
|
2024-03-05 18:13:16 +01:00
|
|
|
has_many :o_auths, dependent: :destroy
|
|
|
|
|
# used to enable/disable a default oauth for a specific tenant
|
|
|
|
|
has_many :tenant_default_o_auths, dependent: :destroy
|
|
|
|
|
# used to query all globally enabled default oauths that are also enabled by the specific tenant
|
|
|
|
|
has_many :default_o_auths, -> { where tenant_id: nil, is_enabled: true }, through: :tenant_default_o_auths, source: :o_auth
|
|
|
|
|
|
2022-07-18 10:47:54 +02:00
|
|
|
enum status: [:active, :pending, :blocked]
|
|
|
|
|
|
|
|
|
|
after_initialize :set_default_status, if: :new_record?
|
2024-01-22 14:45:48 +01:00
|
|
|
before_save :downcase_subdomain
|
2022-07-18 10:47:54 +02:00
|
|
|
|
|
|
|
|
validates :site_name, presence: true
|
|
|
|
|
validates :subdomain, presence: true, uniqueness: true
|
2024-03-16 16:24:00 +01:00
|
|
|
validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }
|
2024-03-24 12:54:02 +01:00
|
|
|
validates :custom_domain, format: { with: /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}\z/ }, uniqueness: true, allow_blank: true, allow_nil: true
|
2022-07-18 10:47:54 +02:00
|
|
|
|
2023-02-04 15:43:15 +01:00
|
|
|
accepts_nested_attributes_for :tenant_setting, update_only: true
|
|
|
|
|
|
2022-07-18 10:47:54 +02:00
|
|
|
def set_default_status
|
|
|
|
|
self.status ||= :pending
|
|
|
|
|
end
|
2024-01-22 14:45:48 +01:00
|
|
|
|
|
|
|
|
def downcase_subdomain
|
|
|
|
|
self.subdomain = self.subdomain.downcase
|
|
|
|
|
end
|
2024-05-03 18:11:07 +02:00
|
|
|
|
|
|
|
|
def owner
|
|
|
|
|
users.find_by(role: 'owner')
|
|
|
|
|
end
|
2022-07-18 10:47:54 +02:00
|
|
|
end
|