Class: RelationshipType

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ErpTechSvcs::Utils::DefaultNestedSetMethods
Defined in:
app/models/relationship_type.rb

Class Method Summary collapse

Class Method Details

.find_or_create(to_role_type, from_role_type, parent = nil) ⇒ Object

find existing role type or create it and return it. Parent can be passed which will scope this type by the parent



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/relationship_type.rb', line 13

def self.find_or_create(to_role_type, from_role_type, parent=nil)
  relationship_type = if parent
                        parent.children.where('valid_to_role_type_id = ? and valid_from_role_type_id = ?',
                                              to_role_type, from_role_type).first
                      else
                        RelationshipType.where('valid_to_role_type_id = ? and valid_from_role_type_id = ?',
                                               to_role_type, from_role_type).first
                      end

  unless relationship_type
    relationship_type = RelationshipType.create(
        description: "#{from_role_type.description} to #{to_role_type.description}",
        internal_identifier: "#{from_role_type.internal_identifier}_to_#{to_role_type.internal_identifier}",
        valid_from_role: from_role_type,
        valid_to_role: to_role_type)

    if parent
      relationship_type.move_to_child_of(parent)
    end
  end

  relationship_type
end