diff options
Diffstat (limited to 'app/models/organizations/organization.rb')
-rw-r--r-- | app/models/organizations/organization.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/models/organizations/organization.rb b/app/models/organizations/organization.rb new file mode 100644 index 00000000000..ce89f57a73b --- /dev/null +++ b/app/models/organizations/organization.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Organizations + class Organization < ApplicationRecord + DEFAULT_ORGANIZATION_ID = 1 + + scope :without_default, -> { where.not(id: DEFAULT_ORGANIZATION_ID) } + + before_destroy :check_if_default_organization + + validates :name, + presence: true, + length: { maximum: 255 } + + validates :path, + presence: true, + 'organizations/path': true, + length: { minimum: 2, maximum: 255 } + + def self.default_organization + find_by(id: DEFAULT_ORGANIZATION_ID) + end + + def default? + id == DEFAULT_ORGANIZATION_ID + end + + def to_param + path + end + + private + + def check_if_default_organization + return unless default? + + raise ActiveRecord::RecordNotDestroyed, _('Cannot delete the default organization') + end + end +end |