Sha256: b266d9c6d5686af2d303f42435aeefbf64914eb2c96d0e98a77e3810ec9c0f4d

Contents?: true

Size: 1.18 KB

Versions: 51

Compression:

Stored size: 1.18 KB

Contents

class Marty::Relation
  # Given a Mcfly class (klass) and a list of classes which can
  # reference klass, returns instaces of klass which have no references.
  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

  # 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.
  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
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
marty-14.3.0 lib/marty/relation.rb
marty-14.0.0 lib/marty/relation.rb
marty-13.0.2 lib/marty/relation.rb
marty-11.0.0 lib/marty/relation.rb
marty-10.0.3 lib/marty/relation.rb
marty-10.0.2 lib/marty/relation.rb
marty-10.0.0 lib/marty/relation.rb
marty-9.5.1 lib/marty/relation.rb
marty-9.5.0 lib/marty/relation.rb
marty-9.3.3 lib/marty/relation.rb
marty-9.3.2 lib/marty/relation.rb
marty-9.3.0 lib/marty/relation.rb
marty-8.5.0 lib/marty/relation.rb
marty-8.4.1 lib/marty/relation.rb
marty-8.3.1 lib/marty/relation.rb
marty-8.2.0 lib/marty/relation.rb
marty-8.0.0 lib/marty/relation.rb
marty-6.1.0 lib/marty/relation.rb
marty-5.2.0 lib/marty/relation.rb
marty-5.1.4 lib/marty/relation.rb