|
| 1 | +# AI Dev Guide - AMBER API |
| 2 | + |
| 3 | +## Env |
| 4 | +Container: `development-environment-alpha-1` |
| 5 | +Path in container: `~/amber-api` |
| 6 | +Shell: Must use `/bin/bash -l` for PATH |
| 7 | +CMD: `docker exec -it development-environment-alpha-1 /bin/bash -l -c "cd ~/amber-api && bundle exec <cmd>"` |
| 8 | +Direct: Enter container, `cd ~/amber-api`, then run `bundle exec <cmd>` |
| 9 | + |
| 10 | +## Rules |
| 11 | +1. Follow existing patterns |
| 12 | +2. Tests for everything (95%+ coverage) |
| 13 | +3. Follow RuboCop or document exception with reason |
| 14 | +4. Keep it simple: convention over configuration |
| 15 | +5. Don't add gems unless they have real benefit (student-maintained, avoid bloat) |
| 16 | + |
| 17 | +## RuboCop |
| 18 | +Config: `.rubocop.yml` |
| 19 | +Target: Rails 7.2, Ruby 3.3, NewCops: enable |
| 20 | +Disabled: HttpPositionalArguments, HasManyOrHasOneDependent, InverseOf, LexicallyScopedActionFilter, RSpec/LeadingSubject, Documentation, FrozenStringLiteralComment, GuardClause |
| 21 | +Modified: RSpec/NestedGroups Max:5, Style/ClassAndModuleChildren excludes v1 resources, Metrics/BlockLength excludes spec/** |
| 22 | +Exceptions: Must explain why in .rubocop.yml comments |
| 23 | + |
| 24 | +## Structure |
| 25 | +Resources: `app/resources/v1/*.rb` extends `V1::ApplicationResource < JSONAPI::Resource` |
| 26 | +Tests: `spec/resources/v1/*_spec.rb` type: :resource |
| 27 | +Factories: `spec/factories/*.rb` use FactoryBot + Faker |
| 28 | + |
| 29 | +## Resource Template |
| 30 | +```ruby |
| 31 | +# app/resources/v1/model_resource.rb |
| 32 | +class V1::ModelResource < V1::ApplicationResource |
| 33 | + attributes :attr1, :attr2 |
| 34 | + has_one :user |
| 35 | + has_many :items |
| 36 | + filter :field |
| 37 | + |
| 38 | + def self.creatable_fields(context) |
| 39 | + %i[attr1 attr2] |
| 40 | + end |
| 41 | + |
| 42 | + def self.updatable_fields(context) |
| 43 | + creatable_fields(context) |
| 44 | + end |
| 45 | + |
| 46 | + def self.searchable_fields |
| 47 | + %i[attr1 attr2] |
| 48 | + end |
| 49 | +end |
| 50 | +``` |
| 51 | + |
| 52 | +## Test Template |
| 53 | +```ruby |
| 54 | +# spec/resources/v1/model_resource_spec.rb |
| 55 | +require 'rails_helper' |
| 56 | + |
| 57 | +RSpec.describe V1::ModelResource, type: :resource do |
| 58 | + let(:user) { create(:user) } |
| 59 | + let(:context) { { user: } } |
| 60 | + |
| 61 | + describe '#creatable_fields' do |
| 62 | + it { expect(described_class.creatable_fields(context)).to match_array(%i[attr1 attr2]) } |
| 63 | + end |
| 64 | + |
| 65 | + describe '#updatable_fields' do |
| 66 | + it { expect(described_class.updatable_fields(context)).to match_array(%i[attr1 attr2]) } |
| 67 | + end |
| 68 | +end |
| 69 | +``` |
| 70 | + |
| 71 | +## Factory Template |
| 72 | +```ruby |
| 73 | +# spec/factories/models.rb |
| 74 | +FactoryBot.define do |
| 75 | + factory :model do |
| 76 | + user |
| 77 | + attr1 { Faker::Lorem.word } |
| 78 | + attr2 { [true, false].sample } |
| 79 | + end |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +## Test What |
| 84 | +Resources: creatable_fields, updatable_fields, fetchable_fields, searchable_fields, filters, relationships, attributes, permissions |
| 85 | +Models: validations, associations, scopes, methods, callbacks |
| 86 | +Controllers: auth, Pundit policies, response codes, response body, error handling |
| 87 | + |
| 88 | +## Commands |
| 89 | +Test all: `bundle exec rspec` |
| 90 | +Test file: `bundle exec rspec spec/path/to/file_spec.rb` |
| 91 | +Test line: `bundle exec rspec spec/path/to/file_spec.rb:12` |
| 92 | +RuboCop: `bundle exec rubocop` |
| 93 | +RuboCop fix: `bundle exec rubocop -a` |
| 94 | +Guard: `bundle exec guard` |
| 95 | +DB: `bundle exec rails db:migrate`, `bundle exec rails g migration Name` |
| 96 | + |
| 97 | +## Docker Commands |
| 98 | +Enter: `docker exec -it development-environment-alpha-1 /bin/bash -l` then `cd ~/amber-api` |
| 99 | +Start: `cd "C:/Users/jorai/Programeren/1. Alpha/1. Development/amber-api" && docker-compose -f docker-compose.development.yml up -d api` |
| 100 | +Stop: `docker-compose -f docker-compose.development.yml down` |
| 101 | +Logs: `docker logs development-environment-alpha-1` |
| 102 | + |
| 103 | +## Pitfalls |
| 104 | +- Don't use `docker exec ... bundle exec ...` directly (PATH + working dir issues) |
| 105 | +- Don't skip tests |
| 106 | +- Don't ignore RuboCop |
| 107 | +- Don't use hash rockets `{:key => val}` except in excluded dirs |
| 108 | +- Don't over-engineer |
| 109 | +- Don't use `_context` param if unused (keep it for consistency) |
| 110 | +- Do use existing patterns from ApplicationResource for permissions |
0 commit comments