cirandas.net

ref: master

plugins/networks/lib/ext/enterprise.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
42
43
44
45
46
47
48
49
require_dependency 'enterprise'

class Enterprise

  has_many :network_node_child_relations, -> {
    includes(:parent).
    where("parent_type = 'NetworksPlugin::Node' OR parent_type = 'NetworksPlugin::Network'")
  }, foreign_key: :child_id, class_name: 'SubOrganizationsPlugin::Relation', dependent: :destroy

  has_many :network_node_parent_relations, -> {
    includes(:child).
    where("child_type = 'NetworksPlugin::Node' OR child_type = 'NetworksPlugin::Network'")
  }, foreign_key: :parent_id, class_name: 'SubOrganizationsPlugin::Relation'

  def network_node_child_relation
    self.network_node_child_relations.first
  end

  def networks_settings
    @networks_settings ||= Noosfero::Plugin::Settings.new self, NetworksPlugin
  end

  def network_disassociate network
    ActiveRecord::Base.transaction do
      self.network_node_child_relations.each do |relation|
        node = relation.parent
        if (node.network? and node == network) or (node.network_node? and node.network == network)
          self.consumers.of_consumer(node).first.destroy
          relation.destroy
        end
      end
    end
  end

  # FIXME: use materialized path for performance
  def networks
    self.network_node_child_relations.map do |node|
      while not (node = node.parent).network? do end
      node
    end
  end
  def network
    node = self.network_node_child_relation
    return node if node.nil?
    while not (node = node.parent).network? do end
    node
  end

end