azeroth 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +21 -10
- data/.claude/agents/architect.md +55 -0
- data/.claude/agents/docs.md +38 -0
- data/.claude/agents/ruby.md +48 -0
- data/.claude/agents/security.md +27 -0
- data/.claude/configuration/auto-fix-all.json +3 -0
- data/.claude/scripts/check_ruby.sh +8 -0
- data/.github/commit_message_template.md +8 -0
- data/.github/copilot-instructions.md +1 -70
- data/.github/pull_request_template.md +17 -0
- data/.gitignore +1 -0
- data/AGENTS.md +103 -0
- data/CLAUDE.md +1 -0
- data/Dockerfile +2 -2
- data/Gemfile +20 -17
- data/README.md +8 -8
- data/azeroth.gemspec +3 -3
- data/azeroth.jpg +0 -0
- data/docs/agents/architecture.md +79 -0
- data/docs/agents/contributing.md +195 -0
- data/docs/agents/flow.md +5 -0
- data/docs/agents/folder-structure.md +43 -0
- data/docs/agents/issue-enhancement.md +10 -0
- data/docs/agents/issues/.gitkeep +1 -0
- data/docs/agents/plans/.gitkeep +1 -0
- data/docs/guides/azeroth/basic-usage.md +66 -0
- data/docs/guides/azeroth/best-practices.md +9 -0
- data/docs/guides/azeroth/decorators.md +58 -0
- data/docs/guides/azeroth/installation.md +15 -0
- data/docs/guides/azeroth/model-for.md +40 -0
- data/docs/guides/azeroth/resource-for-options.md +64 -0
- data/docs/guides/azeroth/testing.md +30 -0
- data/docs/guides/azeroth-usage.md +28 -0
- data/lib/azeroth/version.rb +1 -1
- data/scripts/bump_version.sh +62 -0
- data/spec/dummy/bin/setup +1 -2
- data/spec/dummy/bin/update +1 -2
- data/spec/dummy/db/schema.rb +2 -2
- metadata +35 -13
- data/.github/azeroth-usage.md +0 -277
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Azeroth is a Ruby gem that extends Rails controllers with generated CRUD-style
|
|
6
|
+
action methods. Including `Azeroth::Resourceable` and calling `resource_for`
|
|
7
|
+
(or `model_for`) triggers a chain of builder classes that append methods to
|
|
8
|
+
the controller for handling requests, params, routes/resources, and JSON
|
|
9
|
+
rendering — for both `.html` and `.json` formats.
|
|
10
|
+
|
|
11
|
+
All source code lives under `lib/azeroth/`, loaded lazily via `autoload` from
|
|
12
|
+
`lib/azeroth.rb`. Most classes carry `@api public` or `@api private` YARD
|
|
13
|
+
tags; only `Resourceable`, `Decorator`, `resource_for`, and `model_for` are
|
|
14
|
+
public API — everything else is an internal implementation detail and can
|
|
15
|
+
change without a major version bump.
|
|
16
|
+
|
|
17
|
+
## Source Code Layout
|
|
18
|
+
|
|
19
|
+
All application source code lives under `lib/azeroth/`.
|
|
20
|
+
|
|
21
|
+
### `resourceable.rb` / `resourceable/`
|
|
22
|
+
|
|
23
|
+
Public entry point. `Resourceable` is an `ActiveSupport::Concern` mixed into
|
|
24
|
+
controllers; `Resourceable::ClassMethods` exposes `resource_for` and
|
|
25
|
+
`model_for`. `EndpointsBuilder` and `ResourcesBuilder` orchestrate the other
|
|
26
|
+
builders (`RequestHandler`, `ParamsBuilder`, `RoutesBuilder`,
|
|
27
|
+
`ResourceBuilder`) to generate the requested action methods on the including
|
|
28
|
+
controller.
|
|
29
|
+
|
|
30
|
+
### `decorator.rb` / `decorator/`
|
|
31
|
+
|
|
32
|
+
Public JSON serialization layer. `Decorator` is subclassed per-resource and
|
|
33
|
+
uses the `expose` class method to declare which attributes render in JSON
|
|
34
|
+
responses. `HashBuilder` and `KeyValueExtractor` turn exposed attributes into
|
|
35
|
+
the output hash; `MethodBuilder` generates the reader methods for exposed
|
|
36
|
+
attributes; `Options` holds per-`expose` configuration (`as`, `if`,
|
|
37
|
+
`decorator`, `reader`, `override`).
|
|
38
|
+
|
|
39
|
+
### `dummy_decorator.rb`
|
|
40
|
+
|
|
41
|
+
Fallback decorator used when a resource has no explicit `Decorator` subclass
|
|
42
|
+
— delegates straight to the object's `as_json`.
|
|
43
|
+
|
|
44
|
+
### `request_handler.rb` / `request_handler/`
|
|
45
|
+
|
|
46
|
+
One class per controller action (`Create`, `Show`, `Index`, `Update`,
|
|
47
|
+
`Destroy`, `Edit`, `New`), each responsible for executing that action's logic
|
|
48
|
+
(building/finding/saving the resource, invoking `before_save`/`after_save`
|
|
49
|
+
hooks) and telling the controller how to respond. `Pagination` implements the
|
|
50
|
+
`paginated: true` opt-in behavior and the `pages`/`per_page`/`page` response
|
|
51
|
+
headers.
|
|
52
|
+
|
|
53
|
+
### `resource_builder.rb`, `routes_builder.rb`, `params_builder.rb`
|
|
54
|
+
|
|
55
|
+
`Sinclair::Model`-based builders invoked by `Resourceable` to append,
|
|
56
|
+
respectively: resource-listing/fetching methods, route-action methods, and
|
|
57
|
+
params-handling methods to the including controller.
|
|
58
|
+
|
|
59
|
+
### `model.rb`
|
|
60
|
+
|
|
61
|
+
Interface wrapping the resource's `ActiveRecord` model class, resolving it
|
|
62
|
+
from either a class or a name symbol/string passed to `resource_for`.
|
|
63
|
+
|
|
64
|
+
### `controller_interface.rb`
|
|
65
|
+
|
|
66
|
+
Thin wrapper around the including controller instance, used by request
|
|
67
|
+
handlers to set response headers and otherwise interact with the controller
|
|
68
|
+
without coupling directly to Rails internals.
|
|
69
|
+
|
|
70
|
+
### `options.rb`
|
|
71
|
+
|
|
72
|
+
`Azeroth::Options` (subclass of `Sinclair::Options`) holding the defaults and
|
|
73
|
+
validation for `resource_for`/`model_for` options (`only`, `except`,
|
|
74
|
+
`id_key`, `param_key`, `paginated`, `per_page`, `before_save`, `after_save`,
|
|
75
|
+
`build_with`, `update_with`, `decorator`).
|
|
76
|
+
|
|
77
|
+
### `version.rb`
|
|
78
|
+
|
|
79
|
+
Gem version constant.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Commit Guidelines
|
|
4
|
+
|
|
5
|
+
- **Atomic and Unitary:** Each commit must represent a single logical change.
|
|
6
|
+
*Example:*
|
|
7
|
+
- Good: `Add pagination headers to RequestHandler::Index`
|
|
8
|
+
- Bad: `Add pagination headers and refactor Decorator logic`
|
|
9
|
+
- **No Unrelated Changes:** Do not mix unrelated changes in the same commit.
|
|
10
|
+
- **Separate Refactoring:** Whenever possible, separate refactoring commits from new feature or bugfix commits.
|
|
11
|
+
|
|
12
|
+
## Pull Requests
|
|
13
|
+
|
|
14
|
+
- **Descriptive Summary:** Every PR must include a clear and descriptive summary of its purpose and changes.
|
|
15
|
+
- **PR Description Files:** If a description cannot be provided directly in the PR, generate a file with the PR description (e.g., `docs/agents/issues/<issue_id>_description.md`), but do not commit this file.
|
|
16
|
+
|
|
17
|
+
## Definition of Done for PRs
|
|
18
|
+
|
|
19
|
+
A PR is considered complete when:
|
|
20
|
+
|
|
21
|
+
- The stated objective has been achieved.
|
|
22
|
+
- All tests are passing.
|
|
23
|
+
- Linting (RuboCop) passes without errors.
|
|
24
|
+
- Code coverage is as high as reasonably possible.
|
|
25
|
+
- Code is not overly complex:
|
|
26
|
+
- Classes and methods should have clear, focused responsibilities.
|
|
27
|
+
- If a class or method is taking on too many responsibilities, refactor to simplify.
|
|
28
|
+
- Methods should be small and do exactly one thing. If a method is growing, extract parts into private helper methods or separate classes.
|
|
29
|
+
- *Example:*
|
|
30
|
+
```ruby
|
|
31
|
+
# Good: each method does one thing
|
|
32
|
+
class Worker
|
|
33
|
+
def fetch_job; end
|
|
34
|
+
def process_job(job); end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Bad: method does too much
|
|
38
|
+
class Worker
|
|
39
|
+
def run
|
|
40
|
+
fetch_job
|
|
41
|
+
process_job
|
|
42
|
+
send_metrics
|
|
43
|
+
cleanup
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
- This requirement applies primarily to source code. For specs, refactor only if there is excessive duplication.
|
|
48
|
+
|
|
49
|
+
### CI Checks
|
|
50
|
+
|
|
51
|
+
Before a PR is considered complete, all CI checks relevant to the modified parts of the project must pass locally. This project has a single CircleCI pipeline (`.circleci/config.yml`) that runs against the whole gem — there are no per-folder path filters, so every check below applies regardless of which files were modified.
|
|
52
|
+
|
|
53
|
+
| CircleCI Job | Local Command(s) |
|
|
54
|
+
|--------------|-------------------|
|
|
55
|
+
| `test` (RSpec) | `bundle exec rspec` |
|
|
56
|
+
| `checks` — RuboCop | `bundle exec rubocop` |
|
|
57
|
+
| `checks` — Yardstick documentation coverage | `bundle exec rake verify_measurements` |
|
|
58
|
+
| `checks` — Unit test coverage check | `bundle exec check_specs` (see `config/check_specs.yml`) |
|
|
59
|
+
| `checks` — RubyCritic | `rubycritic.sh` (or `bundle exec rubycritic` locally) |
|
|
60
|
+
|
|
61
|
+
Run all of the above before opening a PR. If a new job or check is added to `.circleci/config.yml` in the future, add its local command to this table before merging changes that would be affected by it.
|
|
62
|
+
|
|
63
|
+
This same process must be followed when **planning how to resolve an issue**: include a final step in the plan that lists the CI commands above to run before opening a PR.
|
|
64
|
+
|
|
65
|
+
## Code Organization
|
|
66
|
+
|
|
67
|
+
### File Responsibility: Class Declarers vs Scripts
|
|
68
|
+
|
|
69
|
+
Every source file under `lib/` (excluding test files) must act as a **class or module declarer** — it should define one class or module and nothing else. Files must not act as **scripts** (i.e., they must not execute logic at load time or perform side effects directly).
|
|
70
|
+
|
|
71
|
+
`lib/azeroth.rb` is the gem's entrypoint: it `require`s dependencies and declares the `Azeroth` module with `autoload` statements for every other file. It does not perform side effects beyond loading.
|
|
72
|
+
|
|
73
|
+
*Example:*
|
|
74
|
+
```ruby
|
|
75
|
+
# Good: class declarer — defines a class, no side effects at load time
|
|
76
|
+
class RoutesBuilder < Sinclair::Model
|
|
77
|
+
def append
|
|
78
|
+
# ...
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Bad: script — executes logic at file load time
|
|
83
|
+
builder = RoutesBuilder.new
|
|
84
|
+
builder.append
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Spec files are exempt from this rule and may execute setup code freely (e.g. `RSpec.describe`, `let`, `before` blocks).
|
|
88
|
+
|
|
89
|
+
### File Naming: snake_case Matching the Class/Module Name
|
|
90
|
+
|
|
91
|
+
Files that define a class or module must use **snake_case** naming, matching the class/module name exactly, with nested namespaces mirrored as subdirectories.
|
|
92
|
+
|
|
93
|
+
*Examples:*
|
|
94
|
+
|
|
95
|
+
- `RoutesBuilder` → `lib/azeroth/routes_builder.rb`
|
|
96
|
+
- `Decorator::MethodBuilder` → `lib/azeroth/decorator/method_builder.rb`
|
|
97
|
+
- `RequestHandler::Index` → `lib/azeroth/request_handler/index.rb`
|
|
98
|
+
|
|
99
|
+
This applies to both source files and their corresponding spec files, mirrored under `spec/lib/`:
|
|
100
|
+
- `lib/azeroth/routes_builder.rb` → spec: `spec/lib/azeroth/routes_builder_spec.rb`
|
|
101
|
+
- `lib/azeroth/decorator/method_builder.rb` → spec: `spec/lib/azeroth/decorator/method_builder_spec.rb`
|
|
102
|
+
|
|
103
|
+
Non-class files (e.g. `config/*.rb` tooling config) use lowercase or snake_case at the author's discretion.
|
|
104
|
+
|
|
105
|
+
### Method Order: Public Before Private
|
|
106
|
+
|
|
107
|
+
Within a class, **public methods must be declared before private methods**. Private methods (below the `private` keyword) serve as implementation helpers and should appear at the end of the class body.
|
|
108
|
+
|
|
109
|
+
*Example:*
|
|
110
|
+
```ruby
|
|
111
|
+
# Good: public methods first, private methods last
|
|
112
|
+
class Worker
|
|
113
|
+
def run
|
|
114
|
+
prepare
|
|
115
|
+
execute
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def status
|
|
119
|
+
# ...
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def prepare; end
|
|
125
|
+
def execute; end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Bad: private methods mixed in with or before public methods
|
|
129
|
+
class Worker
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def prepare; end
|
|
133
|
+
|
|
134
|
+
public
|
|
135
|
+
|
|
136
|
+
def run
|
|
137
|
+
prepare
|
|
138
|
+
execute
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Dependency Injection
|
|
144
|
+
|
|
145
|
+
Classes must receive their dependencies (data, configuration, collaborators) as constructor arguments (via `initialize` or `initialize_with`, as used by `Sinclair::Model` subclasses in this codebase). A class must never reach out to load files, read environment variables, or fetch configuration on its own.
|
|
146
|
+
|
|
147
|
+
**The entry points — `Resourceable::ClassMethods#resource_for`/`#model_for` — are responsible for gathering configuration** (the options hash passed by the including controller). They then pass the resolved data down to the builder and handler classes that need it.
|
|
148
|
+
|
|
149
|
+
This makes every class independently testable: tests simply instantiate the class with the data they need, without touching the filesystem, environment, or a real Rails app beyond the `spec/dummy` app used for integration specs.
|
|
150
|
+
|
|
151
|
+
*Example:*
|
|
152
|
+
```ruby
|
|
153
|
+
# Good: class receives its collaborators as constructor arguments — easy to test
|
|
154
|
+
class RoutesBuilder < Sinclair::Model
|
|
155
|
+
initialize_with(:model, :builder, :options, writter: false)
|
|
156
|
+
|
|
157
|
+
def append
|
|
158
|
+
# uses @model, @builder, @options
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Bad: class reaches out and resolves its own dependencies — hard to test
|
|
163
|
+
class RoutesBuilder
|
|
164
|
+
def append
|
|
165
|
+
model = Model.new(ApplicationController.resource_name, Options.new) # ❌
|
|
166
|
+
# ...
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This principle applies to all classes — including builders and handlers. If a class needs data, it gets it through its constructor.
|
|
172
|
+
|
|
173
|
+
## Refactoring Guidelines
|
|
174
|
+
|
|
175
|
+
When refactoring, aim to:
|
|
176
|
+
|
|
177
|
+
- **Reduce Code Duplication:**
|
|
178
|
+
*Example:* Move repeated setup code in specs to a factory (via `factory_bot`, already used under `spec/support/factories/`).
|
|
179
|
+
```ruby
|
|
180
|
+
# Good
|
|
181
|
+
FactoryBot.define do
|
|
182
|
+
factory :publisher do
|
|
183
|
+
name { 'Books Inc' }
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
# In specs:
|
|
187
|
+
let(:publisher) { create(:publisher, name: 'Other') }
|
|
188
|
+
|
|
189
|
+
# Bad
|
|
190
|
+
let(:publisher) { Publisher.create!(name: 'Other') }
|
|
191
|
+
# ...repeated setup logic in many spec files
|
|
192
|
+
```
|
|
193
|
+
- **Extract Shared Behavior:** Use RSpec `shared_examples` (see `spec/support/shared_examples/`) instead of copy-pasting near-identical spec blocks across request handlers or builders.
|
|
194
|
+
- **Prefer Composition Over Inheritance:** Favor small collaborator objects (as this codebase already does with its builder classes) over deep inheritance hierarchies.
|
|
195
|
+
- **Keep RuboCop Clean:** Do not silence RuboCop offenses with inline disables; if a rule genuinely doesn't fit, add a documented exception to `.rubocop_todo.yml` or `.rubocop.yml` instead.
|
data/docs/agents/flow.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Folder Structure
|
|
2
|
+
|
|
3
|
+
## Project Root
|
|
4
|
+
|
|
5
|
+
| Directory / File | Description |
|
|
6
|
+
|-----------------|-------------|
|
|
7
|
+
| `lib/` | Gem source code — the `Azeroth` module and all its classes. |
|
|
8
|
+
| `spec/` | RSpec test suite, including a Rails dummy app used for integration tests. |
|
|
9
|
+
| `docs/agents/` | Agent-facing documentation (architecture, flow, issues, plans). |
|
|
10
|
+
| `docs/guides/` | User-facing usage guides, including [`azeroth-usage.md`](../guides/azeroth-usage.md) — the gem usage guide meant to be copied into consumer projects. |
|
|
11
|
+
| `config/` | Tooling configuration: `check_specs.yml` (spec coverage exceptions), `yardstick.yml`/`yardstick.rb` (YARD doc coverage), `rubycritc.rb` (RubyCritic config). |
|
|
12
|
+
| `bin/` | Executable scripts, e.g. `bin/test`. |
|
|
13
|
+
| `.github/` | GitHub PR/commit templates and dependency usage guides (`jace-usage.md`, `core_ext-usage.md`, `active_ext-usage.md`), plus `copilot-instructions.md` pointing to `AGENTS.md`. |
|
|
14
|
+
| `.circleci/` | CircleCI pipeline configuration. |
|
|
15
|
+
| `doc/` | Generated YARD API documentation (build output, not hand-maintained). |
|
|
16
|
+
| `measurement/` | Generated YARD/yardstick coverage report (`report.txt`). |
|
|
17
|
+
| `rubycritic/` | Generated RubyCritic code quality report. |
|
|
18
|
+
| `coverage/` | Generated test coverage report (SimpleCov). |
|
|
19
|
+
| `AGENTS.md` | Project instructions for AI coding agents (source of truth; `CLAUDE.md` and `.github/copilot-instructions.md` point here). |
|
|
20
|
+
| `README.md` | Gem usage documentation and examples. |
|
|
21
|
+
| `azeroth.gemspec`| Gem specification (dependencies, metadata). |
|
|
22
|
+
| `Gemfile` / `Gemfile.lock` | Bundler dependency management. |
|
|
23
|
+
| `Rakefile` | Rake tasks (tests, docs, etc). |
|
|
24
|
+
| `Dockerfile` / `docker-compose.yml` | Containerized dev/test environment. |
|
|
25
|
+
| `Makefile` | Shortcut commands for common dev tasks. |
|
|
26
|
+
|
|
27
|
+
## lib/azeroth (Architecture)
|
|
28
|
+
|
|
29
|
+
| Subdirectory | Description |
|
|
30
|
+
|--------------|-------------|
|
|
31
|
+
| `resourceable/` | Implements `resource_for`/`model_for`, generating controller action methods. |
|
|
32
|
+
| `decorator/` | Implements `Azeroth::Decorator`, controlling JSON serialization via `expose`. |
|
|
33
|
+
| `request_handler/` | Per-action request handling logic (create, show, index, update, delete, edit). |
|
|
34
|
+
|
|
35
|
+
## spec/ (Test Suite Layout)
|
|
36
|
+
|
|
37
|
+
| Subdirectory | Description |
|
|
38
|
+
|--------------|-------------|
|
|
39
|
+
| `dummy/` | Minimal Rails application used as the host app for integration/controller specs. |
|
|
40
|
+
| `integration/`| End-to-end specs, including README example verification (`readme/`) and YARD doc checks (`yard/`). |
|
|
41
|
+
| `controllers/`| Controller specs exercising `resource_for`-generated actions. |
|
|
42
|
+
| `lib/azeroth/`| Unit specs mirroring the `lib/azeroth/` source layout. |
|
|
43
|
+
| `support/` | Shared RSpec configuration: `factories/`, `matchers/`, `shared_examples/`, and a test `app/`. |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Issue Enhancement
|
|
2
|
+
|
|
3
|
+
A checklist of concerns to consider when fleshing out a vague issue idea (tagged `Idea`/`Writting`) before it reaches the `Created` stage. Not exhaustive — adjust or extend the list for this project's needs.
|
|
4
|
+
|
|
5
|
+
- **Scope boundaries** — what's explicitly in scope and what's explicitly out.
|
|
6
|
+
- **Alternative solutions** — other ways to solve the same problem, and why this one was chosen.
|
|
7
|
+
- **Edge cases** — inputs, states, or timing the happy path doesn't cover.
|
|
8
|
+
- **Backward compatibility** — whether this breaks existing behavior, data, or integrations.
|
|
9
|
+
- **Testing strategy** — how the change will be verified.
|
|
10
|
+
- **Performance & security considerations** — anything relevant to load, latency, or attack surface.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# Basic Usage
|
|
4
|
+
|
|
5
|
+
## Setting Up `resource_for`
|
|
6
|
+
|
|
7
|
+
Include `Azeroth::Resourceable` in your controller and call `resource_for` with the resource name:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
class PublishersController < ApplicationController
|
|
11
|
+
include Azeroth::Resourceable
|
|
12
|
+
skip_before_action :verify_authenticity_token
|
|
13
|
+
|
|
14
|
+
resource_for :publisher, only: %i[create index]
|
|
15
|
+
end
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This generates the following behavior:
|
|
19
|
+
|
|
20
|
+
- `GET /publishers` → calls `index`, returns all publishers
|
|
21
|
+
- `POST /publishers` → calls `create`, creates a new publisher
|
|
22
|
+
|
|
23
|
+
## Available Actions
|
|
24
|
+
|
|
25
|
+
By default, `resource_for` builds all seven standard actions: `create`, `show`,
|
|
26
|
+
`index`, `update`, `destroy`, `edit`, and `new`. Use `only:` or `except:` to
|
|
27
|
+
restrict which actions are generated:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
# Only generate index and show
|
|
31
|
+
resource_for :article, only: %i[index show]
|
|
32
|
+
|
|
33
|
+
# Generate everything except destroy
|
|
34
|
+
resource_for :article, except: :destroy
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
> Note: the action generated for deleting a resource is named `destroy`
|
|
38
|
+
> (matching Rails' own controller action naming), not `delete`. Pass
|
|
39
|
+
> `except: :destroy` — not `except: :delete` — to skip it.
|
|
40
|
+
|
|
41
|
+
## Nested Resources
|
|
42
|
+
|
|
43
|
+
Override the collection method to scope resources to a parent:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class GamesController < ApplicationController
|
|
47
|
+
include Azeroth::Resourceable
|
|
48
|
+
skip_before_action :verify_authenticity_token
|
|
49
|
+
|
|
50
|
+
resource_for :game, except: :destroy
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def games
|
|
55
|
+
publisher.games
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def publisher
|
|
59
|
+
@publisher ||= Publisher.find(publisher_id)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def publisher_id
|
|
63
|
+
params.require(:publisher_id)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# Best Practices
|
|
4
|
+
|
|
5
|
+
- **Use `only:` / `except:`** to expose only the actions your controller actually needs.
|
|
6
|
+
- **Override collection methods** (e.g., `def games`) to scope resources to a parent instead of returning the full table.
|
|
7
|
+
- **Use decorators** to keep serialization logic out of controllers and models.
|
|
8
|
+
- **Use `before_save` / `after_save`** callbacks for business logic that must run around persistence, rather than overriding generated action methods.
|
|
9
|
+
- **Prefer `resource_for`** over hand-written CRUD actions for standard resources to ensure consistent behavior across your application.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# JSON Serialization with `Azeroth::Decorator`
|
|
4
|
+
|
|
5
|
+
Decorators control which attributes are included in JSON responses.
|
|
6
|
+
|
|
7
|
+
## Defining a Decorator
|
|
8
|
+
|
|
9
|
+
Create a decorator class that inherits from `Azeroth::Decorator` and call `expose` for each attribute to include:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
# app/decorators/pokemon/decorator.rb
|
|
13
|
+
class Pokemon::Decorator < Azeroth::Decorator
|
|
14
|
+
expose :name
|
|
15
|
+
expose :previous_form_name, as: :evolution_of, if: :evolution?
|
|
16
|
+
|
|
17
|
+
def evolution?
|
|
18
|
+
previous_form
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def previous_form_name
|
|
22
|
+
previous_form.name
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## `expose` Options
|
|
28
|
+
|
|
29
|
+
| Option | Type | Description |
|
|
30
|
+
|---|---|---|
|
|
31
|
+
| `as` | Symbol | Custom JSON key for the attribute |
|
|
32
|
+
| `if` | Symbol / Proc | Method or block; attribute is only included when it returns truthy |
|
|
33
|
+
| `decorator` | Class / Boolean | Nested decorator class or flag to enable/disable decoration |
|
|
34
|
+
| `reader` | Boolean | Whether to create a reader method for the attribute |
|
|
35
|
+
| `override` | Boolean | Whether to override an existing method with the same name |
|
|
36
|
+
|
|
37
|
+
## Decorator Inheritance
|
|
38
|
+
|
|
39
|
+
Extend decorators by subclassing them:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
# app/decorators/pokemon/favorite_decorator.rb
|
|
43
|
+
class Pokemon::FavoriteDecorator < Pokemon::Decorator
|
|
44
|
+
expose :nickname
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Using a Custom Decorator in a Controller
|
|
49
|
+
|
|
50
|
+
Pass the decorator class to `resource_for`:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
class PokemonsController < ApplicationController
|
|
54
|
+
include Azeroth::Resourceable
|
|
55
|
+
|
|
56
|
+
resource_for :pokemon, decorator: Pokemon::FavoriteDecorator
|
|
57
|
+
end
|
|
58
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# `model_for`
|
|
4
|
+
|
|
5
|
+
`model_for` adds singular and plural resource accessor methods to a controller (e.g., `document` and `documents`) **without** generating any HTTP route actions. This is useful when a controller needs to access a resource as context (for example a parent record) but should not expose CRUD endpoints for it.
|
|
6
|
+
|
|
7
|
+
## Options
|
|
8
|
+
|
|
9
|
+
| Option | Type | Description |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `id_key` | Symbol | Model attribute the record is looked up by (default: `:id`) |
|
|
12
|
+
| `param_key` | Symbol | Request parameter key read to get the lookup value (default: `:id`) |
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
class PostsController < ApplicationController
|
|
18
|
+
include Azeroth::Resourceable
|
|
19
|
+
|
|
20
|
+
# Generates full CRUD endpoints for Post,
|
|
21
|
+
# scoped to the author resolved by model_for below.
|
|
22
|
+
resource_for :post
|
|
23
|
+
|
|
24
|
+
# Only adds `author` and `authors` helper methods —
|
|
25
|
+
# no HTTP actions are created for Author.
|
|
26
|
+
model_for :author, id_key: :author_id
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def posts
|
|
31
|
+
author.posts
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
In this example:
|
|
37
|
+
|
|
38
|
+
- `GET /authors/:author_id/posts.json` returns all posts belonging to the author.
|
|
39
|
+
- `author` is resolved automatically via `Author.find(params[:author_id])`.
|
|
40
|
+
- No `/authors` CRUD endpoints are generated.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# `resource_for` Options
|
|
4
|
+
|
|
5
|
+
| Option | Type | Description |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| `only` | Symbol / Array | List of actions to generate |
|
|
8
|
+
| `except` | Symbol / Array | List of actions to skip |
|
|
9
|
+
| `decorator` | Class / Boolean | Decorator class or flag to enable/disable decoration |
|
|
10
|
+
| `before_save` | Symbol / Proc | Method or proc called before `create` or `update` saves |
|
|
11
|
+
| `after_save` | Symbol / Proc | Method or proc called after `create` or `update` saves |
|
|
12
|
+
| `build_with` | Symbol / Proc | Method or block used to build the resource on `create` |
|
|
13
|
+
| `update_with` | Symbol / Proc | Method or block used to update the resource on `update` |
|
|
14
|
+
| `paginated` | Boolean | Enable pagination on the `index` action |
|
|
15
|
+
| `per_page` | Integer | Number of items per page when pagination is active (default: 20) |
|
|
16
|
+
| `id_key` | Symbol | Model attribute the record is looked up by (default: `:id`) |
|
|
17
|
+
| `param_key` | Symbol | Request parameter key read to get the lookup value (default: `:id`) |
|
|
18
|
+
|
|
19
|
+
`id_key` and `param_key` work together: the resource is fetched with
|
|
20
|
+
`collection.find_by!(id_key => params[param_key])`. Change `id_key` to look
|
|
21
|
+
records up by a different column (e.g. `:slug`); change `param_key` when the
|
|
22
|
+
identifier arrives under a different param name (e.g. a nested route param
|
|
23
|
+
like `:document_id`).
|
|
24
|
+
|
|
25
|
+
## Callbacks: `before_save` and `after_save`
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class PokemonsController < ApplicationController
|
|
29
|
+
include Azeroth::Resourceable
|
|
30
|
+
|
|
31
|
+
resource_for :pokemon,
|
|
32
|
+
only: %i[create update],
|
|
33
|
+
before_save: :set_favorite
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def set_favorite
|
|
38
|
+
pokemon.favorite = true
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Pagination
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class DocumentsController < ApplicationController
|
|
47
|
+
include Azeroth::Resourceable
|
|
48
|
+
|
|
49
|
+
resource_for :document, only: :index, paginated: true, per_page: 10
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
A paginated `index` request returns the items for the requested page and sets the following response headers:
|
|
54
|
+
|
|
55
|
+
| Header | Description |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `pages` | Total number of pages |
|
|
58
|
+
| `per_page` | Number of items per page |
|
|
59
|
+
| `page` | Current page number |
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
GET /documents.json # page 1 – first 10 documents
|
|
63
|
+
GET /documents.json?page=2 # page 2 – next 10 documents
|
|
64
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[← Back to guide index](../azeroth-usage.md)
|
|
2
|
+
|
|
3
|
+
# Testing Controllers That Use Azeroth
|
|
4
|
+
|
|
5
|
+
Use standard Rails request specs or controller specs with RSpec. Since Azeroth generates conventional Rails actions, they can be exercised like any other controller action:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
RSpec.describe PokemonsController, type: :request do
|
|
9
|
+
let(:master) { create(:pokemon_master) }
|
|
10
|
+
|
|
11
|
+
describe 'POST /pokemons' do
|
|
12
|
+
it 'creates a pokemon' do
|
|
13
|
+
post "/pokemon_masters/#{master.id}/pokemons.json",
|
|
14
|
+
params: { pokemon: { name: 'Bulbasaur' } }
|
|
15
|
+
|
|
16
|
+
expect(response).to have_http_status(:created)
|
|
17
|
+
expect(response.parsed_body['name']).to eq('Bulbasaur')
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe 'GET /pokemons' do
|
|
22
|
+
it 'returns all pokemons' do
|
|
23
|
+
get "/pokemon_masters/#{master.id}/pokemons.json"
|
|
24
|
+
|
|
25
|
+
expect(response).to have_http_status(:ok)
|
|
26
|
+
expect(response.parsed_body).to be_an(Array)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
```
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Azeroth Gem Usage Instructions
|
|
2
|
+
|
|
3
|
+
This guide explains how to use the [Azeroth](https://github.com/darthjee/azeroth) gem in a Rails application. It is designed to be copied into other projects (e.g. as `docs/agents/external/azeroth-usage.md`) for AI coding agents to reference.
|
|
4
|
+
|
|
5
|
+
The links below are relative paths into the [`azeroth/`](azeroth/) folder next to this file — copy that folder alongside this one to keep them working, since this guide may be copied into projects that don't have the rest of Azeroth's own docs tree.
|
|
6
|
+
|
|
7
|
+
## About Azeroth
|
|
8
|
+
|
|
9
|
+
Azeroth is a Ruby gem that simplifies the creation of Rails controller endpoints. Its main feature is the `resource_for` class method, which automatically generates controller action methods (`create`, `show`, `index`, `update`, `destroy`, `edit`, `new`) and handles both HTML and JSON request formats transparently.
|
|
10
|
+
|
|
11
|
+
- HTML requests render templates without performing database operations.
|
|
12
|
+
- JSON requests perform database operations and return serialized JSON.
|
|
13
|
+
|
|
14
|
+
## Guide Contents
|
|
15
|
+
|
|
16
|
+
- [Installation](azeroth/installation.md)
|
|
17
|
+
- [Basic Usage](azeroth/basic-usage.md) — setting up `resource_for`, available actions, nested resources
|
|
18
|
+
- [`resource_for` Options](azeroth/resource-for-options.md) — options table, callbacks, pagination
|
|
19
|
+
- [Decorators](azeroth/decorators.md) — JSON serialization with `Azeroth::Decorator`
|
|
20
|
+
- [`model_for`](azeroth/model-for.md)
|
|
21
|
+
- [Best Practices](azeroth/best-practices.md)
|
|
22
|
+
- [Testing](azeroth/testing.md)
|
|
23
|
+
|
|
24
|
+
## Further Reading
|
|
25
|
+
|
|
26
|
+
- [Azeroth on RubyGems](https://rubygems.org/gems/azeroth)
|
|
27
|
+
- [YARD Documentation](https://www.rubydoc.info/gems/azeroth)
|
|
28
|
+
- [GitHub Repository](https://github.com/darthjee/azeroth)
|
data/lib/azeroth/version.rb
CHANGED