Class: OpenLoam::Generators::EntityGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, PrimaryKeyOptions
Defined in:
lib/generators/open_loam/entity/entity_generator.rb

Overview

rails g open_loam:entity Equipment name:string daily_rate:decimal --domain rental

THE interface for adding a business entity — for humans and AI agents alike. One command produces a tenant-scoped, audited, evented model, its policy, an admin screen, and the isolation tests that prove the guardrails hold. No decisions about how tenancy/permissions work: OpenLoam already decided.

Constant Summary

Constants included from PrimaryKeyOptions

PrimaryKeyOptions::DEFAULT_STRING_LIMIT, PrimaryKeyOptions::SUPPORTED

Instance Method Summary collapse

Methods included from PrimaryKeyOptions

included

Instance Method Details

#add_route ⇒ Object

namespace: makes Rails inject the route INTO the existing namespace :admin do block (the one open_loam:install wrote) instead of stacking one admin block per entity, and falls back to creating the block when there is none. Re-running the generator is a no-op: the injection is force: false, so identical routing code is skipped. The API line carries defaults: { format: :json } — it is what an API namespace wants anyway, and it also has to differ textually from the admin line above: Rails' route injection skips code the file already contains (that is what makes re-running this generator a no-op), so two identical resources :gadgets lines would leave the second one out.

The admin resource carries the soft-delete recycle bin: deleted lists only_deleted, restore brings one back (see OpenLoam::SoftDeletable).



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 74

def add_route
  route <<~RUBY.strip, namespace: :admin
    resources :#{plural_file_name} do
      get :deleted, on: :collection
      patch :restore, on: :member
      get :export, on: :collection   # CSV of the current view
      post :bulk, on: :collection    # datatable bulk actions
    end
  RUBY
  route "resources :#{plural_file_name}, defaults: { format: :json }", namespace: :api
end

#create_admin_controller ⇒ Object



40
41
42
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 40

def create_admin_controller
  template "controller.rb", "app/controllers/admin/#{plural_file_name}_controller.rb"
end

#create_admin_views ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 48

def create_admin_views
  template "views/index.html.erb", "app/views/admin/#{plural_file_name}/index.html.erb"
  template "views/deleted.html.erb", "app/views/admin/#{plural_file_name}/deleted.html.erb"
  template "views/show.html.erb", "app/views/admin/#{plural_file_name}/show.html.erb"
  template "views/new.html.erb", "app/views/admin/#{plural_file_name}/new.html.erb"
  template "views/edit.html.erb", "app/views/admin/#{plural_file_name}/edit.html.erb"
  template "views/_form.html.erb", "app/views/admin/#{plural_file_name}/_form.html.erb"
end

#create_api_controller ⇒ Object



44
45
46
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 44

def create_api_controller
  template "api_controller.rb", "app/controllers/api/#{plural_file_name}_controller.rb"
end

#create_entity_test ⇒ Object



57
58
59
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 57

def create_entity_test
  template "entity_test.rb", "test/entities/#{file_name}_test.rb"
end

#create_migration_file ⇒ Object



28
29
30
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 28

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

#create_model ⇒ Object



32
33
34
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 32

def create_model
  template "model.rb", "app/models/#{file_name}.rb"
end

#create_policy ⇒ Object



36
37
38
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 36

def create_policy
  template "policy.rb", "app/policies/#{file_name}_policy.rb"
end


86
87
88
89
90
91
92
# File 'lib/generators/open_loam/entity/entity_generator.rb', line 86

def print_next_steps
  say ""
  say "Entity #{class_name} created. Next:", :green
  say "  1. bin/rails db:migrate"
  say "  2. Declare field-level permissions in app/policies/#{file_name}_policy.rb"
  say "  3. bin/rails test"
end