Class: DroppableTable::ModelCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/droppable_table/model_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelCollector



9
10
11
12
# File 'lib/droppable_table/model_collector.rb', line 9

def initialize
  @models = []
  @table_mapping = {} # model_class => table_name
end

Instance Attribute Details

#modelsObject (readonly)

Returns the value of attribute models.



7
8
9
# File 'lib/droppable_table/model_collector.rb', line 7

def models
  @models
end

#table_mappingObject (readonly)

Returns the value of attribute table_mapping.



7
8
9
# File 'lib/droppable_table/model_collector.rb', line 7

def table_mapping
  @table_mapping
end

Instance Method Details

#collectObject



14
15
16
17
18
19
20
21
# File 'lib/droppable_table/model_collector.rb', line 14

def collect
  ensure_rails_loaded
  eager_load_all_models
  collect_all_descendants
  build_table_mapping

  self
end

#habtm_tablesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/droppable_table/model_collector.rb', line 42

def habtm_tables
  habtm_join_tables = Set.new

  models.each do |model|
    next if model.abstract_class?

    # Find HABTM associations
    model.reflect_on_all_associations(:has_and_belongs_to_many).each do |association|
      join_table = association.join_table
      habtm_join_tables << join_table if join_table
    end
  rescue StandardError
    # Skip models that can't be inspected
  end

  habtm_join_tables
end

#sti_base_tablesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/droppable_table/model_collector.rb', line 27

def sti_base_tables
  sti_tables = Set.new

  models.each do |model|
    next if model.abstract_class?

    # Check if this model uses STI (has a 'type' column)
    sti_tables << model.table_name if model.columns_hash.key?("type") && model.base_class == model
  rescue StandardError
    # Skip models that can't be inspected
  end

  sti_tables
end

#table_namesObject



23
24
25
# File 'lib/droppable_table/model_collector.rb', line 23

def table_names
  Set.new(table_mapping.values)
end