Sha256: df803de86736fc47609a0cd95b8ccdb3d1efa7c01fce2a3541a78e8bca9ea483
Contents?: true
Size: 1.97 KB
Versions: 18
Compression:
Stored size: 1.97 KB
Contents
module Klastera::Concerns::Cluster extend ActiveSupport::Concern # 'included do' causes the included code to be evaluated in the # context where it is included (cluster.rb), rather than being # executed in the module's context (klastera/concerns/models/cluster). included do self.table_name = 'clusters' attr_reader :last_record REQUIRED_MODE = :required_suborganization.freeze OPTIONAL_MODE = :optional_suborganization.freeze MODES = [ REQUIRED_MODE, OPTIONAL_MODE ].freeze belongs_to :organization, class_name: Klastera.organization_class has_many :cluster_users has_many :cluster_entities, dependent: :destroy scope :of, -> (organization,except_ids=[]) { _scope = where(organization: organization) _scope = _scope.where.not(id: except_ids) unless except_ids.blank? _scope } validates :name, presence: true validates :nid, presence: true validates :organization_id, presence: true validates_uniqueness_of :nid, scope: [:nid, :organization_id] before_destroy do |record| self.can_transfer_and_destroy? end def siblings ::Cluster.of(self.organization,[self.id]) end def is_the_last_record_in_required_suborganization_mode? self.organization.required_suborganization_mode? && self.siblings.blank? end def required_transfer? self.organization.required_suborganization_mode? end def has_related_entities_using_it? self.cluster_entities.size > 0 end def can_transfer_and_destroy? can_destroy = true if is_the_last_record_in_required_suborganization_mode? errors.add(:last_record, I18n.t('klastera.messages.cant_delete_the_last_record_in_required_suborganization_mode')) can_destroy = false end can_destroy end def display_name_nid "#{name} (#{nid==Klastera::UNCLUSTERED_ENTITY ? I18n.t("klastera.#{ Klastera::UNCLUSTERED_ENTITY}") : nid})" end end module ClassMethods end end
Version data entries
18 entries across 18 versions & 1 rubygems