Class: Spree::ApiResourceGenerator
- Inherits:
-
ModelGenerator
- Object
- ModelGenerator
- Spree::ApiResourceGenerator
- Defined in:
- lib/generators/spree/api_resource/api_resource_generator.rb
Overview
spree:api_resource — scaffold a complete v3-conformant API resource on
top of spree:model.
bin/rails g spree:api_resource Brand name:string:uniq active:boolean --writable
Inherits from Spree::ModelGenerator (model + migration with Spree conventions: prefixed IDs, spree_-prefixed tables, null: false, optional acts_as_paranoid + Spree::HasCustomFields). Adds on top:
- Store + Admin controllers (managed — overwrite on re-run)
- Store + Admin serializers (managed — overwrite on re-run)
- Factory (managed — overwrite on re-run)
- Controller specs (managed — overwrite on re-run)
- Routes (idempotent inject between sentinels)
- Permission scope + label (idempotent inject; locale owned-once)
Owned-once contract: if the model file already exists, the generator leaves it (and the migration) alone — domain code is yours after creation. Re-running adds/updates API surfaces only.
TypeScript types and Zod schemas regenerate automatically via the Lefthook pre-commit hook when a serializer file is staged.
See docs/plans/spree-dev-cli-and-generators.md (Track 3) for the owned-once / managed-forever / append-only contract.
Class Method Summary collapse
-
.source_paths ⇒ Object
API-specific templates live alongside this generator.
Instance Method Summary collapse
- #create_admin_controller ⇒ Object
- #create_admin_serializer ⇒ Object
- #create_controller_specs ⇒ Object
- #create_factory ⇒ Object
-
#create_migration_file ⇒ Object
Override parent: skip if the model existed before this run.
-
#create_model_file ⇒ Object
Override parent: skip if the model file already exists.
- #create_permission_locale ⇒ Object
-
#create_store_controller ⇒ Object
--- API surface ---.
- #create_store_serializer ⇒ Object
-
#initialize(*args) ⇒ ApiResourceGenerator
constructor
--- Owned-once gating ---.
- #inject_routes ⇒ Object
- #print_summary ⇒ Object
-
#register_permission_scope ⇒ Object
The Admin controller declares
scoped_resource :<plural>; without a matching catalog scope its keys cannot be granted to a staff role or minted on a secret API key, so only full-access principals get in.
Constructor Details
#initialize(*args) ⇒ ApiResourceGenerator
--- Owned-once gating ---
Thor's parent commands run BEFORE subclass commands (commands merge from superclass first). So we can't snapshot model existence in a subclass action and have parent's create_model_file see it. We capture state in initialize, before any action runs.
94 95 96 97 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 94 def initialize(*args) super @model_existed_before_run = File.exist?(File.join(destination_root, model_file_destination)) end |
Class Method Details
.source_paths ⇒ Object
API-specific templates live alongside this generator. Parent's templates (model.rb.tt, create_table_migration.rb.tt) are inherited via Spree::ModelGenerator.source_paths.
35 36 37 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 35 def self.source_paths [File.('templates', __dir__), *superclass.source_paths] end |
Instance Method Details
#create_admin_controller ⇒ Object
128 129 130 131 132 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 128 def create_admin_controller return unless [:admin] template 'admin_controller.rb.tt', admin_controller_path end |
#create_admin_serializer ⇒ Object
147 148 149 150 151 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 147 def create_admin_serializer return unless [:admin] template 'admin_serializer.rb.tt', admin_serializer_path end |
#create_controller_specs ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 162 def create_controller_specs return if [:skip_specs] if [:store] template 'store_controller_spec.rb.tt', "spec/controllers/spree/api/v3/store/#{plural_name}_controller_spec.rb" end if [:admin] template 'admin_controller_spec.rb.tt', "spec/controllers/spree/api/v3/admin/#{plural_name}_controller_spec.rb" # Documents the endpoints in openapi/admin.yaml (`spree task # openapi:generate`): the SDK request types and the agent tools come # from there. template 'admin_integration_spec.rb.tt', "spec/integration/admin/#{plural_name}_spec.rb" end end |
#create_factory ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 153 def create_factory # spec/factories/ is the FactoryBot default scan path that a freshly- # generated `rspec:install` + `factory_bot_rails` setup already picks # up via `FactoryBot.find_definitions`. Spree's own factories live # under lib/spree/testing_support/factories/ because that path is # exported by gems; downstream apps don't have that loader by default. template 'factory.rb.tt', "spec/factories/spree/#{singular_name}_factory.rb" end |
#create_migration_file ⇒ Object
Override parent: skip if the model existed before this run. Migrations are append-only — schema changes get a separate migration:
pnpm exec spree generate migration AddFooToBar foo:string
112 113 114 115 116 117 118 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 112 def create_migration_file if @model_existed_before_run say_status :skip, 'migration (model already exists; add a new migration for schema changes)', :yellow return end super end |
#create_model_file ⇒ Object
Override parent: skip if the model file already exists. Re-running never overwrites domain code.
101 102 103 104 105 106 107 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 101 def create_model_file if @model_existed_before_run say_status :skip, "model #{model_file_destination} (owned-once; already exists)", :yellow return end super end |
#create_permission_locale ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 212 def return unless [:admin] path = "config/locales/spree_#{plural_name}.en.yml" if File.exist?(File.join(destination_root, path)) say_status :skip, "#{path} (owned-once; already exists)", :yellow return end template 'permissions.en.yml.tt', path end |
#create_store_controller ⇒ Object
--- API surface ---
122 123 124 125 126 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 122 def create_store_controller return unless [:store] template 'store_controller.rb.tt', store_controller_path end |
#create_store_serializer ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 134 def create_store_serializer return unless [:store] template 'store_serializer.rb.tt', store_serializer_path # --store-name aliases the store-facing class under a different name # (e.g. Brand → Discount) while keeping the model/table internal. if store_external_name != template 'store_aliased_serializer.rb.tt', "app/serializers/spree/api/v3/#{store_external_name.underscore}_serializer.rb" end end |
#inject_routes ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 179 def inject_routes return if [:skip_routes] routes_file = api_routes_path # An installed gem is read-only, which is the normal case: a host app # declares its own routes instead, through the engine's route hook. unless File.exist?(routes_file) && File.writable?(routes_file) draw_app_routes return end inject_route_for(:store, store_route_line) if [:store] inject_route_for(:admin, admin_route_line) if [:admin] end |
#print_summary ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 280 def print_summary say '' say "✓ Generated Spree::#{} API resource", :green say '' say " Prefixed ID: #{id_prefix}_xxxxxxxxxx" if store_external_name != say " Store API: /api/v3/store/#{store_external_plural} (aliased from #{})" elsif [:store] say " Store API: /api/v3/store/#{plural_name} (#{writable? ? 'full CRUD' : 'read-only'})" end say " Admin API: /api/v3/admin/#{plural_name} (full CRUD)" if [:admin] say '' say ' Next steps:', :yellow say ' 1. Review the generated model — add validations, scopes, callbacks' say ' 2. Apply the migration: pnpm exec spree migrate' if [:admin] say " 3. Grant read_#{plural_name} / write_#{plural_name} to staff roles or secret API keys" else say ' 3. Add an Admin API resource later to manage it from the dashboard' end say " 4. Add `has_many :#{plural_name}` to Spree::Store" if store_scoped? if [:store] || [:admin] say " #{store_scoped? ? 5 : 4}. Run the specs: pnpm exec spree test spec/controllers/spree/api/v3/" end if [:admin] && ![:skip_specs] say " #{store_scoped? ? 6 : 5}. Document the Admin API (agent tools, SDK types): pnpm exec spree task openapi:generate" end say '' end |
#register_permission_scope ⇒ Object
The Admin controller declares scoped_resource :<plural>; without a
matching catalog scope its keys cannot be granted to a staff role or
minted on a secret API key, so only full-access principals get in.
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/generators/spree/api_resource/api_resource_generator.rb', line 198 def return unless [:admin] if File.exist?(File.join(destination_root, INITIALIZER_PATH)) if File.read(File.join(destination_root, INITIALIZER_PATH)).include?("register_scope(:#{plural_name},") say_status :identical, "#{INITIALIZER_PATH} (#{plural_name} permission scope)", :blue else append_to_file INITIALIZER_PATH, end else create_file INITIALIZER_PATH, .lstrip end end |