Class: MagicModelsGenerator::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_model_generator/schema.rb

Constant Summary collapse

@@models =
nil

Class Method Summary collapse

Class Method Details

.associations(klass) ⇒ Object

Used by the generator to access the association code lines for the generated class



105
106
107
108
109
# File 'lib/magic_model_generator/schema.rb', line 105

def associations(klass)
  @@belongs_to_associations[klass.to_s] +
  @@has_some_associations[klass.to_s] +
  @@has_many_through_associations[klass.to_s]
end

.fks_on_table(table_name) ⇒ Object



32
33
34
35
# File 'lib/magic_model_generator/schema.rb', line 32

def fks_on_table(table_name)
  load_schema if @@models.nil?
  @@fks_by_table[table_name.to_s] || []
end

active record only support 2 column link tables, otherwise use a model table, has_many and through

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/magic_model_generator/schema.rb', line 38

def is_link_table?(table_name)
  load_schema if @@models.nil?
  return @@link_tables[table_name] if ! @@link_tables[table_name].nil?
  column_names = @conn.columns(table_name).map{|x| x.name }
  @@link_tables[table_name] = ! column_names.include?("id") && column_names.length == 2 && column_names.select { |x| x =~ /_id$/ } == column_names
  return @@link_tables[table_name]
end


46
47
48
# File 'lib/magic_model_generator/schema.rb', line 46

def link_tables_for_class(klass)
  load_schema if @@models.nil?
end

.load_schemaObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/magic_model_generator/schema.rb', line 50

def load_schema
  return if ! @@models.nil?
  @@superklass ||= ActiveRecord::Base
  raise "No database connection" if !(@conn = @@superklass.connection)

  @@models = ModelHash.new
  @@tables = Hash.new
  @@fks_by_table = Hash.new
  @@link_tables = Hash.new
  @@belongs_to_associations = Hash.new
  @@has_some_associations = Hash.new
  @@has_many_through_associations = Hash.new

  @@table_names = @conn.tables.sort

  # Work out which tables are in the model and which aren't
  @@table_names.each do |table_name|

    # a model table then...
    model_class_name = ActiveRecord::Base.class_name(table_name)
    logger.debug "Got a model table: #{table_name} => class #{model_class_name}"

    @@models[model_class_name] = table_name
    @@tables[table_name] = model_class_name

    # Process FKs?
    if @conn.supports_fetch_foreign_keys?
      table_names.each do |table_name|
        logger.debug "Getting FKs for #{table_name}"
        @@fks_by_table[table_name] = Array.new
        @conn.foreign_key_constraints(table_name).each do |fk|
          logger.debug "Got one: #{fk}"
          @@fks_by_table[table_name].push(fk)
        end # do each fk
      end # each table
    end
  end
  
  models.each do |model_name, table_name|
    @@belongs_to_associations[model_name] = []
    @@has_some_associations[model_name] = []
    @@has_many_through_associations[model_name] = []
  end
  
  logger.debug "Loaded all models, now generating associations..."
  
  models.keys.sort.each do |model_name|
    logger.debug "Generating for #{model_name}..."
    table_name = models[model_name]
    generate_associations(model_name, table_name)
  end

end

.loggerObject



13
14
15
# File 'lib/magic_model_generator/schema.rb', line 13

def logger
  @@logger ||= MagicModelsGenerator.logger
end

.modelsObject



17
18
19
20
# File 'lib/magic_model_generator/schema.rb', line 17

def models
  load_schema if @@models.nil?
  @@models
end

.table_namesObject



27
28
29
30
# File 'lib/magic_model_generator/schema.rb', line 27

def table_names
  load_schema if @@table_names.nil?
  @@table_names
end

.tablesObject



22
23
24
25
# File 'lib/magic_model_generator/schema.rb', line 22

def tables
  load_schema if @@tables.nil?
  @@tables
end