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
@@ -3,111 +3,84 @@ require 'spec_helper'
3
3
  module Localtower
4
4
  module Generators
5
5
  describe Migration do
6
- before(:all) do
7
- clean_files
8
- end
9
-
10
- after(:all) do
11
- end
12
-
13
- it 'create_table' do
14
- data = attributes_for(:create_table)
15
- data["run_migrate"] = true
16
-
17
- generator = ::Localtower::Generators::Migration.new(data).run
18
-
19
- expect(::Localtower::Tools.word_in_file?(last_migration, /create_table :posts/)).to eq(true)
20
- end
21
-
22
- it 'create_table_two' do
23
- data = attributes_for(:create_table_two)
24
- data["run_migrate"] = true
25
-
26
- generator = ::Localtower::Generators::Migration.new(data).run
27
-
28
- expect(::Localtower::Tools.word_in_file?(last_migration, /create_table :users/)).to eq(true)
29
- end
30
-
31
6
  it 'add_column' do
32
- data = attributes_for(:add_column)
33
- data["run_migrate"] = true
7
+ data = attributes_for(:add_column)[:migrations]
34
8
 
35
9
  generator = ::Localtower::Generators::Migration.new(data).run
36
10
 
37
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :tags, :text, default: \[\], array: true/)).to eq(true)
38
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_index :posts, :tags, using: :gin/)).to eq(true)
39
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :views, :integer, default: 0, null: false, index: true/)).to eq(true)
40
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :content, :text/)).to eq(true)
41
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :title, :string, index: true/)).to eq(true)
11
+ expect(word_in_file?(last_migration, /add_column :posts, :tags, :string, default: \[\], array: true/)).to eq(true)
12
+ expect(word_in_file?(last_migration, /add_column :posts, :views, :integer, default: 0, null: false/)).to eq(true)
13
+ expect(word_in_file?(last_migration, /add_column :posts, :content, :text/)).to eq(true)
14
+ expect(word_in_file?(last_migration, /add_column :posts, :title, :string/)).to eq(true)
42
15
  end
43
16
 
44
- it 'add_column fail' do
45
- data = attributes_for(:add_column_fail)
46
- data["run_migrate"] = true
17
+ it 'remove_column' do
18
+ data = attributes_for(:remove_column)[:migrations]
47
19
 
48
20
  generator = ::Localtower::Generators::Migration.new(data).run
49
21
 
50
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :tags, :text, default: \[\], array: true/)).to eq(false)
51
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_index :posts, :tags, using: :gin/)).to eq(false)
52
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :views, :integer, default: 0, null: false, index: true/)).to eq(false)
53
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :content, :text/)).to eq(false)
54
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_column :posts, :title, :string, index: true/)).to eq(false)
22
+ expect(word_in_file?(last_migration, /remove_column :posts, :title/)).to eq(true)
23
+ expect(word_in_file?(last_migration, /rename_column :posts, :views, :views_by_users/)).to eq(true)
55
24
  end
56
25
 
57
26
  it 'rename_column' do
58
- data = attributes_for(:rename_column)
59
- data["run_migrate"] = true
27
+ data = attributes_for(:rename_column)[:migrations]
60
28
 
61
29
  generator = ::Localtower::Generators::Migration.new(data).run
62
30
 
63
- expect(::Localtower::Tools.word_in_file?(last_migration, /rename_column :posts, :content, :content_new/)).to eq(true)
31
+ expect(word_in_file?(last_migration, /rename_column :posts, :content, :content_new/)).to eq(true)
64
32
  end
65
33
 
66
- it 'remove_column' do
67
- data = attributes_for(:remove_column)
68
- data["run_migrate"] = true
34
+ it 'change_column_type' do
35
+ data = attributes_for(:change_column_type)[:migrations]
69
36
 
70
37
  generator = ::Localtower::Generators::Migration.new(data).run
71
38
 
72
- expect(::Localtower::Tools.word_in_file?(last_migration, /remove_column :posts, :title/)).to eq(true)
73
- expect(::Localtower::Tools.word_in_file?(last_migration, /rename_column :posts, :views, :views_by_users/)).to eq(true)
39
+ expect(word_in_file?(last_migration, /add_index :posts, :views_by_users/)).to eq(true)
40
+ expect(word_in_file?(last_migration, /change_column :posts, :content_new, :string/)).to eq(true)
74
41
  end
75
42
 
76
- it 'change_column_type' do
77
- data = attributes_for(:change_column_type)
78
- data["run_migrate"] = true
43
+ it 'add_index_to_column' do
44
+ data = attributes_for(:add_index_to_column)[:migrations]
79
45
 
80
46
  generator = ::Localtower::Generators::Migration.new(data).run
81
47
 
82
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_index :posts, :views_by_users/)).to eq(true)
83
- expect(::Localtower::Tools.word_in_file?(last_migration, /change_column :posts, :content_new, :string/)).to eq(true)
48
+ expect(word_in_file?(last_migration, /disable_ddl_transaction!/)).to eq(true)
49
+ expect(word_in_file?(last_migration, /add_index :posts, :content/)).to eq(true)
50
+ expect(word_in_file?(last_migration, /add_index :posts, :content_second, unique: true/)).to eq(true)
51
+ expect(word_in_file?(last_migration, /add_index :posts, :content_third, using: :gin, algorithm: :concurrently/)).to eq(true)
84
52
  end
85
53
 
86
- it 'add_index_to_column' do
87
- data = attributes_for(:add_index_to_column)
88
- data["run_migrate"] = true
54
+ it 'belongs_to' do
55
+ data = attributes_for(:belongs_to)[:migrations]
56
+
57
+ model_post = "#{Rails.root}/app/models/post.rb"
58
+ model_user = "#{Rails.root}/app/models/user.rb"
59
+
60
+ File.open(model_post, 'w') { |f| f.write("class Post < ApplicationRecord\nend")}
61
+ File.open(model_user, 'w') { |f| f.write("class User < ApplicationRecord\nend")}
89
62
 
90
63
  generator = ::Localtower::Generators::Migration.new(data).run
91
64
 
92
- expect(::Localtower::Tools.word_in_file?(last_migration, /add_index :posts, :content_new/)).to eq(true)
65
+ expect(word_in_file?(model_post, /# belongs_to :user/)).to eq(true)
66
+ expect(word_in_file?(model_user, /# has_many :posts/)).to eq(true)
67
+ expect(word_in_file?(last_migration, /add_reference :posts, :user, foreign_key: true, index: true/)).to eq(true)
93
68
  end
94
69
 
95
70
  it 'remove_index_to_column' do
96
- data = attributes_for(:remove_index_to_column)
97
- data["run_migrate"] = true
71
+ data = attributes_for(:remove_index_to_column)[:migrations]
98
72
 
99
73
  generator = ::Localtower::Generators::Migration.new(data).run
100
74
 
101
- expect(::Localtower::Tools.word_in_file?(last_migration, /remove_index :posts, :views_by_users/)).to eq(true)
75
+ expect(word_in_file?(last_migration, /remove_index :posts, :views_by_users/)).to eq(true)
102
76
  end
103
77
 
104
78
  it 'drop_table' do
105
- data = attributes_for(:drop_table)
106
- data["run_migrate"] = true
79
+ data = attributes_for(:drop_table)[:migrations]
107
80
 
108
81
  generator = ::Localtower::Generators::Migration.new(data).run
109
82
 
110
- expect(::Localtower::Tools.word_in_file?(last_migration, /drop_table :posts, force: :cascade/)).to eq(true)
83
+ expect(word_in_file?(last_migration, /drop_table :posts/)).to eq(true)
111
84
  end
112
85
  end
113
86
  end
@@ -3,40 +3,49 @@ require 'spec_helper'
3
3
  module Localtower
4
4
  module Generators
5
5
  describe Model do
6
- before(:all) do
7
- clean_files
8
- end
9
-
10
- after(:all) do
11
- end
12
-
13
- it 'create a post' do
14
- data = attributes_for(:post_one)
15
- data["run_migrate"] = true
16
-
17
- ::Localtower::Generators::Model.new(data).run
18
-
19
- expect(File.exist?("#{Rails.root}/app/models/post.rb")).to eq(true)
20
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/app/models/post.rb", /class Post/)).to eq(true)
21
-
22
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /create_table "posts"/)).to eq(true)
23
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.string(.*)"title"/)).to eq(true)
24
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.text(.*)"content"/)).to eq(true)
25
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.index(.*)\["title"\], name: "index_posts_on_title"/)).to eq(true)
26
- end
27
-
28
- it 'create a user' do
29
- data = attributes_for(:user_one)
30
- data["run_migrate"] = true
31
-
32
- ::Localtower::Generators::Model.new(data).run
33
-
34
- expect(File.exist?("#{Rails.root}/app/models/user.rb")).to eq(true)
35
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/app/models/user.rb", /class User/)).to eq(true)
36
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /create_table "users"/)).to eq(true)
37
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.string(.*)"name"/)).to eq(true)
38
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.jsonb(.*)"metadata"/)).to eq(true)
39
- expect(::Localtower::Tools.word_in_file?("#{Rails.root}/db/schema.rb", /t.index(.*)\["name"\], name: "index_users_on_name"/)).to eq(true)
6
+ context 'model 1' do
7
+ let(:expected_migration) do
8
+ <<-TEXT_MULTILINE.strip_heredoc
9
+ class CreatePosts < ActiveRecord::Migration[5.2]
10
+ disable_ddl_transaction!
11
+
12
+ def change
13
+ create_table :posts do |t|
14
+ t.string :title
15
+ t.string :tags, array: true, default: [], null: false
16
+ t.text :content, null: false
17
+ t.integer :likes_count, default: 0
18
+ t.float :score
19
+ t.jsonb :metadata, default: {}, null: false
20
+
21
+ t.timestamps
22
+ end
23
+ add_index :posts, :title, unique: true
24
+ add_index :posts, :tags, using: :gin, unique: true, algorithm: :concurrently
25
+ add_index :posts, :content, using: :gist
26
+ add_index :posts, :likes_count
27
+ add_index :posts, :score
28
+ add_index :posts, :metadata
29
+ end
30
+ end
31
+ TEXT_MULTILINE
32
+ end
33
+
34
+ let(:expected_model) do
35
+ <<-TEXT_MULTILINE.strip_heredoc
36
+ class Post < ApplicationRecord
37
+ end
38
+ TEXT_MULTILINE
39
+ end
40
+
41
+ let(:attributes) { attributes_for(:post_one).deep_stringify_keys }
42
+
43
+ it 'create a post' do
44
+ ::Localtower::Generators::Model.new(attributes).run
45
+
46
+ expect(File.read("#{Rails.root}/app/models/post.rb")).to eq(expected_model)
47
+ expect(File.read(last_migration)).to eq(expected_migration)
48
+ end
40
49
  end
41
50
  end
42
51
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Localtower::Generators::ServiceObjects::InsertArray do
4
+ let(:service) { described_class.new(attributes) }
5
+
6
+ let(:attributes) {
7
+ ['tags']
8
+ }
9
+
10
+ let(:base_file_content) {
11
+ <<-MIGRATION.strip_heredoc
12
+ class CreateTests < ActiveRecord::Migration[7.0]
13
+ def change
14
+ create_table :tests do |t|
15
+ t.string :tags
16
+
17
+ t.timestamps
18
+ end
19
+ end
20
+ end
21
+ MIGRATION
22
+ }
23
+
24
+ let(:expected_file_content) {
25
+ <<-MIGRATION.strip_heredoc
26
+ class CreateTests < ActiveRecord::Migration[7.0]
27
+ def change
28
+ create_table :tests do |t|
29
+ t.string :tags, array: true
30
+
31
+ t.timestamps
32
+ end
33
+ end
34
+ end
35
+ MIGRATION
36
+ }
37
+
38
+ before do
39
+ File.open("#{Rails.root}/db/migrate/0_migration_name_.rb", 'w') { |f| f.write(base_file_content) }
40
+ end
41
+
42
+ it 'works' do
43
+ service.call
44
+
45
+ expect(File.read(last_migration)).to eq(expected_file_content)
46
+ end
47
+ end
@@ -1,32 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ::Localtower::Generators::ServiceObjects::InsertDefaults do
4
-
5
- let(:latest_migration) do
6
- Tempfile.new('foo').tap do |file|
7
- file.puts base_file_content
8
- file.rewind
9
- end
10
- end
4
+ let(:service) { described_class.new(attributes) }
11
5
 
12
6
  let(:attributes) {
13
7
  [
14
- { 'first' => "true" },
15
- { 'third' => "nil" },
8
+ { 'first' => "true" },
9
+ { 'third' => "nil" },
16
10
  { 'fourth' => "false" },
17
- { 'fifth' => "0" }
11
+ { 'foo' => "{}" },
12
+ { 'fifth' => "0" }
18
13
  ]
19
14
  }
20
15
 
21
16
  let(:base_file_content) {
22
17
  <<-MIGRATION.strip_heredoc
23
- class CreateTests < ActiveRecord::Migration[5.0]
18
+ class CreateTests < ActiveRecord::Migration[7.0]
24
19
  def change
25
20
  create_table :tests do |t|
26
21
  t.string :first
27
22
  t.integer :second
28
23
  t.string :third
29
24
  t.string :fourth
25
+ t.jsonb :foo
30
26
  t.integer :fifth
31
27
 
32
28
  t.timestamps
@@ -37,34 +33,33 @@ describe ::Localtower::Generators::ServiceObjects::InsertDefaults do
37
33
  MIGRATION
38
34
  }
39
35
 
40
- let(:expected_file_structure_array) {
41
- [
42
- "class CreateTests < ActiveRecord::Migration[5.0]\n",
43
- " def change\n",
44
- " create_table :tests do |t|\n",
45
- " t.string :first, default: true\n",
46
- " t.integer :second\n",
47
- " t.string :third, default: nil\n",
48
- " t.string :fourth, default: false\n",
49
- " t.integer :fifth, default: 0\n",
50
- "\n",
51
- " t.timestamps\n",
52
- " end\n",
53
- " add_index :tests, :first\n",
54
- " end\n",
55
- "end\n"
56
- ]
57
- }
36
+ let(:expected_file_content) {
37
+ <<-MIGRATION.strip_heredoc
38
+ class CreateTests < ActiveRecord::Migration[7.0]
39
+ def change
40
+ create_table :tests do |t|
41
+ t.string :first, default: true
42
+ t.integer :second
43
+ t.string :third, default: nil
44
+ t.string :fourth, default: false
45
+ t.jsonb :foo, default: {}
46
+ t.integer :fifth, default: 0
58
47
 
59
- let(:service) { described_class.new(attributes) }
48
+ t.timestamps
49
+ end
50
+ add_index :tests, :first
51
+ end
52
+ end
53
+ MIGRATION
54
+ }
60
55
 
61
56
  before do
62
- allow(service).to receive(:latest_migration).and_return(latest_migration)
63
- service.call
57
+ File.open("#{Rails.root}/db/migrate/0_migration_name_.rb", 'w') { |f| f.write(base_file_content) }
64
58
  end
65
59
 
66
- it 'inserts default values for attributes' do
67
- lines_array = File.readlines(latest_migration)
68
- expect(lines_array).to match_array expected_file_structure_array
60
+ it 'works' do
61
+ service.call
62
+
63
+ expect(File.read(last_migration)).to eq(expected_file_content)
69
64
  end
70
65
  end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Localtower::Generators::ServiceObjects::InsertIndexes do
4
+ let(:service) { described_class.new(attributes) }
5
+
6
+ let(:attributes) {
7
+ [
8
+ { 'first' =>
9
+ {
10
+ 'using' => 'gin',
11
+ 'algorithm' => 'concurrently',
12
+ }
13
+ },
14
+ { 'third' =>
15
+ {
16
+ 'using' => 'gist',
17
+ 'unique' => true,
18
+ }
19
+ },
20
+ { 'fourth' =>
21
+ {
22
+ 'using' => 'default',
23
+ 'algorithm' => 'concurrently',
24
+ }
25
+ },
26
+ { 'fifth' =>
27
+ {
28
+ 'using' => 'default',
29
+ 'unique' => true,
30
+ }
31
+ }
32
+ ]
33
+ }
34
+
35
+ let(:base_file_content) {
36
+ <<-MIGRATION.strip_heredoc
37
+ class CreateTests < ActiveRecord::Migration[7.0]
38
+ def change
39
+ create_table :tests do |t|
40
+ t.string :zero
41
+ t.string :first
42
+ t.string :third
43
+ t.string :fourth
44
+ t.integer :fifth
45
+
46
+ t.timestamps
47
+ end
48
+ add_index :tests, :first
49
+ add_index :tests, :third
50
+ add_index :tests, :fourth
51
+ add_index :tests, :fifth
52
+ end
53
+ end
54
+ MIGRATION
55
+ }
56
+
57
+ let(:expected_file_content) {
58
+ <<-MIGRATION.strip_heredoc
59
+ class CreateTests < ActiveRecord::Migration[7.0]
60
+ disable_ddl_transaction!
61
+
62
+ def change
63
+ create_table :tests do |t|
64
+ t.string :zero
65
+ t.string :first
66
+ t.string :third
67
+ t.string :fourth
68
+ t.integer :fifth
69
+
70
+ t.timestamps
71
+ end
72
+ add_index :tests, :first, using: :gin, algorithm: :concurrently
73
+ add_index :tests, :third, using: :gist, unique: true
74
+ add_index :tests, :fourth, algorithm: :concurrently
75
+ add_index :tests, :fifth, unique: true
76
+ end
77
+ end
78
+ MIGRATION
79
+ }
80
+
81
+ before do
82
+ File.open("#{Rails.root}/db/migrate/0_migration_name_.rb", 'w') { |f| f.write(base_file_content) }
83
+ end
84
+
85
+ it 'works' do
86
+ service.call
87
+
88
+ expect(File.read(last_migration)).to eq(expected_file_content)
89
+ end
90
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Localtower::Generators::ServiceObjects::InsertNullable do
4
+ let(:service) { described_class.new(attributes) }
5
+
6
+ let(:attributes) {
7
+ [
8
+ 'first',
9
+ 'third',
10
+ 'fourth'
11
+ ]
12
+ }
13
+
14
+ let(:base_file_content) {
15
+ <<-MIGRATION.strip_heredoc
16
+ class CreateTests < ActiveRecord::Migration[7.0]
17
+ def change
18
+ create_table :tests do |t|
19
+ t.string :first
20
+ t.integer :second
21
+ t.string :third
22
+ t.string :fourth, default: 'foo'
23
+ t.integer :fifth
24
+
25
+ t.timestamps
26
+ end
27
+ add_index :tests, :first
28
+ end
29
+ end
30
+ MIGRATION
31
+ }
32
+
33
+ let(:expected_file_content) {
34
+ <<-MIGRATION.strip_heredoc
35
+ class CreateTests < ActiveRecord::Migration[7.0]
36
+ def change
37
+ create_table :tests do |t|
38
+ t.string :first, null: false
39
+ t.integer :second
40
+ t.string :third, null: false
41
+ t.string :fourth, null: false, default: 'foo'
42
+ t.integer :fifth
43
+
44
+ t.timestamps
45
+ end
46
+ add_index :tests, :first
47
+ end
48
+ end
49
+ MIGRATION
50
+ }
51
+
52
+ before do
53
+ File.open("#{Rails.root}/db/migrate/0_migration_name_.rb", 'w') { |f| f.write(base_file_content) }
54
+ end
55
+
56
+ it 'works' do
57
+ service.call
58
+
59
+ expect(File.read(last_migration)).to eq(expected_file_content)
60
+ end
61
+ end
@@ -1,14 +1,4 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Localtower::Tools do
4
- before(:each) do
5
- end
6
-
7
- after(:all) do
8
- end
9
-
10
- # TODO:
11
- it 'list models' do
12
- models = ::Localtower::Tools.models_presented
13
- end
3
+ RSpec.describe Localtower::Tools do
14
4
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,24 @@
1
1
  def clean_files
2
- ::Localtower::Tools.sql_drop_all_tables
3
- ::Localtower::Generators::Migration.new({}).remove_all_migrations
4
- Dir["#{Rails.root}/app/models/**/*.*"].each { |model_file| File.delete(model_file) }
2
+ ::ActiveRecord::Base.connection.tables.each do |table|
3
+ cmd = "DROP TABLE if exists #{table.upcase} cascade;"
4
+ ::ActiveRecord::Base.connection.execute(cmd)
5
+ end
6
+
7
+ # remove Schema:
8
+ Dir["#{Rails.root}/db/migrate/*"].each { |migration_file| File.delete(migration_file) }
9
+
10
+ content_schema = """
11
+ ActiveRecord::Schema.define(version: 0) do
12
+ end
13
+ """
14
+
15
+ File.open("#{Rails.root}/db/schema.rb", "w") do |f|
16
+ f.write(content_schema)
17
+ end
18
+
19
+ Dir["#{Rails.root}/app/models/**/*.*"]
20
+ .reject { |file_name| file_name['application_record.rb']}
21
+ .each { |model_file| File.delete(model_file) }
5
22
  end
6
23
 
7
24
  def migration_files
@@ -12,6 +29,10 @@ def last_migration
12
29
  migration_files.sort.last
13
30
  end
14
31
 
32
+ def word_in_file?(file, word_or_exp)
33
+ File.readlines(file).grep(word_or_exp).size > 0
34
+ end
35
+
15
36
  #============================
16
37
  ENV['RAILS_ENV'] = 'test'
17
38
 
@@ -30,13 +51,12 @@ RSpec.configure do |config|
30
51
  config.infer_base_class_for_anonymous_controllers = true
31
52
 
32
53
  config.order = 123
33
- # config.order = 'random'
34
54
 
35
55
  config.before(:suite) do
36
56
  FactoryBot.find_definitions
37
57
  end
38
58
 
39
- config.before(:all) do
59
+ config.before(:each) do
40
60
  clean_files
41
61
  end
42
62