Sha256: 4adc12be9a8dde4560cd40c91ef3f1557e1a4efa9edf3974c271eeb9f3c0a096

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module NullifyDependencies
  def nullify_dependencies(dependencies_symbols_to_nullify)
    dependencies_symbols_to_nullify.map do |symbol|
      dependencies = self.send(symbol) # e.g. build.tags_for_that_this_build_is_last

      dependencies.map do |entry|
        foreign_key = self.class.reflect_on_association(symbol).foreign_key.to_sym
        entry.update(foreign_key => nil) # e.g. tag.update(last_build_id: nil)
        {
          related_table: entry.class.table_name,
          foreign_key: foreign_key,
          parent_id: self.id,
          related_id: entry.id
        }
      end
    end.flatten
  end
  
  def nullify_all_dependencies
    nullify_dependencies(symbols_of_all_direct_dependencies)
  end

  def nullify_default_dependencies
    nullify_dependencies(default_dependencies_symbols_to_nullify)
  end

  def default_dependencies_symbols_to_nullify
    self.class.default_dependencies_symbols_to_nullify
  end

  def self.default_dependencies_symbols_to_nullify
    raise "self.default_dependencies_symbols_to_nullify not implemented in the #{self.class} class"
  end

  def default_dependencies_to_nullify
    default_dependencies_symbols_to_nullify.map do |symbol|
      self.send(symbol).to_a
    end.flatten(1)
  end

  def symbols_of_all_direct_dependencies
    self.class.reflect_on_all_associations.map do |association|
      next if association.macro == :belongs_to

      association.name
    end.compact
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dependency-trees-0.0.8 lib/nullify_dependencies.rb
dependency-trees-0.0.7 lib/nullify_dependencies.rb
dependency-trees-0.0.6 lib/nullify_dependencies.rb
dependency-trees-0.0.5 lib/nullify_dependencies.rb