fix: set tenant in bulk creates after setting up changeset

This commit is contained in:
Zach Daniel 2024-04-04 20:23:08 -04:00
parent 0f8c8152c7
commit 950ff3d060
4 changed files with 31 additions and 5 deletions

View file

@ -1,13 +1,15 @@
# What is Ash? # What is Ash?
At a high level, Ash is an opinionated, foundational set of application building blocks, designed for maximum compatibility, reuse and extensibility. It can be used for any kind of Elixir application, but it shines when building web applications, APIs and services. It is not a web framework, like Phoenix or Rails. It is a framework for building your application layer, independent of how it is exposed or consumed. It is not an **alternative** to frameworks like Phoenix, rather a **complement** to them. Ash is an opinionated, composable set of application building blocks designed for extensibility. It shines when building web apps, APIs and services (though it can be used for any kind of Elixir application). It has integrations for Phoenix, LiveView, Postgres, GraphQL, Oban, and many more.
More concretely, Ash provides a "Resource" abstraction, with which you model your application. These resources determine the database schema, API endpoints, state machines, background jobs, and more. They are the source of truth for your entire application, and everything stems from them. To achieve this Ash provides a "Resource" abstraction you use to model the heart of your application. These resources then determine the database schema, API endpoints, state machines, background jobs, and more. Resources are the source of truth for your entire application, and everything stems from them.
Ash is a declarative framework, with deep extensibility. We provide a suite of extensions, as well as a toolkit to build your own. When you need to break out of our design patterns, we provide escape hatches ranging from the simple and small in scope to abilities to override large pieces of behavior. Additionally, your Ash application is just an Elixir application, so if you want to do something completley custom outside of Ash, it won't get in your way. Ash is a declarative framework with deep extensibility. We provide a suite of extensions informed from building production apps, as well as a toolkit so you can build your own. We provide escape hatches ranging from the simple and small to the ability to override large pieces of behavior. Your Ash application is just an Elixir application, so if you want to do something completely custom then Ash wont get in your way.
It is not a web framework, like Phoenix or Rails. It is a framework for building your application layer, independent of how it is exposed or consumed. It is not an **alternative** to frameworks like Phoenix, rather a **complement** to them.
> #### Model your domain, derive the rest {: .info} > #### Model your domain, derive the rest {: .info}
> With the tools provided by Ash you can get entire application layers, derived and configured directly by your resources, practically for free. > With the tools provided by Ash you can get large swaths of your application , derived and configured directly by your resources, practically for free.
> Ash leverages the best of the Elixir ecosystem under the hood, but provides a single unified tool-chain for our users. > Ash leverages the best of the Elixir ecosystem under the hood, but provides a single unified tool-chain for our users.
--- ---

View file

@ -283,7 +283,6 @@ defmodule Ash.Actions.Create.Bulk do
}) })
|> Ash.Actions.Helpers.add_context(opts) |> Ash.Actions.Helpers.add_context(opts)
|> Ash.Changeset.set_context(opts[:context] || %{}) |> Ash.Changeset.set_context(opts[:context] || %{})
|> set_tenant()
|> Ash.Changeset.prepare_changeset_for_action(action, opts) |> Ash.Changeset.prepare_changeset_for_action(action, opts)
end end
@ -506,6 +505,7 @@ defmodule Ash.Actions.Create.Bulk do
) )
|> set_lazy_non_matching_defaults() |> set_lazy_non_matching_defaults()
|> set_lazy_matching_defaults(lazy_matching_default_values) |> set_lazy_matching_defaults(lazy_matching_default_values)
|> set_tenant()
end end
defp set_tenant(changeset) do defp set_tenant(changeset) do

View file

@ -153,6 +153,7 @@ defmodule Ash.Test.Actions.BulkCreateTest do
belongs_to :org, Org do belongs_to :org, Org do
public?(true) public?(true)
allow_nil? false allow_nil? false
attribute_public? false
end end
end end
end end

View file

@ -106,6 +106,29 @@ defmodule Ash.Test.Actions.ValidationTest do
end) end)
end end
describe "custom validations" do
test "they can return exceptions" do
assert {:error, _} =
Profile
|> Ash.Changeset.for_create(:create, %{status: "baz"})
|> Ash.create()
end
test "they can return exceptions inside of embedded resources" do
assert {:error, _} =
Profile
|> Ash.Changeset.for_create(:create, %{embedded: %{name: "thing"}})
|> Ash.create()
end
test "they can return exceptions inside of embedded lists" do
assert {:error, _error} =
Profile
|> Ash.Changeset.for_create(:create, %{embedded_list: [%{name: "thing"}]})
|> Ash.create()
end
end
describe "one_of" do describe "one_of" do
test "it succeeds if the value is in the list" do test "it succeeds if the value is in the list" do
Profile Profile