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
74
75
|
# File 'lib/generators/steroid/model/model_generator.rb', line 10
def add_model
say "Applying steroid: Model", [:bold, :magenta]
cmd = ["rails generate model"]
prompt = TTY::Prompt.new
model_name = prompt.ask("\nWhat is the great name of your model?") do |q|
q.required true
q.modify :remove
end
cmd << model_name
boolean_choices = [{name: "yes", value: true}, {name: "no", value: false}]
columns = []
while prompt.select("\nWould you like to add model attributes(columns)?", boolean_choices)
column_name = prompt.ask("Specify name of column:") do |q|
q.required true
q.modify :remove
end
column_type = prompt.select("Choose type of column:", %w(references integer decimal float boolean binary string text date time datetime primary_key digest token))
if column_type == 'references'
column_type = "#{column_type}{polymorphic}" if prompt.select("Polymorphic association?", boolean_choices)
end
if %w(integer string text binary).include?(column_type)
if prompt.select("Set limit?", boolean_choices)
limit = prompt.ask("Specify limit:") do |q|
q.required true
q.modify :remove
q.convert(:int, "Invalid input! Please provide integer value.")
end
column_type = "#{column_type}{#{limit}}"
end
end
if column_type == 'decimal'
if prompt.select("Set precision & scale?", boolean_choices)
precision = prompt.ask("Specify precision:") do |q|
q.required true
q.modify :remove
q.convert(:int, "Invalid input! Please provide integer value.")
end
scale = prompt.ask("Specify scale:") do |q|
q.required true
q.modify :remove
q.convert(:int, "Invalid input! Please provide integer value.")
end
column_type = "'#{column_type}{#{precision},#{scale}}'"
end
end
index_option = nil
if prompt.select("Add index?", boolean_choices)
index_option = prompt.select("Unique index?", boolean_choices) ? 'uniq' : 'index'
end
columns << [column_name, column_type, index_option].compact.join(':')
end
cmd += columns
cmd << "--no-migration" if prompt.select("\nSkip migration?", boolean_choices)
cmd << "--no-timestamps" if prompt.select("\nSkip created_at, updated_at timestamps?", boolean_choices)
cmd << "--no-indexes" if prompt.select("\nSkip indexes?", boolean_choices)
run cmd.join(" ")
end
|