Module: IdsOfAllDependencies

Defined in:
lib/ids_of_all_dependencies.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_model(name) ⇒ Object



4
5
6
# File 'lib/ids_of_all_dependencies.rb', line 4

def self.get_model(name)
  self.subclasses.find{ |m| m.name == name.to_s.camelcase }
end

Instance Method Details

#ids_of_all_dependencies(to_filter = nil, filtering_strategy = :with_parents) ⇒ Object



8
9
10
# File 'lib/ids_of_all_dependencies.rb', line 8

def ids_of_all_dependencies(to_filter=nil, filtering_strategy=:with_parents)
  ids_of_all_dependencies_with_filtered(to_filter, filtering_strategy)[:main]
end

#ids_of_all_dependencies_with_filtered(to_filter = nil, filtering_strategy = :with_parents) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ids_of_all_dependencies.rb', line 12

def ids_of_all_dependencies_with_filtered(to_filter=nil, filtering_strategy=:with_parents)
  id_hash = ids_of_all_dependencies_without_reflection(to_filter || {}, filtering_strategy)
  move_wrongly_assigned_to_main(to_filter, id_hash) if to_filter && filtering_strategy == :with_parents
  id_hash[:main].sort_arrays!
  id_hash[:filtered_out].sort_arrays!
  id_hash
end

#ids_of_all_dependencies_without_reflection(to_filter, filtering_strategy = :with_parents) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ids_of_all_dependencies.rb', line 20

def ids_of_all_dependencies_without_reflection(to_filter, filtering_strategy=:with_parents)
  result = { main: IdHash.new, filtered_out: IdHash.new }
  self_symbol = self.class.name.underscore.to_sym

  self.class.reflect_on_all_associations.map do |association|
    next if association.macro == :belongs_to
    symbol = association.klass.name.underscore.to_sym
    context = { to_filter: to_filter, self_symbol: self_symbol, association: association, strategy: filtering_strategy }

    self.send(association.name).map do |associated_object|
      hash_to_use = get_hash_to_use(result, **context, object: associated_object)
      hash_to_use.add(symbol, associated_object.id)
    end

    result = get_result_with_grandchildren_hashes(result, context)
  end

  result
end