fix: add back in writable? option to relationships, and add attribute_writable? to belongs_to

This commit is contained in:
Zach Daniel 2022-07-06 08:29:46 -04:00
parent b10b8e1dc2
commit 40904cae80
9 changed files with 54 additions and 7 deletions

View file

@ -18,6 +18,7 @@ locals_without_parens = [
attribute: 1,
attribute: 2,
attribute: 3,
attribute_writable?: 1,
authorize_if: 1,
authorize_if: 2,
authorize_unless: 1,

View file

@ -1796,6 +1796,15 @@ defmodule Ash.Changeset do
add_error(changeset, error)
%{writable?: false} = relationship ->
error =
InvalidRelationship.exception(
relationship: relationship.name,
message: "Relationship is not editable"
)
add_error(changeset, error)
%{manual: manual} = relationship when not is_nil(manual) ->
error =
InvalidRelationship.exception(

View file

@ -21,6 +21,7 @@ defmodule Ash.Resource.Relationships.BelongsTo do
:writable?,
:context,
:description,
:attribute_writable?,
validate_destination_field?: true,
cardinality: :one,
type: :belongs_to
@ -39,6 +40,8 @@ defmodule Ash.Resource.Relationships.BelongsTo do
primary_key?: boolean,
define_field?: boolean,
field_type: term,
writable?: boolean,
attribute_writable?: boolean,
destination_field: atom,
private?: boolean,
source_field: atom | nil,
@ -56,12 +59,6 @@ defmodule Ash.Resource.Relationships.BelongsTo do
@opt_schema Ash.OptionsHelpers.merge_schemas(
[
writable?: [
type: :boolean,
doc:
"Whether or not the attribute created by this relationship will be marked with `writable?: true`.",
default: false
],
primary_key?: [
type: :boolean,
default: false,
@ -73,6 +70,15 @@ defmodule Ash.Resource.Relationships.BelongsTo do
doc:
"Whether this relationship must always be present, e.g: must be included on creation, and never removed (it can still be changed)"
],
attribute_writable?: [
type: :boolean,
default: false,
doc: """
Whether this relationship's generated attribute will be marked as writable.
Has no effect when combined with `define_field?: false`.
"""
],
define_field?: [
type: :boolean,
default: true,

View file

@ -16,6 +16,7 @@ defmodule Ash.Resource.Relationships.HasMany do
:violation_message,
:manual,
:api,
:writable?,
no_fields?: false,
could_be_related_at_creation?: false,
validate_destination_field?: true,
@ -32,6 +33,7 @@ defmodule Ash.Resource.Relationships.HasMany do
no_fields?: boolean,
name: atom,
type: Ash.Type.t(),
writable?: boolean,
destination: Ash.Resource.t(),
destination_field: atom,
private?: boolean,

View file

@ -18,6 +18,7 @@ defmodule Ash.Resource.Relationships.HasOne do
:not_found_message,
:violation_message,
:manual,
:writable?,
no_fields?: false,
could_be_related_at_creation?: false,
validate_destination_field?: true,
@ -33,6 +34,7 @@ defmodule Ash.Resource.Relationships.HasOne do
name: atom,
read_action: atom,
no_fields?: boolean,
writable?: boolean,
type: Ash.Type.t(),
filter: Ash.Filter.t() | nil,
destination: Ash.Resource.t(),

View file

@ -19,6 +19,7 @@ defmodule Ash.Resource.Relationships.ManyToMany do
:description,
:context,
:filter,
:has_many,
could_be_related_at_creation?: false,
validate_destination_field?: true,
cardinality: :many,
@ -29,6 +30,7 @@ defmodule Ash.Resource.Relationships.ManyToMany do
type: :many_to_many,
cardinality: :many,
source: Ash.Resource.t(),
has_many: boolean,
private?: boolean,
filter: Ash.Filter.t() | nil,
read_action: atom,

View file

@ -53,6 +53,13 @@ defmodule Ash.Resource.Relationships.SharedOptions do
For example, if a value is added that has no match in the destination (very hard to do with the way Ash relationship changes work).
"""
],
writable?: [
type: :boolean,
default: true,
doc: """
Wether or not the relationship may be edited.
"""
],
read_action: [
type: :atom,
doc: """

View file

@ -29,7 +29,7 @@ defmodule Ash.Resource.Transformers.BelongsToAttribute do
else
not relationship.required?
end,
writable?: relationship.writable?,
writable?: relationship.attribute_writable?,
private?: true,
primary_key?: relationship.primary_key?
)

View file

@ -56,6 +56,24 @@ defmodule Ash.Test.Resource.Relationships.BelongsToTest do
] = Ash.Resource.Info.attributes(Post)
end
test "it creates an attribute that honors attribute_writable?" do
defposts do
relationships do
belongs_to(:foobar, FooBar, attribute_writable?: true)
end
end
assert [
_,
%Ash.Resource.Attribute{
name: :foobar_id,
primary_key?: false,
type: Ash.Type.UUID,
writable?: true
}
] = Ash.Resource.Info.attributes(Post)
end
test "it creates a relationship" do
defposts do
relationships do