-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.rb
executable file
·74 lines (63 loc) · 2.04 KB
/
generate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
68
69
70
71
72
73
#!/usr/bin/env ruby
if ARGV[0].nil? or not ['model', 'migration'].include?(ARGV[0])
puts "Usage:"
puts "#{__FILE__} model ModelName|model_name"
puts "#{__FILE__} migration SomeChangeToSomeTable|some_change_to_some_table"
exit
end
require 'rubygems'
require 'active_support/core_ext/string/inflections'
include ActiveSupport::CoreExtensions::String::Inflections
# setup
option = ARGV[0]
name = ARGV[1]
name = name.camelize
Dir.mkdir('migrate') unless File.exists?('migrate')
# get the new_version string
versions = Dir.entries("migrate/").map { |file| file.to_i }
last_version = versions.sort.last
new_version = sprintf("%03d", (last_version + 1))
if option == 'model'
# create the model file
puts "Creating: #{name}.rb"
File.open("#{name}.rb", 'w') do |file|
file.write "class #{name} < ActiveRecord::Base\nend"
end
# create the model proxy file
puts "Creating: #{name}Proxy.rb"
File.open("#{name}Proxy.rb", 'w') do |file|
file.write "class #{name}Proxy < OSX::ActiveRecordProxy\nend"
end
# create the migration file
filename = "migrate/#{new_version}_create_#{name.tableize}.rb"
puts "Creating: #{filename}"
File.open(filename, 'w') do |file|
str = "class Create#{name.pluralize} < ActiveRecord::Migration\n"
str += " def self.up\n"
str += " create_table :#{name.tableize} do |t|\n"
str += " # t.column :title, :string, :default => 'foo'\n"
str += " end\n"
str += " end\n"
str += "\n"
str += " def self.down\n"
str += " drop_table :#{name.tableize}\n"
str += " end\n"
str += "end"
file.write str
end
elsif option == 'migration'
# create the migration file
filename = "migrate/#{new_version}_#{name.underscore}.rb"
puts "Creating: #{filename}"
File.open(filename, 'w') do |file|
str = "class #{name} < ActiveRecord::Migration\n"
str += " def self.up\n"
str += " # add_column :table, :column, :type\n"
str += " end\n"
str += "\n"
str += " def self.down\n"
str += " end\n"
str += "end"
file.write str
end
end