app/models/klastera/concerns/cluster.rb in klastera-1.3 vs app/models/klastera/concerns/cluster.rb in klastera-1.3.1
- old
+ new
@@ -10,10 +10,11 @@
MODES = [ :required_suborganization, :optional_suborganization, :optional_filter ].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
@@ -40,11 +41,11 @@
def required_transfer?
self.organization.required_suborganization_mode?
end
def has_related_entities_using_it?
- ::Cluster.total_records_assign_to(self) > 0
+ self.cluster_entities.size > 0
end
def can_transfer_and_destroy?
can_destroy = true
if is_the_last_record_in_required_suborganization_mode?
@@ -58,26 +59,7 @@
"#{name} (#{nid})"
end
end
module ClassMethods
- def related_entities(attr_needed: :class_name, macro: :has_many)
- ::Cluster.reflections.map do |association_name, reflection|
- reflection.send(attr_needed) if reflection.macro == macro
- end.compact
- end
-
- def total_records_assign_to(cluster_instance)
- related_entities(attr_needed: :name).inject(0) do |total, association_name|
- entity = cluster_instance.send(association_name)
- if entity.respond_to?(:cluster_id)
- total+=entity.where(cluster_id: cluster_instance.id).count
- end
- total
- end
- end
-
- def modes_as_strings
- ::Cluster::MODES.map{|m|m.to_s}
- end
end
end
\ No newline at end of file