Ash Framework: some comments

Hi Rebecca. Thanks for your quick reply.

I cloned the starter app.
But I think it doesn’t include the blank project?
I wanted to start from scratch and code along with your book.
That way I get a better feeling for Ash (I think : :slight_smile: :innocent:

Thanks for your great work, Heiko

Hi Rebecca,

A little suggestion concerning the Searching from the catalog on page 66ff.
I expected not to be forced to press the return key to confirm a search.
Instead I really like to be able to type and get the result, when I stop typing.

I added phx-debounce=“500”, phx-change=“search”, autocomplete=“off” to the the <input /> and like it a lot :slight_smile:

Kind regards from Heiko

Another small suggestion concerning the Searching from the catalog on page 66ff.
I think the captions Prev and Next are missing

<.button_link
data-role=“previous-page”
patch={~p"/?#{query_string(@page, @query_text, @sort_by, “prev”)}“}
disabled={!AshPhoenix.LiveView.prev_page?(@page)}
class=“join-item”
kind=“primary”
outline
>
Prev
</.button_link>
<.button_link
data-role=“next-page”
patch={~p”/?#{query_string(@page, @query_text, @sort_by, “next”)}"}
disabled={!AshPhoenix.LiveView.next_page?(@page)}
class=“join-item”
kind=“primary”
outline
>
Next
</.button_link>

Hi there!

I don’t think the labels are missing - are we looking at the same part of the book?

Dear Jessica.

That is really embarrassing,
I didn’t see the << Previous and Next >>
I don’t know why and I am really sorry.
Next time I will look at least ten times….

Viele Grüße von Heiko Goes

Hi Rebecca.

I hope, I am not stupid this time :innocent:

On Chapter 6 page 142 Assigning Roles to Users is this code example:

attributes do
# ...
attribute :role, :atom do
    allow_nil? false
    default :user
    constraints [one_of: [:admin, :editor, :user]]
end
end

when inserting constraints [one_of: [:admin, :editor, :user]] into my code, I get an compile error:

== Compilation error in file lib/tunez/accounts/user.ex ==
** (Spark.Error.DslError) [Tunez.Accounts.User]
attributes -> attribute -> role:
  unknown options [:one_of], valid options are: []
    (ash 3.4.62) /Volumes/Quizzlab/Projects/tunez/deps/spark/lib/spark/dsl/extension.ex:1187: Ash.Resource.Dsl.Attributes.Attribute.__build__/3
    (spark 2.2.45) lib/spark/dsl/extension/entity.ex:91: Spark.Dsl.Extension.Entity.handle/6
    lib/tunez/accounts/user.ex:284: (module)

I looked at your GitHub-repo, but the constraints-code is missing there.
I looked into the Hex-Docs:

action :priority, :integer do
  constraints [min: 1, max: 3]
  argument :status, :atom, constraints: [one_of: [:high, :medium, :low]]

and thought: okay that’s the typo, tried:

constraints: [one_of: [:admin, :editor, :user]]

and got:

== Compilation error in file lib/tunez/accounts/user.ex ==
** (SyntaxError) invalid syntax found on lib/tunez/accounts/user.ex:288:54:
     error: unexpectedly reached end of line. The current expression is invalid or incomplete
     │
 288 │       constraints: [one_of: [:admin, :editor, :user]]
     │                                                      ^
     │
     └─ lib/tunez/accounts/user.ex:288:54
    (elixir 1.18.1) lib/kernel/parallel_compiler.ex:428: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/8

Can you help me?

Just as I posted this message, I think, I found the solution.
The code:

attribute :role, Tunez.Accounts.Role do
  allow_nil? false
  default :user
end

does

constraints [one_of: [:admin, :editor, :user]]

automatically, because of the attribute:

attribute :role, Tunez.Accounts.Role

right?
Maybe this could be clarified.

Best wishes to you from Heiko

Correct, only the :atom type supports the one_of constraint, and when using enums it isn’t necessary :ok_hand:

1 Like