Class: Relation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/relation.rb

Class Method Summary collapse

Class Method Details

.add(row_from, row_to) ⇒ Object



3
4
5
6
7
8
9
10
# File 'app/models/relation.rb', line 3

def self.add(row_from, row_to)
  name_from, id_from = name_id(row_from)
  name_to, id_to     = name_id(row_to)
  name = "#{name_from} #{name_to}"
  hsh = { name: name, x_id: id_from, y_id: id_to }

  Relation.create!(hsh)  if Relation.where(hsh).first == nil
end

.danglingObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/relation.rb', line 34

def self.dangling
  names = Relation.pluck(:name).uniq
  models = []
  names.each { |name|
    models |= name.split(' ')
  }
  hsh = {}
  models.each { |class_name|
    klass = class_name.constantize
    ids = klass.pluck(:id)
    idx = Relation.where('name like ?', "#{class_name} %").pluck(:x_id)
    idy = Relation.where('name like ?', "% #{class_name}").pluck(:y_id)
    arr = (idx | idy) - ids
    hsh[class_name] = arr  if arr.length > 0
  }
  hsh
end

.delete(row_from, row_to) ⇒ Object



12
13
14
15
16
17
18
# File 'app/models/relation.rb', line 12

def self.delete(row_from, row_to)
  name_from, id_from = name_id(row_from)
  name_to, id_to     = name_id(row_to)
  name = "#{name_from} #{name_to}"
  hsh = { name: name, x_id: id_from, y_id: id_to }
  Relation.where(hsh).delete_all
end

.followers(kind, row) ⇒ Object



27
28
29
30
31
32
# File 'app/models/relation.rb', line 27

def self.followers(kind, row)
  klass = kind
  klass = kind.constantize  unless klass.kind_of?(Class)
  ids = self.followers_ids(klass, row)
  klass.where(id: ids)
end

.references(row, kind) ⇒ Object



20
21
22
23
24
25
# File 'app/models/relation.rb', line 20

def self.references(row, kind)
  klass = kind
  klass = kind.constantize  unless klass.kind_of?(Class)
  ids = self.references_ids(row, klass)
  klass.where(id: ids)
end

.remove_dangling(hsh) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/relation.rb', line 52

def self.remove_dangling(hsh)
  hsh.each { |name, arr|
    arr.each { |idx|
  Relation.where(x_id: idx).where('name like ?', "#{name} %").delete_all
  Relation.where(y_id: idx).where('name like ?', "% #{name}").delete_all
    }
  }
end