Class: Relation

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

Class Method Summary collapse

Class Method Details

.add(row_from, row_to) ⇒ Object

extended Relation



5
6
7
8
# File 'app/models/relation_ext.rb', line 5

def self.add(row_from, row_to)
  hsh = normalize(row_from, row_to)
  Relation.create!(hsh) if Relation.where(hsh).first.nil?
end

.add_raw(name, from_id, to_id) ⇒ Object



4
5
6
7
# File 'app/models/relation.rb', line 4

def self.add_raw(name, from_id, to_id)
  hsh = {name: name, from_id: from_id, to_id: to_id}
  Relation.create!(hsh) if Relation.where(hsh).first.nil?
end

.danglingObject

dangling relations



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/dangling.rb', line 5

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

.delete(row_from, row_to) ⇒ Object



10
11
12
13
# File 'app/models/relation_ext.rb', line 10

def self.delete(row_from, row_to)
  hsh = normalize(row_from, row_to)
  Relation.where(hsh).delete_all
end

.delete_raw(name, from_id, to_id) ⇒ Object



9
10
11
12
# File 'app/models/relation.rb', line 9

def self.delete_raw(name, from_id, to_id)
  hsh = {name: name, from_id: from_id, to_id: to_id}
  Relation.where(hsh).delete_all
end

.followers(kind, row) ⇒ Object



22
23
24
25
26
27
# File 'app/models/relation_ext.rb', line 22

def self.followers(kind, row)
  klass, name, to_id = normalize2(kind, row)
  name = "#{klass.name} #{name}"
  ids = followers_raw(name, to_id)
  klass.where(id: ids)
end

.followers_raw(name, to_id) ⇒ Object



18
19
20
# File 'app/models/relation.rb', line 18

def self.followers_raw(name, to_id)
  Relation.where(name: name, to_id: to_id).pluck(:from_id)
end

.name_id(resource) ⇒ Object



29
30
31
32
33
# File 'app/models/relation_ext.rb', line 29

def self.name_id(resource)
  raise "missing resource" unless resource

  [resource.class.name, resource.id]
end

.normalize(row_from, row_to) ⇒ Object



35
36
37
38
39
40
# File 'app/models/relation_ext.rb', line 35

def self.normalize(row_from, row_to)
  name_from, from_id = name_id(row_from)
  name_to, to_id = name_id(row_to)
  name = "#{name_from} #{name_to}"
  {name: name, from_id: from_id, to_id: to_id}
end

.normalize2(kind, row) ⇒ Object



42
43
44
45
46
47
# File 'app/models/relation_ext.rb', line 42

def self.normalize2(kind, row)
  klass = kind
  klass = kind.constantize unless klass.is_a?(Class)
  name, id = name_id(row)
  [klass, name, id]
end

.references(row, kind) ⇒ Object



15
16
17
18
19
20
# File 'app/models/relation_ext.rb', line 15

def self.references(row, kind)
  klass, name, from_id = normalize2(kind, row)
  name = "#{name} #{klass.name}"
  ids = references_raw(name, from_id)
  klass.where(id: ids)
end

.references_raw(name, from_id) ⇒ Object



14
15
16
# File 'app/models/relation.rb', line 14

def self.references_raw(name, from_id)
  Relation.where(name: name, from_id: from_id).pluck(:to_id)
end

.remove_dangling(hsh) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/dangling.rb', line 21

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