localtower 0.4.2 → 1.0.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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +37 -46
  3. data/app/controllers/localtower/pages_controller.rb +28 -42
  4. data/app/helpers/localtower/application_helper.rb +0 -18
  5. data/app/views/layouts/localtower/application.html.erb +20 -79
  6. data/app/views/localtower/pages/_alert_no_models.html.erb +3 -0
  7. data/app/views/localtower/pages/migrations.html.erb +50 -120
  8. data/app/views/localtower/pages/models.html.erb +53 -68
  9. data/app/views/localtower/pages/new_migration.html.erb +103 -0
  10. data/app/views/localtower/pages/new_model.html.erb +87 -0
  11. data/config/routes.rb +8 -17
  12. data/lib/localtower/generators/migration.rb +53 -181
  13. data/lib/localtower/generators/model.rb +53 -32
  14. data/lib/localtower/generators/service_objects/insert_array.rb +23 -0
  15. data/lib/localtower/generators/service_objects/insert_defaults.rb +15 -43
  16. data/lib/localtower/generators/service_objects/insert_indexes.rb +80 -0
  17. data/lib/localtower/generators/service_objects/insert_nullable.rb +23 -0
  18. data/lib/localtower/status.rb +12 -7
  19. data/lib/localtower/tools.rb +29 -24
  20. data/lib/localtower/version.rb +1 -1
  21. data/lib/localtower.rb +0 -1
  22. data/public/css/app.css +0 -49
  23. data/public/js/app.js +216 -86
  24. data/public/light-bootstrap-dashboard-master/assets/css/light-bootstrap-dashboard.css +9 -35
  25. data/public/screenshots/v1.0.0/migrations.png +0 -0
  26. data/public/screenshots/v1.0.0/models.png +0 -0
  27. data/public/screenshots/v1.0.0/new_migration.png +0 -0
  28. data/public/screenshots/v1.0.0/new_model.png +0 -0
  29. data/public/vendor/font-awesome.min.css +4 -0
  30. data/public/vendor/highlight-js-default.min.css +9 -0
  31. data/public/vendor/highlight.min.js +1198 -0
  32. data/public/{js → vendor}/masonry.pkgd.min.js +0 -0
  33. data/spec/dummy/Gemfile +0 -3
  34. data/spec/dummy/Gemfile.lock +78 -95
  35. data/spec/dummy/app/controllers/pages_controller.rb +0 -2
  36. data/spec/dummy/app/models/post.rb +3 -0
  37. data/spec/dummy/app/models/user.rb +3 -0
  38. data/spec/dummy/app/views/pages/home.html.erb +0 -1
  39. data/spec/dummy/config/database.yml +1 -1
  40. data/spec/dummy/config/environments/development.rb +3 -1
  41. data/spec/dummy/config/puma.rb +1 -1
  42. data/spec/dummy/db/migrate/20230119221452_create_users.rb +14 -0
  43. data/spec/dummy/db/migrate/20230119221751_change_users_at1674166670.rb +7 -0
  44. data/spec/dummy/db/migrate/20230119222054_create_posts.rb +11 -0
  45. data/spec/dummy/db/migrate/20230119222106_change_posts_at1674166865.rb +5 -0
  46. data/spec/dummy/db/schema.rb +41 -0
  47. data/spec/dummy/log/development.log +16030 -10038
  48. data/spec/dummy/log/localtower.log +1878 -439
  49. data/spec/dummy/log/test.log +0 -281
  50. data/spec/dummy/test/index.html +38 -0
  51. data/spec/dummy/tmp/pids/server.pid +1 -1
  52. data/spec/factories/migration.rb +25 -41
  53. data/spec/factories/model.rb +39 -25
  54. data/spec/lib/localtower/generators/migration_spec.rb +36 -63
  55. data/spec/lib/localtower/generators/model_spec.rb +43 -34
  56. data/spec/lib/localtower/generators/service_objects/insert_array_spec.rb +47 -0
  57. data/spec/lib/localtower/generators/service_objects/insert_defaults_spec.rb +30 -35
  58. data/spec/lib/localtower/generators/service_objects/insert_indexes_spec.rb +90 -0
  59. data/spec/lib/localtower/generators/service_objects/insert_nullable_spec.rb +61 -0
  60. data/spec/lib/localtower/tools_spec.rb +1 -11
  61. data/spec/spec_helper.rb +25 -5
  62. metadata +38 -30
  63. data/app/views/localtower/pages/dashboard.html.erb +0 -67
  64. data/app/views/localtower/pages/logs.html.erb +0 -84
  65. data/app/views/localtower/pages/relations.html.erb +0 -66
  66. data/app/views/localtower/pages/tasks.html.erb +0 -48
  67. data/lib/localtower/generators/relation.rb +0 -59
  68. data/lib/localtower/plugins/capture.rb +0 -197
  69. data/lib/tasks/localtower_tasks.rake +0 -4
  70. data/spec/dummy/lib/playground.rb +0 -0
  71. data/spec/dummy/log/localtower_capture_d8979d360722798f7b6eb1a2694a3791.json +0 -1
  72. data/spec/lib/localtower/generators/relation_spec.rb +0 -65
@@ -6,21 +6,15 @@ module Localtower
6
6
 
7
7
  no_commands do
8
8
  def migration_add_column(data)
9
- file = data["last_migration_file"]
10
-
11
- if %w(array).include?(data["column_type"])
12
- line = " add_column :#{data['table_name']}, :#{data['column']}, :text"
9
+ # Special case for array
10
+ if %w[array].include?(data["column_type"])
11
+ line = " add_column :#{data['table_name']}, :#{data['column']}, :string"
13
12
  line << ", default: []"
14
13
  line << ", array: true"
15
-
16
- # Add an gin inde as well:
17
- line << "\n"
18
- line << " add_index :#{data['table_name']}, :#{data['column']}, using: :gin"
19
14
  else
20
15
  line = " add_column :#{data['table_name']}, :#{data['column']}, :#{data['column_type']}"
21
16
  line << ", default: #{data['default']}" if data['default'].present?
22
- line << ", null: false" if not data['nullable']
23
- line << ", index: true" if data['index'] == true
17
+ line << ", null: false" if data['nullable'] == false
24
18
  end
25
19
 
26
20
  line << "\n"
@@ -29,90 +23,68 @@ module Localtower
29
23
  end
30
24
 
31
25
  def migration_remove_column(data)
32
- file = data['last_migration_file']
33
-
34
26
  line = " remove_column :#{data['table_name']}, :#{data['column']}\n"
35
27
 
36
28
  inject_in_migration(file, line)
37
29
  end
38
30
 
39
31
  def migration_rename_column(data)
40
- file = data['last_migration_file']
41
-
42
32
  line = " rename_column :#{data['table_name']}, :#{data['column']}, :#{data['new_column_name']}\n"
43
33
 
44
34
  inject_in_migration(file, line)
45
35
  end
46
36
 
47
37
  def migration_change_column_type(data)
48
- file = data['last_migration_file']
49
-
50
38
  line = " change_column :#{data['table_name']}, :#{data['column']}, :#{data['new_column_type']}\n"
51
39
 
52
40
  inject_in_migration(file, line)
53
41
  end
54
42
 
55
43
  def migration_add_index_to_column(data)
56
- file = data['last_migration_file']
57
-
58
- line = " add_index :#{data["table_name"]}, :#{data["column"]}\n"
44
+ line = " add_index :#{data['table_name']}, :#{data['column']}\n"
59
45
 
60
46
  inject_in_migration(file, line)
61
- end
62
-
63
- # def migration_add_index_to_column_combined(data)
64
- # file = data["last_migration_file"]
65
- # attr_names = data["column"].split(",")
66
47
 
67
- # # add_index :relationships, [:follower_id, :followed_id], unique: true
68
- # line = " add_index :#{data["table_name"]}, [#{attr_names.map { |i| ":#{i}"}.join(', ')}]"
69
- # line << ", unique: true" if data["unique"]
70
- # line << "\n"
71
-
72
- # inject_in_migration(file, line)
73
- # end
48
+ # Custom options:
49
+ params = [data]
50
+ .select { |attr| attr.dig('index', 'using').present? }
51
+ .reject { |attr| attr.dig('index', 'using') == 'none' }
52
+ .each_with_object([]) do |attr, arr|
53
+ arr << Hash[attr['column'], attr['index']]
54
+ end
55
+ ::Localtower::Generators::ServiceObjects::InsertIndexes.new(params).call
56
+ end
74
57
 
75
58
  def migration_remove_index_to_column(data)
76
- file = data["last_migration_file"]
77
-
78
- line = " remove_index :#{data["table_name"]}, :#{data["column"]}\n"
59
+ line = " remove_index :#{data['table_name']}, :#{data['column']}\n"
79
60
 
80
61
  inject_in_migration(file, line)
81
62
  end
82
63
 
83
64
  def migration_belongs_to(data)
84
- file = data["last_migration_file"]
85
-
86
- line = " add_reference :#{data["table_name"]}, :#{data["column"]}, foreign_key: true, index: true\n"
65
+ line = " add_reference :#{data['table_name']}, :#{data['column']}, foreign_key: true, index: true\n"
87
66
 
88
67
  inject_in_migration(file, line)
89
68
 
90
- file = "#{Rails.root}/app/models/#{data["table_name"].underscore.singularize}.rb"
69
+ file = "#{Rails.root}/app/models/#{data['table_name'].underscore.singularize}.rb"
91
70
  after = /class #{data["table_name"].camelize.singularize} < ApplicationRecord\n/
92
- line1 = " belongs_to :#{data["column"].singularize.underscore}\n"
93
-
94
- ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
71
+ line1 = " # belongs_to :#{data['column'].singularize.underscore}\n"
95
72
 
73
+ if File.exist?(file)
74
+ ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
75
+ end
96
76
 
97
- file = "#{Rails.root}/app/models/#{data["column"].underscore.singularize}.rb"
77
+ file = "#{Rails.root}/app/models/#{data['column'].underscore.singularize}.rb"
98
78
  after = /class #{data["column"].camelize.singularize} < ApplicationRecord\n/
99
- line1 = " has_many :#{data["table_name"].pluralize.underscore}\n"
79
+ line1 = " # has_many :#{data['table_name'].pluralize.underscore}\n"
100
80
 
101
- ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
102
- end
103
-
104
- def migration_create_table(data)
105
- file = data["last_migration_file"]
106
-
107
- line = " create_table :#{data["table_name"]}\n"
108
-
109
- inject_in_migration(file, line)
81
+ if File.exist?(file)
82
+ ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
83
+ end
110
84
  end
111
85
 
112
86
  def migration_drop_table(data)
113
- file = data["last_migration_file"]
114
-
115
- line = " drop_table :#{data["table_name"]}, force: :cascade\n"
87
+ line = " drop_table :#{data['table_name']}\n"
116
88
 
117
89
  inject_in_migration(file, line)
118
90
  end
@@ -120,66 +92,42 @@ module Localtower
120
92
  private
121
93
 
122
94
  def inject_in_migration(file, line)
123
- inject_into_file(file, line, after: "def change\n")
95
+ inject_into_file(file, line, before: " end\nend")
96
+ end
97
+
98
+ def file
99
+ Localtower::Tools.last_migration
124
100
  end
125
101
  end
126
102
  end
127
103
 
128
104
  class Migration
129
- TYPES = %w(string datetime text uuid integer float json jsonb decimal binary boolean array references).freeze
130
- ACTIONS = [
131
- 'add_column',
132
- 'remove_column',
133
- 'rename_column',
134
- 'change_column_type',
135
- 'add_index_to_column',
136
- 'belongs_to',
137
- # 'add_index_to_column_combined',
138
- 'remove_index_to_column',
139
- # 'create_table',
140
- 'drop_table',
105
+ TYPES = %w[string datetime date text uuid integer float json jsonb decimal binary boolean array references].freeze
106
+ ACTIONS = %w[
107
+ add_column
108
+ add_index_to_column
109
+ remove_index_to_column
110
+ belongs_to
111
+ remove_column
112
+ change_column_type
113
+ rename_column
114
+ drop_table
141
115
  ].freeze
142
116
 
143
- DEFAULTS = [
144
- "true", "false", "nil"
145
- ]
146
-
147
117
  # @opts =
148
- def initialize(opts)
118
+ def initialize(migrations)
149
119
  @thor = ThorGeneratorMigration.new
150
- @opts = JSON[opts.to_json]
120
+ @migrations = JSON[migrations.to_json]
151
121
  end
152
122
 
153
- def remove_all_migrations
154
- Dir["#{Rails.root}/db/migrate/*"].each { |migration_file| File.delete(migration_file) }
123
+ def run
124
+ model_names = migrations.map { |line| line['table_name'].camelize }.uniq.join
155
125
 
156
- content_schema = """
157
- ActiveRecord::Schema.define(version: 0) do
158
- end
159
- """
126
+ cmd = "Change#{model_names}At#{Time.now.to_i}"
127
+ ::Localtower::Tools.perform_migration(cmd)
160
128
 
161
- File.open("#{Rails.root}/db/schema.rb", "w") do |f|
162
- f.write(content_schema)
163
- end
164
- end
165
-
166
- def run
167
- cmd = "ChangeTheModel#{@opts['migration_name']}AtTime#{Time.now.to_i}"
168
- ::Localtower::Tools.perform_migration(cmd, true)
169
-
170
- @opts['migrations'].each do |action_line|
171
- next unless action_line['action'].present?
172
-
173
- # table_name = action_line['table_name']
174
- # column = action_line['column']
175
- # index = action_line['index']
176
- # default = action_line['default']
177
- # null = action_line['null']
178
- # unique = action_line['unique']
179
- # column_type = action_line['column_type']
180
- # new_column_type = action_line['new_column_type']
181
- # new_column_name = action_line['new_column_name']
182
- action_line["last_migration_file"] = last_migration_file
129
+ migrations.each do |action_line|
130
+ next if action_line['action'].blank?
183
131
 
184
132
  {
185
133
  'add_column' => -> { add_column(action_line) },
@@ -188,126 +136,50 @@ end
188
136
  'change_column_type' => -> { change_column_type(action_line) },
189
137
  'add_index_to_column' => -> { add_index_to_column(action_line) },
190
138
  'belongs_to' => -> { belongs_to(action_line) },
191
- # 'add_index_to_column_combined' => -> { add_index_to_column_combined(action_line) },
192
139
  'remove_index_to_column' => -> { remove_index_to_column(action_line) },
193
- 'create_table' => -> { create_table(action_line) },
194
140
  'drop_table' => -> { drop_table(action_line) }
195
- }[action_line['action'].downcase].call
141
+ }[action_line['action']].call
196
142
  end
197
-
198
- if @opts['run_migrate']
199
- run_migration_or_nil
200
- end
201
- end
202
-
203
- #====================================
204
- def last_migration_file
205
- Dir["#{Rails.root}/db/migrate/*"].sort.last
206
143
  end
207
144
 
208
- # VALIDATION before continuing:
209
145
  private
210
146
 
211
- def add_column(data)
212
- if not data["table_name"].present? \
213
- or not data["column"].present? \
214
- or not data["column_type"].present?
215
- return nil
216
- end
147
+ attr_reader :migrations
217
148
 
149
+ def add_column(data)
218
150
  @thor.migration_add_column(data)
219
151
  end
220
152
 
221
153
  def remove_column(data)
222
- if not data["table_name"].present? \
223
- or not data["column"].present?
224
- return nil
225
- end
226
-
227
154
  @thor.migration_remove_column(data)
228
155
  end
229
156
 
230
157
  def rename_column(data)
231
- if not data["table_name"].present? \
232
- or not data["column"].present? \
233
- or not data["new_column_name"].present?
234
- return nil
235
- end
236
-
237
158
  @thor.migration_rename_column(data)
238
159
  end
239
160
 
240
161
  def change_column_type(data)
241
- if not data["table_name"].present? \
242
- or not data["column"].present? \
243
- or not data["new_column_type"].present?
244
- return nil
245
- end
246
-
247
162
  @thor.migration_change_column_type(data)
248
163
  end
249
164
 
250
165
  def add_index_to_column(data)
251
- if not data["table_name"].present? \
252
- or not data["column"].present?
253
- return nil
254
- end
255
-
256
166
  @thor.migration_add_index_to_column(data)
257
167
  end
258
168
 
259
169
  def belongs_to(data)
260
- if not data["table_name"].present? and data["belongs_to"].present?
261
- return nil
262
- end
263
-
264
170
  data["column"] = data["belongs_to"].underscore.singularize
265
171
  data['column_type'] = "references"
266
172
 
267
173
  @thor.migration_belongs_to(data)
268
174
  end
269
175
 
270
- # def add_index_to_column_combined(data)
271
- # @thor.migration_add_index_to_column_combined(data)
272
- # end
273
-
274
176
  def remove_index_to_column(data)
275
- if not data["table_name"].present? \
276
- or not data["column"].present?
277
- return nil
278
- end
279
-
280
177
  @thor.migration_remove_index_to_column(data)
281
178
  end
282
179
 
283
- def create_table(data)
284
- if not data["table_name"].present?
285
- return nil
286
- end
287
-
288
- @thor.migration_create_table(data)
289
- end
290
-
291
180
  def drop_table(data)
292
- if not data["table_name"].present?
293
- return nil
294
- end
295
-
296
181
  @thor.migration_drop_table(data)
297
182
  end
298
-
299
- def run_migration_or_nil
300
- if last_migration_changed?
301
- ::Localtower::Tools.perform_cmd('rake db:migrate')
302
- else
303
- File.delete(last_migration_file)
304
- nil
305
- end
306
- end
307
-
308
- def last_migration_changed?
309
- File.readlines(last_migration_file).join != ~ /def change\n end/
310
- end
311
183
  end
312
184
  end
313
185
  end
@@ -2,57 +2,78 @@ module Localtower
2
2
  module Generators
3
3
  class Model
4
4
  def initialize(opts)
5
- @opts = JSON[opts.to_json]
5
+ @opts = JSON[opts.to_json] # stringify keys
6
+ @model_name = @opts['model_name']
7
+ @attributes = @opts['attributes']
8
+
9
+ raise "No model_name provided" if @model_name.blank?
10
+ raise "No attributes provided" if @attributes.blank?
6
11
  end
7
12
 
8
13
  def run
9
- if not @opts['attributes'] or not @opts['model_name'].present?
10
- return nil
11
- end
12
-
13
- attributes_list = []
14
-
15
- @opts['attributes'].each do |attribute_data|
16
- str = "#{attribute_data["attribute_name"]}:#{attribute_data["attribute_type"]}"
17
- str << ":index" if attribute_data["index"]
14
+ attributes_str = parse_attributes.join(" ")
15
+ cmd = "rails generate model #{model_name.camelize} #{attributes_str}"
18
16
 
19
- attributes_list << str
20
- end
17
+ ::Localtower::Tools.perform_cmd(cmd)
21
18
 
22
- attributes_str = attributes_list.join(" ")
23
- cmd = "rails g model #{@opts['model_name'].camelize} #{attributes_str}"
19
+ insert_non_nullable_values
20
+ insert_default_values
21
+ insert_array
22
+ insert_indexes
23
+ end
24
24
 
25
- ::Localtower::Tools.perform_cmd(cmd, false)
25
+ private
26
26
 
27
- if defaults_present?
28
- insert_default_values.call
29
- end
27
+ attr_reader :model_name
28
+ attr_reader :attributes
30
29
 
31
- if @opts['run_migrate']
32
- ::Localtower::Tools.perform_cmd('rake db:migrate', false)
33
- # ::Localtower::Tools.perform_cmd('rake db:migrate RAILS_ENV=test', false)
30
+ def parse_attributes
31
+ attributes.map do |attr_hash|
32
+ [
33
+ attr_hash['attribute_name'],
34
+ (attr_hash['attribute_type'] == 'array' ? 'string' : attr_hash['attribute_type']), # we need this transformation of "array"
35
+ (attr_hash.dig('index', 'using') != 'none') ? 'index' : nil
36
+ ].compact.join(':')
34
37
  end
35
-
36
- self
37
38
  end
38
39
 
39
- private
40
+ def insert_array
41
+ params = attributes
42
+ .select { |attr| attr['attribute_type'] == 'array' }
43
+ .map { |attr| attr['attribute_name'] }
40
44
 
41
- def defaults_present?
42
- return false unless @opts['attributes'].any? { |attr| attr["defaults"].present? }
43
- true
45
+ Localtower::Generators::ServiceObjects::InsertArray.new(params).call
44
46
  end
45
47
 
46
- def params_for_defaults
47
- @opts['attributes'].each_with_object([]) do |attr, arr|
48
- arr << Hash[ attr['attribute_name'], attr['defaults'] ] unless attr['defaults'].empty?
48
+ def insert_default_values
49
+ params = attributes
50
+ .reject { |attr| attr['default'] == nil }
51
+ .reject { |attr| attr['default'] == '' }
52
+ .each_with_object([]) do |attr, arr|
53
+ arr << Hash[attr['attribute_name'], attr['default']]
49
54
  end
55
+
56
+ Localtower::Generators::ServiceObjects::InsertDefaults.new(params).call
50
57
  end
51
58
 
52
- def insert_default_values
53
- ::Localtower::Generators::ServiceObjects::InsertDefaults.new(params_for_defaults)
59
+ def insert_non_nullable_values
60
+ params = attributes
61
+ .select { |attr| attr['nullable'] == false }
62
+ .map{ |attr| attr['attribute_name'] }
63
+
64
+ ::Localtower::Generators::ServiceObjects::InsertNullable.new(params).call
54
65
  end
55
66
 
67
+ def insert_indexes
68
+ params = attributes
69
+ .select { |attr| attr.dig('index', 'using').present? }
70
+ .reject { |attr| attr.dig('index', 'using') == 'none' }
71
+ .each_with_object([]) do |attr, arr|
72
+ arr << Hash[attr['attribute_name'], attr['index']]
73
+ end
74
+
75
+ ::Localtower::Generators::ServiceObjects::InsertIndexes.new(params).call
76
+ end
56
77
  end
57
78
  end
58
79
  end
@@ -0,0 +1,23 @@
1
+ module Localtower
2
+ module Generators
3
+ module ServiceObjects
4
+ class InsertArray
5
+ def initialize(attributes)
6
+ @attributes = attributes
7
+ end
8
+
9
+ def call
10
+ attributes.each do |attribute|
11
+ line_str = File.read(Localtower::Tools.last_migration).match(/((.*)t\.string :#{attribute})/)[0]
12
+ content = File.read(Localtower::Tools.last_migration).gsub(line_str, "#{line_str}, array: true")
13
+ File.write(Localtower::Tools.last_migration, content)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :attributes
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,62 +2,34 @@ module Localtower
2
2
  module Generators
3
3
  module ServiceObjects
4
4
  class InsertDefaults
5
+ DEFAULTS = {
6
+ '(no default value)' => '',
7
+ "empty string ('')" => "''",
8
+ 'zero (0)' => '0',
9
+ 'true' => 'true',
10
+ 'false' => 'false',
11
+ 'empty array ([])' => '[]',
12
+ 'empty hash ({})' => '{}',
13
+ 'nil' => 'nil',
14
+ }
5
15
 
6
16
  def initialize(attributes)
7
17
  @attributes = attributes
8
18
  end
9
19
 
10
20
  def call
11
- insert_defaults
12
- end
13
-
14
- private
15
-
16
- attr_reader :attributes
17
-
18
- def insert_defaults
19
21
  attributes.each do |attribute|
20
22
  attribute.each do |attr_key, attr_value|
21
- process_migration_file(attr_key, attr_value)
23
+ line_str = File.read(Localtower::Tools.last_migration).match(/((.*)t\.(.*)\:#{attr_key})/)[0]
24
+ content = File.read(Localtower::Tools.last_migration).gsub(line_str, "#{line_str}, default: #{attr_value}")
25
+ File.write(Localtower::Tools.last_migration, content)
22
26
  end
23
27
  end
24
- build_file(file_lines)
25
- end
26
-
27
- def process_migration_file(attr_key, attr_value)
28
- file_lines.map do |line|
29
- attach_default_value(line, attr_key, attr_value)
30
- end
31
- end
32
-
33
- def attach_default_value(line, attr_key, attr_value)
34
- if table_attribute_line?(line) and line.include? attr_key
35
- build_line(line, attr_value)
36
- else
37
- line
38
- end
39
28
  end
40
29
 
41
- def build_file(lines)
42
- File.open(latest_migration, 'w') { |f| f.puts lines }
43
- end
44
-
45
- def build_line(line, attr_value)
46
- line.gsub!("\n", "") << ", default: " << "#{attr_value}" << "\n"
47
- end
48
-
49
- def latest_migration
50
- @latest_migration ||= Dir["#{Rails.root}/db/migrate/*"].last
51
- end
52
-
53
- def file_lines
54
- @file_lines ||= File.readlines(latest_migration)
55
- end
56
-
57
- def table_attribute_line?(line)
58
- line.squish.start_with? "t."
59
- end
30
+ private
60
31
 
32
+ attr_reader :attributes
61
33
  end
62
34
  end
63
35
  end
@@ -0,0 +1,80 @@
1
+ module Localtower
2
+ module Generators
3
+ module ServiceObjects
4
+ class InsertIndexes
5
+ USING = [
6
+ 'none',
7
+ 'default',
8
+ 'gin',
9
+ 'gist',
10
+ ].freeze
11
+
12
+ UNIQUE = [
13
+ false,
14
+ true,
15
+ ]
16
+
17
+ ALGO = [
18
+ 'default',
19
+ 'concurrently',
20
+ ]
21
+
22
+ def initialize(attributes)
23
+ @attributes = attributes
24
+ end
25
+
26
+ def call
27
+ attributes.each do |attribute|
28
+ attribute.each do |attr_key, options|
29
+ line_str_original = File.read(Localtower::Tools.last_migration).match(/((.*)add_index :(.*), :#{attr_key})/)[0]
30
+ line_str = line_str_original.clone
31
+
32
+ line_str = inser_using(line_str, options)
33
+ line_str = inser_unique(line_str, options)
34
+ line_str = inser_algorithm(line_str, options)
35
+
36
+ content = File.read(Localtower::Tools.last_migration).gsub(line_str_original, line_str)
37
+ content = add_disable_ddl_transaction(content, options)
38
+
39
+ File.write(Localtower::Tools.last_migration, content)
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :attributes
47
+
48
+ def inser_using(line_str, options)
49
+ return line_str if options['using'] == 'default'
50
+
51
+ line_str << ", using: :#{options['using']}"
52
+ end
53
+
54
+ def inser_unique(line_str, options)
55
+ return line_str if options['unique'] != true
56
+
57
+ line_str << ", unique: true"
58
+ end
59
+
60
+ def inser_algorithm(line_str, options)
61
+ return line_str unless options['algorithm']
62
+ return line_str if options['algorithm'] == 'default'
63
+
64
+ line_str << ", algorithm: :#{options['algorithm']}"
65
+ end
66
+
67
+ def add_disable_ddl_transaction(content, options)
68
+ return content if options['algorithm'] != 'concurrently'
69
+
70
+ # Add disable_ddl_transaction! if the algorythm is 'concurrently'
71
+ if content['disable_ddl_transaction'].blank?
72
+ content = content.gsub(/^class (.*)$/, "class \\1\n disable_ddl_transaction!\n")
73
+ end
74
+
75
+ content
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ module Localtower
2
+ module Generators
3
+ module ServiceObjects
4
+ class InsertNullable
5
+ def initialize(attributes)
6
+ @attributes = attributes
7
+ end
8
+
9
+ def call
10
+ attributes.each do |attribute|
11
+ line_str = File.read(Localtower::Tools.last_migration).match(/((.*)t\.(.*)\:#{attribute})/)[0]
12
+ content = File.read(Localtower::Tools.last_migration).gsub(line_str, "#{line_str}, null: false")
13
+ File.write(Localtower::Tools.last_migration, content)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :attributes
20
+ end
21
+ end
22
+ end
23
+ end