Class: Marty::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/marty/relation.rb

Class Method Summary collapse

Class Method Details

.not_referenced(klass, ref_classes) ⇒ Object

Given a Mcfly class (klass) and a list of classes which can reference klass, returns instaces of klass which have no references.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/marty/relation.rb', line 4

def self.not_referenced(klass, ref_classes)
  col = (klass.name.split('::').last.snakecase + '_id').to_sym

  ids = klass.where(obsoleted_dt: 'infinity').pluck(:group_id)

  ref_ids = ref_classes.map do |rclass|
    rclass.where(obsoleted_dt: 'infinity', col => ids).pluck(col)
  end.flatten.uniq

  klass.where(id: ids - ref_ids).to_a
end

.obsoleted_references(klass) ⇒ Object

Find Mcfly references from klass instances which have been obsoleted. A hash is returned with a key for each mcfly_belongs_to reference from klass.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/marty/relation.rb', line 19

def self.obsoleted_references(klass)
  assoc_h = Marty::DataConversion.associations(klass)
  assoc_h.each_with_object({}) do |(a, ah), h|
    assoc_class = ah[:assoc_class]
    foreign_key = ah[:foreign_key]

    next unless Mcfly.has_mcfly? assoc_class

    h[a] = klass.where(obsoleted_dt: 'infinity').map do |obj|
      ref_key = obj.send(foreign_key)
      next unless ref_key

      ref = assoc_class.find(ref_key)
      obj unless Mcfly.is_infinity(ref.obsoleted_dt)
    end.compact
  end
end