Class: RailsErdViewer::SchemaGenerator
- Inherits:
-
Object
- Object
- RailsErdViewer::SchemaGenerator
- Defined in:
- lib/rails_erd_viewer/schema_generator.rb
Overview
ERDGenerator
Class Method Summary collapse
- .add_column_to_file(file, column, column_end) ⇒ Object
- .add_columns_to_file(file, columns) ⇒ Object
- .add_foreign_keys_to_file(file, table_name) ⇒ Object
- .add_table_to_file(file, table_name, columns) ⇒ Object
- .column_type_map ⇒ Object
- .generate ⇒ Object
- .get_column_type(column) ⇒ Object
Class Method Details
.add_column_to_file(file, column, column_end) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 44 def self.add_column_to_file(file, column, column_end) type = get_column_type(column) null_constraint = column.null ? '' : ' NOT NULL' default_constraint = column.default.nil? ? '' : " DEFAULT #{column.default.inspect}" file.write " #{column.name} #{type}#{null_constraint}#{default_constraint}#{column_end}\n" end |
.add_columns_to_file(file, columns) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 36 def self.add_columns_to_file(file, columns) columns_count = columns.size columns.each_with_index do |column, index| column_end = index == columns_count - 1 ? '' : ',' add_column_to_file(file, column, column_end) end end |
.add_foreign_keys_to_file(file, table_name) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 26 def self.add_foreign_keys_to_file(file, table_name) foreign_keys = ActiveRecord::Base.connection.foreign_keys(table_name) foreign_keys.each do |fk| file.write( "ALTER TABLE #{fk.from_table} ADD FOREIGN KEY (#{fk.column}) " \ "REFERENCES #{fk.to_table} (#{fk.primary_key});\n" ) end end |
.add_table_to_file(file, table_name, columns) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 19 def self.add_table_to_file(file, table_name, columns) file.write "CREATE TABLE #{table_name} (\n" add_columns_to_file(file, columns) file.write " PRIMARY KEY (id)\n" if columns.any? { |c| c.name == 'id' } file.write ");\n\n" end |
.column_type_map ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 57 def self.column_type_map { string: 'varchar(255)', text: 'text', integer: 'integer(11)', float: 'float', decimal: 'decimal(10,2)', datetime: 'datetime', boolean: 'boolean', date: 'date', time: 'time' } end |
.generate ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 6 def self.generate file_path = Rails.root.join('tmp/application_schema.sql') File.open(file_path, 'w') do |file| ActiveRecord::Base.connection.tables.each do |table_name| columns = ActiveRecord::Base.connection.columns(table_name) add_table_to_file(file, table_name, columns) add_foreign_keys_to_file(file, table_name) end end end |
.get_column_type(column) ⇒ Object
53 54 55 |
# File 'lib/rails_erd_viewer/schema_generator.rb', line 53 def self.get_column_type(column) column_type_map[column.type] || 'unknown' end |