Class: Fosm::Generators::AppGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/fosm/app/app_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



17
18
19
# File 'lib/generators/fosm/app/app_generator.rb', line 17

def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#create_agent_fileObject



29
30
31
# File 'lib/generators/fosm/app/app_generator.rb', line 29

def create_agent_file
  template "agent.rb.tt", "app/agents/fosm/#{file_name}_agent.rb"
end

#create_controller_fileObject



25
26
27
# File 'lib/generators/fosm/app/app_generator.rb', line 25

def create_controller_file
  template "controller.rb.tt", "app/controllers/fosm/#{file_name}_controller.rb"
end

#create_migration_fileObject



40
41
42
# File 'lib/generators/fosm/app/app_generator.rb', line 40

def create_migration_file
  migration_template "migration.rb.tt", "db/migrate/create_fosm_#{table_name}.rb"
end

#create_model_fileObject



21
22
23
# File 'lib/generators/fosm/app/app_generator.rb', line 21

def create_model_file
  template "model.rb.tt", "app/models/fosm/#{file_name}.rb"
end

#create_view_filesObject



33
34
35
36
37
38
# File 'lib/generators/fosm/app/app_generator.rb', line 33

def create_view_files
  template "views/index.html.erb.tt", "app/views/fosm/#{file_name}/index.html.erb"
  template "views/show.html.erb.tt",  "app/views/fosm/#{file_name}/show.html.erb"
  template "views/new.html.erb.tt",   "app/views/fosm/#{file_name}/new.html.erb"
  template "views/_form.html.erb.tt", "app/views/fosm/#{file_name}/_form.html.erb"
end

#inject_agent_instructionsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/fosm/app/app_generator.rb', line 44

def inject_agent_instructions
  claude_md = ::Rails.root.join("CLAUDE.md")
  sentinel  = "<!-- fosm:agent-instructions -->"

  if File.exist?(claude_md) && File.read(claude_md).include?(sentinel)
    say_status :skip, "CLAUDE.md already has FOSM agent instructions", :yellow
    return
  end

  gem_root = Fosm::Engine.root.to_s
  agents_md_path = File.join(gem_root, "AGENTS.md")

  section = <<~MARKDOWN

    #{sentinel}
    ## FOSM (fosm-rails)

    This project uses `fosm-rails` — a Finite Object State Machine engine.
    When the user asks to build, extend, or modify features involving state machines,
    lifecycles, FOSM, or any code under `app/models/fosm/`, `app/controllers/fosm/`,
    or `app/agents/fosm/`, you **must** read and follow the instructions in:

    `#{agents_md_path}`

    ### Database / connection pool (IMPORTANT — read before touching database.yml)

    `Fosm::ApplicationRecord` only creates a separate Active Record connection pool
    when the host app has a `fosm:` role in `database.yml`. Without it, all FOSM
    models share the default app pool — this is correct for single-database setups.

    **Do NOT** add a `primary:` database role for FOSM. `primary` is the default role
    name Rails assigns to every `database.yml` entry, so that condition would create a
    redundant pool on the same database and cause a deterministic cross-pool deadlock
    whenever a `Fosm::*` model with `has_one_attached` / `has_many_attached` is saved,
    or when `ActiveRecord::Base.transaction` wraps a `Fosm::*` save.

    To opt in to a dedicated FOSM database, add this to `database.yml`:

    ```yaml
    fosm:
      <<: *default
      database: db/fosm.sqlite3   # or a separate PostgreSQL/MySQL URL
    ```

    See the "Database configuration and connection pools" section in AGENTS.md for
    the full explanation and the pre-0.2.2 workaround.
  MARKDOWN

  if File.exist?(claude_md)
    append_to_file claude_md, section
  else
    create_file claude_md, section.lstrip
  end
end

#update_routesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/fosm/app/app_generator.rb', line 99

def update_routes
  routes_file = ::Rails.root.join("config/routes/fosm.rb")

  route_entry = <<~RUBY
    scope module: "fosm", path: "/fosm/apps", as: :fosm do
      resources :#{plural_name}, controller: "#{file_name}" do
        member { post :fire_event }
      end
    end
  RUBY

  if File.exist?(routes_file)
    append_to_file routes_file, "\n#{route_entry}"
  else
    create_file routes_file, route_entry
    # Also ensure main routes.rb draws from fosm.rb
    main_routes = ::Rails.root.join("config/routes.rb")
    unless File.read(main_routes).include?("draw :fosm")
      inject_into_file main_routes, "\n  draw :fosm", after: "Rails.application.routes.draw do"
    end
  end
end