Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/mix/tasks/gen/ash.gen.resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if Code.ensure_loaded?(Igniter) do

## Options

* `--attribute` or `-a` - An attribute or comma separated list of attributes to add, as `name:type`. Modifiers: `primary_key`, `public`, `sensitive`, and `required`. i.e `-a name:string:required`
* `--attribute` or `-a` - An attribute or comma separated list of attributes to add, as `name:type`. Modifiers: `primary_key`, `array`, `public`, `sensitive`, and `required`. i.e `-a name:string:required`
* `--relationship` or `-r` - A relationship or comma separated list of relationships to add, as `type:name:dest`. Modifiers: `public`. `belongs_to` only modifiers: `primary_key`, `sensitive`, and `required`. i.e `-r belongs_to:author:MyApp.Accounts.Author:required`
* `--default-actions` - A csv list of default action types to add. The `create` and `update` actions accept the public attributes being added.
* `--uuid-primary-key` or `-u` - Adds a UUIDv4 primary key with that name. i.e `-u id`
Expand Down Expand Up @@ -223,6 +223,9 @@ if Code.ensure_loaded?(Igniter) do
"sensitive" ->
"sensitive? true"

"array" ->
"array"

unknown ->
raise ArgumentError,
"""
Expand Down Expand Up @@ -343,7 +346,11 @@ if Code.ensure_loaded?(Igniter) do
end

name_atom = String.to_atom(name)
type = resolve_type(type)

{type, modifiers} =
if Enum.any?(modifiers, &(&1 == "array")),
do: {{:array, resolve_type(type)}, modifiers -- ["array"]},
else: {resolve_type(type), modifiers}

attribute_code =
if Enum.empty?(modifiers) do
Expand Down
30 changes: 28 additions & 2 deletions test/mix/tasks/ash.gen.resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ defmodule Mix.Tasks.Ash.Gen.ResourceTest do
""")
end

test "generates array attributes with modifiers" do
test_project()
|> Igniter.compose_task("ash.gen.resource", [
"MyApp.Blog.Post",
"--attribute",
"list_atom:atom:array:public,list_string:string:array:required"
])
|> assert_creates("lib/my_app/blog/post.ex", """
defmodule MyApp.Blog.Post do
use Ash.Resource,
otp_app: :test,
domain: MyApp.Blog

attributes do
attribute :list_atom, {:array, :atom} do
public?(true)
end

attribute :list_string, {:array, :string} do
allow_nil?(false)
end
end
end
""")
end

test "generates attributes with custom types" do
test_project()
|> Igniter.compose_task("ash.gen.resource", [
Expand Down Expand Up @@ -751,12 +777,12 @@ defmodule Mix.Tasks.Ash.Gen.ResourceTest do

attributes do
uuid_primary_key(:id)

attribute :title, :string do
public? true
end
end

actions do
defaults [:read]
end
Expand Down
Loading