Module: NullifyDependencies

Included in:
Model
Defined in:
lib/nullify_dependencies.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_dependencies_symbols_to_nullifyObject



33
34
35
# File 'lib/nullify_dependencies.rb', line 33

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

Instance Method Details

#default_dependencies_symbols_to_nullifyObject



29
30
31
# File 'lib/nullify_dependencies.rb', line 29

def default_dependencies_symbols_to_nullify
  self.class.default_dependencies_symbols_to_nullify
end

#default_dependencies_to_nullifyObject



37
38
39
40
41
# File 'lib/nullify_dependencies.rb', line 37

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

#nullify_all_dependenciesObject



21
22
23
# File 'lib/nullify_dependencies.rb', line 21

def nullify_all_dependencies
  nullify_dependencies(symbols_of_all_direct_dependencies)
end

#nullify_default_dependenciesObject



25
26
27
# File 'lib/nullify_dependencies.rb', line 25

def nullify_default_dependencies
  nullify_dependencies(default_dependencies_symbols_to_nullify)
end

#nullify_dependencies(dependencies_symbols_to_nullify) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nullify_dependencies.rb', line 4

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