Class: Statecraft::Generators::MachineGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Statecraft::Generators::MachineGenerator
- 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
- #create_application_machine ⇒ Object
- #create_log_model ⇒ Object
- #create_machine_class ⇒ Object
- #create_migration_file ⇒ Object
- #create_namespace_module ⇒ Object
- #create_or_mount_model ⇒ Object
- #detect_model_presence ⇒ Object
Instance Method Details
#create_application_machine ⇒ Object
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_model ⇒ Object
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_class ⇒ Object
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_file ⇒ Object
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_module ⇒ Object
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_model ⇒ Object
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_presence ⇒ Object
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 |