Class: ModelFromSqlGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/support/model_from_sql/model_from_sql_generator.rb

Instance Method Summary collapse

Instance Method Details

#build_modelsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/support/model_from_sql/model_from_sql_generator.rb', line 4

def build_models
  tables = ActiveRecord::Base.connection.tables

  Rails.application.eager_load!
  in_models = ActiveRecord::Base.descendants.map(&:table_name)
  ap in_models
  new_tables = tables - in_models - ['schema_migrations', 'ar_internal_metadata']

  new_tables.each do |table_name|
    generate "model #{table_name} --skip-migration" # or whatever you want here
    model_file = "app/models/#{table_name.singularize}.rb"
    inject_into_file model_file, before: "end\n" do "self.table_name = \"table_name_replace\"\n    RUBY\n    end\n    gsub_file model_file, 'table_name_replace', table_name\n  end\n\nend\n"