Class: SOULs::DB

Inherits:
Thor
  • Object
show all
Defined in:
lib/souls/cli/db/index.rb,
lib/souls/cli/db/model.rb,
lib/souls/cli/db/rspec_model.rb,
lib/souls/cli/db/create_migration.rb

Instance Method Summary collapse

Instance Method Details

#add_column(class_name) ⇒ Object



71
72
73
74
# File 'lib/souls/cli/db/index.rb', line 71

def add_column(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  system("rake db:create_migration NAME=add_column_to_#{pluralized_class_name}")
end

#change_column(class_name) ⇒ Object



83
84
85
86
# File 'lib/souls/cli/db/index.rb', line 83

def change_column(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  system("rake db:create_migration NAME=change_column_to_#{pluralized_class_name}")
end

#createObject



23
24
25
26
27
28
29
30
# File 'lib/souls/cli/db/index.rb', line 23

def create
  case options[:env]
  when "production"
    db_system("rake db:create RACK_ENV=production")
  else
    db_system("rake db:create")
  end
end

#create_migration(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/souls/cli/db/create_migration.rb', line 4

def create_migration(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  singularized_class_name = class_name.underscore.singularize
  SOULs::DB.new.invoke(:model, [singularized_class_name], {})
  SOULs::DB.new.invoke(:rspec_model, [singularized_class_name], {})
  SOULs::Painter.create_file("")
  system("rake db:create_migration NAME=create_#{pluralized_class_name}")
  file_path = Dir["db/migrate/*create_#{pluralized_class_name}.rb"].first
  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      class Create#{pluralized_class_name.camelize} < ActiveRecord::Migration[6.1]
        def change
          create_table :#{pluralized_class_name} do |t|

            t.boolean :is_deleted, null: false, default: false
            t.timestamps
          end
        end
      end
    TEXT
  end
end

#drop_table(class_name) ⇒ Object



95
96
97
98
# File 'lib/souls/cli/db/index.rb', line 95

def drop_table(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  system("rake db:create_migration NAME=drop_table_to_#{pluralized_class_name}")
end

#migrateObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/souls/cli/db/index.rb', line 10

def migrate
  case options[:env]
  when "production"
    db_system("rake db:migrate RACK_ENV=production")
  else
    db_system("rake db:migrate")
    db_system("rake db:migrate RACK_ENV=test")
  end
  true
end

#migrate_resetObject



46
47
48
49
50
51
52
53
54
# File 'lib/souls/cli/db/index.rb', line 46

def migrate_reset
  case options[:env]
  when "production"
    db_system("rake db:migrate:reset RACK_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1")
  else
    db_system("rake db:migrate:reset")
    db_system("rake db:migrate RACK_ENV=test")
  end
end

#model(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/db/model.rb', line 4

def model(class_name)
  file_dir = "./app/models/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "#{file_dir}#{class_name.singularize}.rb"
  return "Model already exist! #{file_path}" if File.exist?(file_path)

  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      class #{class_name.camelize} < ActiveRecord::Base
      end
    TEXT
  end
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#remove_column(class_name) ⇒ Object



89
90
91
92
# File 'lib/souls/cli/db/index.rb', line 89

def remove_column(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  system("rake db:create_migration NAME=remove_column_to_#{pluralized_class_name}")
end

#rename_column(class_name) ⇒ Object



77
78
79
80
# File 'lib/souls/cli/db/index.rb', line 77

def rename_column(class_name)
  pluralized_class_name = class_name.underscore.pluralize
  system("rake db:create_migration NAME=rename_column_to_#{pluralized_class_name}")
end

#resetObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/souls/cli/db/index.rb', line 58

def reset
  case options[:env]
  when "production"
    db_system("rake db:migrate:reset RACK_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1")
    db_system("rake db:seed RACK_ENV=production")
  else
    db_system("rake db:migrate:reset")
    db_system("rake db:migrate RACK_ENV=test")
    db_system("rake db:seed")
  end
end

#rspec_model(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/souls/cli/db/rspec_model.rb', line 4

def rspec_model(class_name)
  file_dir = "./spec/models/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "./spec/models/#{class_name}_spec.rb"
  return "RspecModel already exist! #{file_path}" if File.exist?(file_path)

  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      RSpec.describe "#{class_name.camelize} Model テスト", type: :model do
        describe "#{class_name.camelize} データを書き込む" do
          it "valid #{class_name.camelize} Model" do
            expect(FactoryBot.build(:#{class_name.singularize})).to be_valid
          end
        end
      end
    TEXT
  end
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#seedObject



34
35
36
37
38
39
40
41
42
# File 'lib/souls/cli/db/index.rb', line 34

def seed
  case options[:env]
  when "production"
    db_system("rake db:seed RACK_ENV=production")
  else
    db_system("rake db:seed")
    db_system("rake db:seed RACK_ENV=test")
  end
end