2020-10-29 15:26:45 +13:00
|
|
|
defmodule AshPostgres.MultitenancyTest.Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:52:28 +13:00
|
|
|
domain: AshPostgres.MultitenancyTest.Domain,
|
2023-12-23 15:14:40 +13:00
|
|
|
data_layer: AshPostgres.DataLayer,
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
policies do
|
|
|
|
policy always() do
|
|
|
|
authorize_if(always())
|
|
|
|
end
|
|
|
|
|
|
|
|
policy action(:update_with_policy) do
|
|
|
|
# this is silly, but we want to force it to make a query
|
|
|
|
authorize_if(expr(exists(self, true)))
|
|
|
|
end
|
|
|
|
end
|
2020-10-29 15:26:45 +13:00
|
|
|
|
|
|
|
attributes do
|
2021-01-13 14:27:39 +13:00
|
|
|
uuid_primary_key(:id, writable?: true)
|
2024-03-28 09:52:28 +13:00
|
|
|
attribute(:name, :string, public?: true)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:52:28 +13:00
|
|
|
default_accept(:*)
|
|
|
|
|
2022-04-20 03:08:28 +12:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
2023-12-23 15:14:40 +13:00
|
|
|
|
|
|
|
update(:update_with_policy)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
postgres do
|
|
|
|
table "multitenant_posts"
|
|
|
|
repo AshPostgres.TestRepo
|
|
|
|
end
|
|
|
|
|
|
|
|
multitenancy do
|
2020-11-03 16:59:51 +13:00
|
|
|
# Tells the resource to use the data layer
|
|
|
|
# multitenancy, in this case separate postgres schemas
|
2021-01-13 14:22:28 +13:00
|
|
|
strategy(:context)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:52:28 +13:00
|
|
|
belongs_to(:org, AshPostgres.MultitenancyTest.Org) do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to(:user, AshPostgres.MultitenancyTest.User) do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
has_one(:self, __MODULE__, destination_attribute: :id, source_attribute: :id, public?: true)
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
2024-04-23 11:06:06 +12:00
|
|
|
|
|
|
|
calculations do
|
|
|
|
calculate(:last_word, :string, expr(fragment("split_part(?, ' ', -1)", name)))
|
|
|
|
end
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|