rails_steroids 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 656c3847e194bf888f0b4c12482d3d7ba06200ab1af54d433bada03401d84398
4
- data.tar.gz: 7bd0c50707fb1e44a256f09c5645cc1e8410bc0f7f131c20ea8f4cf70f22431b
3
+ metadata.gz: 9e8022f4d9c1864d8fb7886a799f32b7f8d0f7db85152a7f29db9810ce104367
4
+ data.tar.gz: 04571a62c83ac2d43d3294189265a9563e351fd43350dea47c413e2738d1b76e
5
5
  SHA512:
6
- metadata.gz: cce4f6c5f46464d33fb3c9e8721a5d6db48595be7369bba3487882ee3c98c77fa98b57f653d6490254ad0815fed235f0b8c37534e4d4415b0d69d8bb7423bc73
7
- data.tar.gz: 98e2c27907407481adf9c168116b2cc5e46c992d9c5c12d2f5892f30e41cfe38c9eba051b4208bdef7ba9c8fed0b8e19b5d82f6435335f62fb2497a36f18d19d
6
+ metadata.gz: 30fb811636d668a114df763e0f0eb50dffae19cef6ab6e2246a12772cfbabd8e6f3f4d8b66e948bbcd80f646d9233aca061259456b39712c16ecec78b91f36bd
7
+ data.tar.gz: 902918a36f478d5da29a785da787ab4eaf19896354f6c24fe2ca3143aba778b07535f6a689902fea8042085220f22b5e270a846f7f0c50cf37d9d42799e8dbb9
data/CHANGELOG.md CHANGED
@@ -1,8 +1,20 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.7.0] - 2024-02-08
4
+
5
+ - Improvement in steroid recipe: migration
6
+ - Create new migration interactively for modify table with
7
+ * add_column, remove_column
8
+ * add_index, remove_index
9
+ * add_remove, remove_reference
10
+
3
11
  ## [0.6.0] - 2024-02-07
4
12
 
5
- - New steroid recipe: migration (Create new migration interactively)
13
+ - New steroid recipe: migration
14
+ - Create new migration interactively for
15
+ * Create table
16
+ * Create join table
17
+ * Drop table
6
18
 
7
19
  ## [0.5.0] - 2024-02-06
8
20
 
@@ -19,3 +19,7 @@ What will this do?:
19
19
  - index and unique index
20
20
  - id column, custom join table name for join table creation
21
21
  - Addition to original migration generator is we can set default value as well
22
+ * Modify table with
23
+ - add_column, remove_column
24
+ - add_index, remove_index
25
+ - add_remove, remove_reference
@@ -15,25 +15,19 @@ module Steroid
15
15
  def add_migration
16
16
  say "Injecting steroid: Migration", :green
17
17
  cmd = ["rails generate migration"]
18
- prompt = TTY::Prompt.new
19
- boolean_choices = [{name: "yes", value: true}, {name: "no", value: false}]
20
18
 
21
19
  action_choices = [
22
20
  {name: 'Create table', value: 'create_table'},
23
21
  {name: 'Create join table', value: 'create_join_table'},
24
22
  {name: 'Drop table', value: 'drop_table'},
25
- # {name: 'Modify table columns/index', value: 'modify_table'},
26
- # {name: 'Add column', value: 'add_column'},
27
- # {name: 'Remove column', value: 'remove_column'},
28
- # {name: 'Add index', value: 'add_index'},
29
- # {name: 'Remove index', value: 'remove_index'},
23
+ {name: 'Modify table columns/index', value: 'modify_table'},
30
24
  ]
31
- action = prompt.select("What would you like to do?", action_choices)
25
+ @action = prompt.select("What would you like to do?", action_choices)
32
26
 
33
- case action
27
+ case @action
34
28
  when 'create_table'
35
29
  table_name = prompt.ask("What is the name of table to be created?", required: true) { |q| q.modify :remove }
36
- @content = content_for_create_table(table_name, collect_columns_data, need_timestamps?)
30
+ @content = content_for_create_table(table_name, collect_columns_data)
37
31
  migration_template "migration.rb", "#{db_migrate_path}/create_#{table_name}.rb"
38
32
  when 'create_join_table'
39
33
  table1_name = prompt.ask("What is the name of first table to be joined?", required: true) { |q| q.modify :remove }
@@ -51,48 +45,81 @@ module Steroid
51
45
  end
52
46
  if prompt.select("Add composite index for #{table1_name} and #{table2_name} foreign_key?", boolean_choices)
53
47
  uniq_index_option = prompt.select("Unique combination index?", boolean_choices) ? {meta: {unique: true}} : {}
54
- columns_data << {name: "[:#{table2_name.singularize}_id, :#{table2_name.singularize}_id]", type: 'index'}.merge(uniq_index_option)
48
+ columns_data << {name: ["#{table2_name.singularize}_id", "#{table2_name.singularize}_id"], type: 'index'}.merge(uniq_index_option)
55
49
  end
56
- @content = content_for_create_join_table(table1_name, table2_name, table_name, (columns_data + collect_columns_data), need_timestamps?)
50
+ @content = content_for_create_join_table(table1_name, table2_name, table_name, (columns_data + collect_columns_data))
57
51
  migration_template "migration.rb", "#{db_migrate_path}/create_join_table_#{table_name}.rb"
58
52
  when 'drop_table'
59
53
  table_name = prompt.ask("What is the name of table to be dropped?", required: true) { |q| q.modify :remove }
60
54
  @content = content_for_drop_table(table_name)
61
55
  migration_template "migration.rb", "#{db_migrate_path}/drop_#{table_name}.rb"
62
- # when 'modify_table'
63
- # table_name = prompt.ask("What is the name of table to add/remove columns?", required: true) { |q| q.modify :remove }
64
- # columns_to_be_added = collect_columns_data
65
- # columns_to_be_removed
66
- # migration_template "migration.rb", "#{db_migrate_path}/drop_#{table_name}.rb"
67
- # when 'add_column'
68
- # when 'remove_column'
69
- # when 'add_index'
70
- # when 'remove_index'
56
+ when 'modify_table'
57
+ table_name = prompt.ask("What is the name of table to add/remove columns/index?", required: true) { |q| q.modify :remove }
58
+ modify_table_choices = [
59
+ {name: 'Add column', value: 'add_column'}, {name: 'Remove column', value: 'remove_column'},
60
+ {name: 'Add index', value: 'add_index'}, {name: 'Remove index', value: 'remove_index'},
61
+ {name: 'Add reference', value: 'add_reference'}, {name: 'Remove reference', value: 'remove_reference'},
62
+ # {name: 'Add timestamps', value: 'add_timestamps'}, {name: 'Remove timestamps', value: 'remove_timestamps'},
63
+ {name: 'Exit', value: 'exit'}
64
+ ]
65
+ @content = []
66
+ file_name = []
67
+ while (modify_table_action = prompt.select("What would you like to do?", modify_table_choices)) != 'exit'
68
+ for_removal = modify_table_action.start_with?('remove_')
69
+ if modify_table_action.end_with?('_index') || modify_table_action.end_with?('_reference')
70
+ data = modify_table_action.end_with?('_index') ? ask_index_data(for_removal: for_removal) : ask_reference_data(for_removal: for_removal)
71
+ file_name << modify_table_action << data[:name]
72
+ else
73
+ data = ask_column_data(for_removal: for_removal)
74
+ file_name << (for_removal ? 'remove' : 'add') << data[:name]
75
+ end
76
+ @content << format_statement_for_modify_table(modify_table_action, table_name, data)
77
+ end
78
+ @content = content_for_modify_table(@content.join("\n "))
79
+ migration_template "migration.rb", "#{db_migrate_path}/#{file_name.join('_')}_in_#{table_name}.rb"
71
80
  end
72
81
  end
73
82
 
74
83
  private
75
84
 
85
+ def prompt
86
+ TTY::Prompt.new
87
+ end
88
+
89
+ def boolean_choices
90
+ [{name: "yes", value: true}, {name: "no", value: false}]
91
+ end
92
+
76
93
  def collect_columns_data
77
- prompt = TTY::Prompt.new
78
- boolean_choices = [{name: "yes", value: true}, {name: "no", value: false}]
79
- columns = []
94
+ columns_data = []
80
95
  while prompt.select("Would you like to add model attributes(columns)?", boolean_choices)
81
- columns_data = { meta: {} }
82
- column_name = prompt.ask("Specify name of column:", required: true) { |q| q.modify :remove }
96
+ columns_data << ask_column_data
97
+ end
98
+ timestamps_data = ask_timestamps_data
99
+ columns_data << timestamps_data if timestamps_data
100
+ columns_data
101
+ end
83
102
 
84
- column_type = prompt.select("Choose type of column:", %w(references boolean string text integer decimal float binary date time datetime primary_key digest token))
103
+ def ask_column_data(for_removal: false)
104
+ column_data = { meta: {} }
105
+ column_name = prompt.ask("Specify name of column:", required: true) { |q| q.modify :remove }
85
106
 
86
- if column_type == 'references'
87
- columns_data[:meta][:polymorphic] = true if prompt.select("Polymorphic association?", boolean_choices)
88
- end
107
+ column_type_choices = %w(boolean string text integer decimal float binary date time datetime primary_key digest token)
108
+ column_type_choices << 'references' unless @action == 'modify_table'
109
+ column_type = prompt.select("Choose type of column:", column_type_choices)
110
+
111
+ if column_type == 'references'
112
+ column_data[:meta][:foreign_key] = true if prompt.select("Foreign_key to be #{for_removal ? 'removed' : 'added'}?", boolean_choices)
113
+ column_data[:meta][:polymorphic] = true if prompt.select("Polymorphic association?", boolean_choices)
114
+ end
115
+ unless for_removal
89
116
  if %w(integer string text binary).include?(column_type)
90
117
  if prompt.select("Set limit?", boolean_choices)
91
118
  limit = prompt.ask("Specify limit:", required: true) do |q|
92
119
  q.modify :remove
93
120
  q.convert(:int, "Invalid input! Please provide integer value.")
94
121
  end
95
- columns_data[:meta][:limit] = limit
122
+ column_data[:meta][:limit] = limit
96
123
  end
97
124
  end
98
125
  if column_type == 'decimal'
@@ -101,66 +128,106 @@ module Steroid
101
128
  q.modify :remove
102
129
  q.convert(:int, "Invalid input! Please provide integer value.")
103
130
  end
104
- columns_data[:meta][:precision] = precision
131
+ column_data[:meta][:precision] = precision
105
132
  scale = prompt.ask("Specify scale:", required: true) do |q|
106
133
  q.modify :remove
107
134
  q.convert(:int, "Invalid input! Please provide integer value.")
108
135
  end
109
- columns_data[:meta][:scale] = scale
136
+ column_data[:meta][:scale] = scale
110
137
  end
111
138
  end
112
139
 
113
140
  if %w(references primary_key digest token).exclude?(column_type) && prompt.select("Add default value?", boolean_choices)
114
141
  acceptable_value_type = { 'text' => :string }[column_type] || column_type.to_sym
115
- columns_data[:meta][:default] = prompt.ask("Specify default value:", required: true) do |q|
142
+ column_data[:meta][:default] = prompt.ask("Specify default value:", required: true) do |q|
116
143
  q.modify :remove
117
144
  q.convert(acceptable_value_type, "Invalid input! Please provide %{type} value.")
118
145
  end
119
146
  end
120
147
 
121
148
  if prompt.select("Add index?", boolean_choices)
122
- columns_data[:meta][:index] = prompt.select("Unique index?", boolean_choices) ? :unique : true
149
+ column_data[:meta][:index] = prompt.select("Unique index?", boolean_choices) ? :unique : true
123
150
  end
151
+ end
152
+
153
+ column_data.merge!(name: column_name, type: column_type)
154
+ column_data
155
+ end
156
+
157
+ def ask_reference_data(for_removal: false)
158
+ column_data = { meta: {} }
159
+ column_data[:name] = prompt.ask("Specify name of reference:", required: true) { |q| q.modify :remove }
160
+ column_data[:meta][:foreign_key] = true if prompt.select("Foreign_key to be #{for_removal ? 'removed' : 'added'}?", boolean_choices)
161
+ column_data[:meta][:polymorphic] = true if prompt.select("Polymorphic association?", boolean_choices)
162
+ column_data
163
+ end
124
164
 
125
- columns_data.merge!(name: column_name, type: column_type)
126
- columns << columns_data
165
+ def ask_index_data(for_removal: false)
166
+ column_data = { meta: {} }
167
+ names = []
168
+ names << prompt.ask("Specify name of column:", required: true) { |q| q.modify :remove }
169
+ while prompt.select("Add more columns for Composite index?", boolean_choices)
170
+ names << prompt.ask("Specify name of another column:", required: true) { |q| q.modify :remove }
171
+ end
172
+ column_data[:name] = names
173
+ unless for_removal
174
+ column_data[:meta][:unique] = true if prompt.select("Unique index?", boolean_choices)
127
175
  end
128
- columns
176
+ custom_name = prompt.ask("Specify custom name for index (optional):") { |q| q.modify :remove }
177
+ column_data[:meta][:name] = custom_name if custom_name.present?
178
+ column_data
129
179
  end
130
180
 
131
- def need_timestamps?
132
- prompt = TTY::Prompt.new
133
- boolean_choices = [{name: "yes", value: true}, {name: "no", value: false}]
134
- prompt.select("Add timestamps?", boolean_choices)
181
+ def ask_timestamps_data
182
+ if prompt.select("Add timestamps?", boolean_choices)
183
+ { type: 'timestamps' }
184
+ end
135
185
  end
136
186
 
137
- def content_for_create_table(table_name, columns_data, need_timestamps)
138
- columns_data_content = columns_data.map do |c|
139
- column_row = ["t.#{c[:type]} :#{c[:name]}"]
140
- c[:meta].each { |key, value| column_row << "#{key}: #{value.inspect}" }
141
- column_row.join(', ')
187
+ def format_statement_for_modify_table(modify_table_action, table_name, column_data)
188
+ statement_data = ["#{modify_table_action} :#{table_name}"]
189
+ statement_data << if column_data[:name].is_a?(Array)
190
+ col_name = column_data[:name].size > 1 ? "[#{column_data[:name].map { |n| ":#{n}" }.join(', ')}]" : ":#{column_data[:name].first}"
191
+ "column: #{col_name}" if modify_table_action == 'remove_index'
192
+ else
193
+ ":#{column_data[:name]}"
142
194
  end
143
- columns_data_content << "t.timestamps" if need_timestamps
195
+ statement_data << ":#{column_data[:type]}" if column_data[:type]
196
+ column_data[:meta].each { |key, value| statement_data << "#{key}: #{value.inspect}" }
197
+ statement_data = statement_data.join(', ')
198
+ end
199
+
200
+ def format_statement_for_create_table(columns_data)
201
+ columns_data.map do |c|
202
+ column_row = if c[:name].is_a?(Array)
203
+ ["t.#{c[:type]} [#{c[:name].map { |n| ":#{n}" }.join(', ')}]"]
204
+ elsif c[:name].nil?
205
+ ["t.#{c[:type]}"]
206
+ else
207
+ ["t.#{c[:type]} :#{c[:name]}"]
208
+ end
209
+ c[:meta].each { |key, value| column_row << "#{key}: #{value.inspect}" } if c[:meta]
210
+ column_row.join(', ')
211
+ end.join("\n ")
212
+ end
213
+
214
+ def content_for_create_table(table_name, columns_data)
215
+ columns_data_content = format_statement_for_create_table(columns_data)
144
216
  <<-CONTENT
145
217
  def change
146
218
  create_table :#{table_name}#{primary_key_type} do |t|
147
- #{columns_data_content.join("\n ")}
219
+ #{columns_data_content}
148
220
  end
149
221
  end
150
222
  CONTENT
151
223
  end
152
224
 
153
- def content_for_create_join_table(table1_name, table2_name, table_name, columns_data, need_timestamps)
154
- columns_data_content = columns_data.map do |c|
155
- column_row = ["t.#{c[:type]} :#{c[:name]}"]
156
- c[:meta]&.each { |key, value| column_row << "#{key}: #{value.inspect}" }
157
- column_row.join(', ')
158
- end
159
- columns_data_content << "t.timestamps" if need_timestamps
225
+ def content_for_create_join_table(table1_name, table2_name, table_name, columns_data)
226
+ columns_data_content = format_statement_for_create_table(columns_data)
160
227
  <<-CONTENT
161
228
  def change
162
229
  create_join_table :#{table1_name}, :#{table2_name}, table_name: :#{table_name} do |t|
163
- #{columns_data_content.join("\n ")}
230
+ #{columns_data_content}
164
231
  end
165
232
  end
166
233
  CONTENT
@@ -174,5 +241,13 @@ module Steroid
174
241
  CONTENT
175
242
  end
176
243
 
244
+ def content_for_modify_table(statements)
245
+ <<-CONTENT
246
+ def change
247
+ #{statements}
248
+ end
249
+ CONTENT
250
+ end
251
+
177
252
  end
178
253
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSteroids
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_steroids
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anand Bait
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails