Class: Frozen::Generators::DbGenerator

Inherits:
FrozenRails::Generator show all
Defined in:
lib/generators/frozen/db_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/frozen/db_generator.rb', line 10

def add_gems
  add_frozen_gems <<~RUBY
    # frozen:db
    gem "static_db"
    gem "sqlite_extensions-uuid"
    gem "friendly_id"
  RUBY

  add_frozen_gems <<~RUBY, env: "development"
    # frozen:db
    gem "avo", ">= 3.2"
  RUBY
end

#add_routesObject



28
29
30
31
32
33
34
35
# File 'lib/generators/frozen/db_generator.rb', line 28

def add_routes
  append_to_routes <<~RUBY
    # frozen:db
    if Rails.env.development?
      mount_avo at: "/avo"
    end
  RUBY
end

#bundle_gemsObject



24
25
26
# File 'lib/generators/frozen/db_generator.rb', line 24

def bundle_gems
  bundle!
end

#configure_applicationObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/frozen/db_generator.rb', line 43

def configure_application
  gsub_file "config/application.rb", /(config\.autoload_lib\(ignore: %w\[).*(\]\))/, '\1assets generators tasks templates\2'

  append_to_application_config <<~RUBY
    # frozen:db
    config.generators do |g|
      g.orm :active_record, primary_key_type: :string
      g.helper nil
    end
    config.active_storage.draw_routes = true
  RUBY
end

#copy_filesObject



37
38
39
40
41
# File 'lib/generators/frozen/db_generator.rb', line 37

def copy_files
  copy_file "database.yml", "config/database.yml", force: true
  copy_directory "initializers", "config/initializers"
  copy_directory "lib", "lib"
end

#migrate_and_cleanup_dbObject



119
120
121
122
# File 'lib/generators/frozen/db_generator.rb', line 119

def migrate_and_cleanup_db
  rails_command "db:migrate"
  rails_command "db:drop"
end

#prepare_dbObject



56
57
58
59
# File 'lib/generators/frozen/db_generator.rb', line 56

def prepare_db
  rails_command "db:create"
  rails_command "db:schema:load"
end

#setup_active_storageObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/frozen/db_generator.rb', line 61

def setup_active_storage
  rails_command "active_storage:install"

  active_storage_migration = in_root { Dir.glob("db/migrate/*.active_storage.rb").first }

  gsub_file active_storage_migration, /# Use.*\n.*primary_and_foreign_key_types/, <<~RUBY.chomp.lines.map(&:chomp).join("\n    ")
    # Use custom primary and foreign key types to support SQLite UUIDs.
    primary_key_type = { id: :string, default: -> { "uuid()" }, limit: 36 }
    foreign_key_type = { type: :string, limit: 36 }
  RUBY
  gsub_file active_storage_migration, "id: primary_key_type", "**primary_key_type"
  gsub_file active_storage_migration, "type: foreign_key_type", "**foreign_key_type"
  gsub_file active_storage_migration, /\n\s*private.*end(?=\nend)/m, ""
end

#setup_avoObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/generators/frozen/db_generator.rb', line 95

def setup_avo
  rails_command "g avo:install"

  prepend_to_file "config/initializers/avo.rb", <<~RUBY + "\n"
    if Rails.env.development?
      # Allow moving Avo controllers into `app/avo/controllers/`
      Rails.autoloaders.main.collapse(
        Rails.root.join("app/avo/controllers")
      )
    else
      # Don't load Avo
      Rails.autoloaders.main.ignore(
        Rails.root.join("app/avo"),
        Rails.root.join("app/controllers/avo")
      )

      # Don't configure Avo
      return
    end
  RUBY

  run "bin/rubocop -a config/initializers/avo.rb"
end

#setup_friendly_idObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/frozen/db_generator.rb', line 76

def setup_friendly_id
  rails_command "g migration create_friendly_id_slugs"

  friendly_id_migration = in_root { Dir.glob("db/migrate/*create_friendly_id_slugs.rb").first }

  gsub_file friendly_id_migration, /create_table.*?end(?=\n)/m, <<~RUBY.chomp.lines.map(&:chomp).join("\n    ")
    create_table :friendly_id_slugs, id: :string, default: -> { "uuid()" }, limit: 36 do |t|
      t.string :slug, null: false
      t.string :sluggable_id, limit: 36, null: false
      t.string :sluggable_type, limit: 50
      t.string :scope
      t.datetime :created_at
    end
    add_index :friendly_id_slugs, [ :sluggable_type, :sluggable_id ]
    add_index :friendly_id_slugs, [ :slug, :sluggable_type ], length: { slug: 140, sluggable_type: 50 }
    add_index :friendly_id_slugs, [ :slug, :sluggable_type, :scope ], length: { slug: 70, sluggable_type: 50, scope: 70 }, unique: true
  RUBY
end