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 of the model's connection owner, so multi-database apps get it next to their model.

Instance Method Summary collapse

Instance Method Details

#create_application_machine ⇒ Object



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

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_model ⇒ Object



36
37
38
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 36

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

#create_machine_class ⇒ Object



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

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

#create_migration_file ⇒ Object



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

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

#create_or_mount_model ⇒ Object



40
41
42
43
44
45
46
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 40

def create_or_mount_model
  if existing_model?
    inject_into_class "app/models/#{file_name}.rb", class_name, mounting_line
  else
    template "model.rb.tt", "app/models/#{file_name}.rb"
  end
end

#detect_model_presence ⇒ Object



21
22
23
# File 'lib/generators/statecraft/machine/machine_generator.rb', line 21

def detect_model_presence
  @existing_model = File.exist?(File.join(destination_root, "app/models/#{file_name}.rb"))
end