Class: Statecraft::Generators::FromAasmGenerator

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

Overview

rails generate statecraft:from_aasm Order [machine_name] — the door off aasm. Reads the live aasm machine through reflection (states, the initial state, real event names, the state column and the symbol guards), generates the machine skeleton, the log table migration and the mounting line. aasm keeps state in a column already, so nothing is backfilled: the conversion adds the history aasm never had.

Instance Method Summary collapse

Instance Method Details

#create_application_machineObject



28
29
30
31
32
33
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 28

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_migrationObject



48
49
50
51
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 48

def create_log_migration
  migration_template "create_log_table.rb.tt",
                     "#{migration_directory}/create_#{migration_slug}_transitions.rb"
end

#create_machine_skeletonObject



35
36
37
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 35

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

#load_aasm_machineObject



22
23
24
25
26
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 22

def load_aasm_machine
  @aasm_machine = resolve_aasm_machine
  assert_graph_present
  assert_no_branching_events
end

#mount_modelObject



39
40
41
42
43
44
45
46
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 39

def mount_model
  unless File.exist?(File.join(destination_root, model_file))
    say_status :skip, "#{model_file} not found — mount the machine yourself: #{mounting_line.strip}", :yellow
    return
  end

  inject_into_class model_file, class_name, mounting_line
end


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generators/statecraft/from_aasm/from_aasm_generator.rb', line 53

def print_cleanup_instructions
  say "\nOne migration: it creates the log table aasm never had and indexes " \
      "the existing #{state_column} column.", :green
  say "The column itself is left alone — NOT NULL, a default and a CHECK on a live"
  say "table are locks and legacy-row explosions; the migration header carries the recipe."
  say "\nAfter it runs, finish the move by hand:", :green
  say "  * #{class_name}: drop `include AASM` and the whole `aasm do ... end` block."
  say "  * Event calls keep their names with helpers: true (`order.pay!`), but a losing"
  say "    race now raises TransitionConflict instead of quietly answering false."
  say "  * aasm state predicates (`order.paid?`) are not generated: use `in_state?(:paid)`"
  say "    or the `#{class_name}.paid` scope."
  say "  * Guards that were lambdas are TODO comments in the skeleton — port them by hand."
end