Module: Kaal::Rails

Defined in:
lib/kaal/rails.rb,
lib/kaal/rails/railtie.rb,
lib/kaal/rails/version.rb,
lib/kaal/rails/installer.rb

Overview

Rails integration surface for Kaal.

Defined Under Namespace

Classes: Installer, Railtie

Constant Summary collapse

DETECT_BACKEND_DEFAULT =
Object.new
VERSION =
'0.4.0'

Class Method Summary collapse

Class Method Details

.build_backend(backend_name = detect_backend_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/kaal/rails.rb', line 32

def build_backend(backend_name = detect_backend_name)
  case backend_name.to_s
  when 'sqlite'
    Kaal::Backend::SQLite.new
  when 'postgres'
    Kaal::Backend::Postgres.new
  when 'mysql'
    Kaal::Backend::MySQL.new
  end
end

.configure_backend!(configuration: Kaal.configuration, backend: build_backend) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/kaal/rails.rb', line 43

def configure_backend!(configuration: Kaal.configuration, backend: build_backend)
  current_backend = configuration.backend
  return current_backend if current_backend
  return nil unless backend

  configuration.backend = backend
end

.detect_backend_name(db_config = DETECT_BACKEND_DEFAULT) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kaal/rails.rb', line 19

def detect_backend_name(db_config = DETECT_BACKEND_DEFAULT)
  db_config = ::ActiveRecord::Base.connection_db_config if db_config.equal?(DETECT_BACKEND_DEFAULT)
  adapter = db_config&.adapter.to_s.downcase

  return 'sqlite' if adapter.include?('sqlite')
  return 'postgres' if adapter.include?('postgres')
  return 'mysql' if adapter.include?('mysql') || adapter.include?('trilogy')

  nil
rescue ::ActiveRecord::ConnectionNotEstablished
  nil
end

.install!(root: ::Rails.root, backend: detect_backend_name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/kaal/rails.rb', line 51

def install!(root: ::Rails.root, backend: detect_backend_name)
  installer = Installer.new(root:, backend:)
  {
    scheduler_config: installer.install_scheduler_config,
    migrations: installer.install_migrations
  }
end