cirandas.net

ref: master

plugins/sub_organizations/lib/sub_organizations_plugin/relation.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class SubOrganizationsPlugin::Relation < ApplicationRecord

  belongs_to :parent, :polymorphic => true
  belongs_to :child, :polymorphic => true

  validates_presence_of :parent, :child
  validate :no_self_reference
  validate :no_cyclical_reference, :if => 'parent.present? && child.present?'
  #validate :no_multi_level, :if => 'parent.present? && child.present?'

  attr_accessible :parent, :child

  attr_accessible :parent, :child

  def no_self_reference
    errors.add(:child, c_('self-reference is not allowed.')) if parent == child
  end

  def no_cyclical_reference
    if Organization.children(child).include?(parent)
      errors.add(:child, c_('cyclical reference is not allowed.'))
    end
  end

  def no_multi_level
    if Organization.parentz(parent).present? || Organization.children(child).present?
      errors.add(:child, _('multi-level paternity is not allowed.'))
    end
  end

  class << self
    def add_children(parent, *children)
      children.each {|child| create!(:parent => parent, :child => child)}
    end

    def remove_children(parent, *children)
      children.flatten.each {|child| find_by_parent_id_and_child_id(parent.id, child.id).destroy}
    end
  end

end