Skip to content

feat: pass the model and attribute name to writable guards - #511

Merged
jkeen merged 1 commit into
mainfrom
feature/writable-guard-args
Jul 29, 2026
Merged

jkeen merged 1 commit into
mainfrom
feature/writable-guard-args

Conversation

@jkeen

@jkeen jkeen commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Writable guards can now receive the model being written and the name of the guarded attribute, mirroring what readable guards have long supported at render time. This resolves the remaining piece of #481

attribute :salary, :integer, writable: :salary_writable?

def salary_writable?(model, attribute_name)
  PolicyChecker.new(model).attribute_writable?(attribute_name)
end

Dispatch is arity-based, just like the readable guards: zero parameters is called as before, one receives the model, two receive both. Symbols, strings, and procs all work.

A few deliberate choices:

  • The guard sees the pre-write state — a fresh find on update, a new unsaved instance on create. That's what a policy object expects for "may this user touch this attribute", not a validation of the incoming values.
  • The model resolves lazily and memoizes — many guarded attributes cost at most one find, and zero-arg guards pay nothing (spec-enforced).

Fully backwards compatible: existing guards all have arity zero and dispatch identically to today.

@jkeen
jkeen force-pushed the feature/writable-guard-args branch from 32d240f to 58a5ed7 Compare July 29, 2026 03:07
@jkeen
jkeen merged commit 24a8a94 into main Jul 29, 2026
51 checks passed
github-actions Bot pushed a commit that referenced this pull request Jul 29, 2026
# [1.12.0](v1.11.1...v1.12.0) (2026-07-29)

### Bug Fixes

* GQL name chaining ([#415](#415)) [skip ci] ([930928a](930928a))

### Features

* conditional relationships evaluate readable/writable guards per-request ([#450](#450)) ([3ed3187](3ed3187))
* pass the model and attribute name to writable guards ([#511](#511)) [skip ci] ([24a8a94](24a8a94))
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

jkeen added a commit to jkeen/graphiti that referenced this pull request Jul 30, 2026
jkeen pushed a commit to jkeen/graphiti that referenced this pull request Jul 30, 2026
# [1.12.0](graphiti-api/graphiti@v1.11.1...v1.12.0) (2026-07-29)

### Bug Fixes

* GQL name chaining ([graphiti-api#415](graphiti-api#415)) [skip ci] ([930928a](graphiti-api@930928a))

### Features

* conditional relationships evaluate readable/writable guards per-request ([graphiti-api#450](graphiti-api#450)) ([3ed3187](graphiti-api@3ed3187))
* pass the model and attribute name to writable guards ([graphiti-api#511](graphiti-api#511)) [skip ci] ([24a8a94](graphiti-api@24a8a94))
@jkeen
jkeen deleted the feature/writable-guard-args branch July 30, 2026 20:58
@jkeen jkeen mentioned this pull request Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant