Method: ArModelGenerator.create_models

Defined in:
lib/ar-model-generator.rb

.create_models(namespace, connection) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ar-model-generator.rb', line 37

def create_models(namespace, connection)
  path = "app/models/#{namespace}/"
  namespace_up = namespace.classify
  FileUtils.mkdir_p path

  # Base-File
  base_path = path + "base.rb"
  File.open(base_path , "w+") do |f|
    f.write "class #{namespace_up}::Base < ActiveRecord::Base\n  self.abstract_class = true\n  establish_connection(#{connection.inspect}) end\n"
  end
  $stdout.puts h.color("Writing #{base_path}", :green)

  ActiveRecord::Base.connection.tables.each do |table_name|
    table_up = table_name.classify
    string = "class #{namespace_up}::#{table_up} < #{namespace_up}::Base\n  self.table_name = '#{table_name}'"

    if table_has_type_column?(table_name)
      $stderr.puts "Table #{table_name} has column :type. Adding inheritance_column."
      string += "\n  self.inheritance_column = :sti_type"
    end
    string += add_table_id_column(table_name)
    string += "\nend\n"

    target_path = path + "#{table_up.underscore}.rb"
    $stdout.puts h.color("Writing #{namespace_up}::#{table_up} to #{target_path}", :green)
    File.open(target_path, "w+") { |f|
      f.write string
    }
  end
end