Class: Statecraft::Generators::MachineGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/statecraft/machine/machine_generator.rb

Overview

rails generate statecraft:machine Order — the single statecraft generator. Creates the migration (state column, optional changed_at, the per-model log table with a cascade FK and a CHECK constraint for a freshly created table), the machine class, the readonly log model, the model mounting with helpers and scopes on, and lazily the shared ApplicationMachine parent. The migration lands in the migration path configured for the model's database, so multi-database apps get it next to their model.

Instance Method Summary collapse

Instance Method Details

#create_application_machineObject



29
30
31
32
33
34
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 29

def create_application_machine
  application_machine_path = "app/state_machines/application_machine.rb"
  return if File.exist?(File.join(destination_root, application_machine_path))

  template "application_machine.rb.tt", application_machine_path
end

#create_log_modelObject



49
50
51
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 49

def create_log_model
  template "log_model.rb.tt", "app/models/#{file_path}_transition.rb"
end

#create_machine_classObject



45
46
47
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 45

def create_machine_class
  template "machine.rb.tt", "app/state_machines/#{file_path}_flow.rb"
end

#create_migration_fileObject



61
62
63
64
65
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 61

def create_migration_file
  migration_source = existing_model? ? "add_migration.rb.tt" : "create_migration.rb.tt"
  migration_template migration_source,
                     "#{migration_directory}/create_#{migration_slug}_state_machine.rb"
end

#create_namespace_moduleObject



36
37
38
39
40
41
42
43
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 36

def create_namespace_module
  return if class_path.empty? || existing_model?

  module_file = "app/models/#{class_path.join("/")}.rb"
  return if File.exist?(File.join(destination_root, module_file))

  template "namespace_module.rb.tt", module_file
end

#create_or_mount_modelObject



53
54
55
56
57
58
59
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 53

def create_or_mount_model
  if existing_model?
    inject_into_class model_file, mounting_target_class, mounting_line
  else
    template "model.rb.tt", model_file
  end
end

#detect_model_presenceObject



25
26
27
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 25

def detect_model_presence
  @existing_model = File.exist?(File.join(destination_root, model_file))
end