Created
May 20, 2024 16:08
-
-
Save emtudo/e8040203e1070e92476dbd899c40a88d to your computer and use it in GitHub Desktop.
unarchive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.Tenant.Organization do | |
# Using Ash.Resource turns this module into an Ash resource. | |
use Ash.Resource, | |
# Tells Ash where the generated code interface belongs | |
domain: App.Tenant, | |
# Tells Ash you want this resource to store its data in Postgres. | |
data_layer: AshPostgres.DataLayer, | |
extensions: [AshArchival.Resource] | |
archive do | |
exclude_read_actions [:archived, :archived_by_id] | |
end | |
# resource do | |
# base_filter expr(is_nil(archived_at)) // se colocar isso não vai ser possível listar os arquivados | |
# end | |
# The Postgres keyword is specific to the AshPostgres module. | |
postgres do | |
# Tells Postgres what to call the table | |
table "organizations" | |
# Tells Ash how to interface with the Postgres table | |
repo App.Repo | |
base_filter_sql "(archived_at IS NULL)" | |
end | |
actions do | |
# Exposes default built in actions to manage the resource | |
defaults [:read, :destroy] | |
create :create do | |
# accept name and domain as input | |
accept [:name, :domain] | |
change fn changeset, _context -> | |
name = Ash.Changeset.get_attribute(changeset, :name) | |
changeset | |
|> Ash.Changeset.change_attribute(:slug, name) | |
end | |
change {App.Changes.Slugify, attribute: :slug} | |
end | |
update :update do | |
# accept name and domain as input | |
accept [:name, :domain] | |
end | |
update :unarchive do | |
accept [:name, :domain] | |
change set_attribute(:archived_at, nil) | |
end | |
# Defines custom read action which fetches post by id. | |
read :by_id do | |
# This action has one argument :id of type :uuid | |
argument :id, :uuid, allow_nil?: false | |
# Tells us we expect this action to return a single result | |
get? true | |
# Filters the `:id` given in the argument | |
# against the `id` of each element in the resource | |
filter expr(id == ^arg(:id)) | |
end | |
read :archived do | |
filter expr(not is_nil(archived_at)) | |
end | |
# Defines custom read action which fetches post by id. | |
read :archived_by_id do | |
# This action has one argument :id of type :uuid | |
argument :id, :uuid, allow_nil?: false | |
# Tells us we expect this action to return a single result | |
get? true | |
# Filters the `:id` given in the argument | |
# against the `id` of each element in the resource | |
filter expr(id == ^arg(:id)) | |
end | |
end | |
# Attributes are simple pieces of data that exist in your resource | |
attributes do | |
# Add an autogenerated UUID primary key called `:id`. | |
uuid_primary_key :id | |
# Add a string type attribute called `:name` | |
attribute :name, :string do | |
# We don't want the name to ever be `nil` | |
allow_nil? false | |
constraints max_length: 100 | |
end | |
# Add a string type attribute called `:slug` | |
# If allow_nil? is not specified, then slug can be nil | |
attribute :slug, :string do | |
constraints max_length: 100 | |
end | |
attribute :domain, :string do | |
constraints max_length: 100 | |
allow_nil? false | |
end | |
attribute :inserted_at, :utc_datetime_usec do | |
writable? false | |
default &DateTime.utc_now/0 | |
match_other_defaults? true | |
allow_nil? false | |
end | |
attribute :updated_at, :utc_datetime_usec do | |
writable? false | |
default &DateTime.utc_now/0 | |
update_default &DateTime.utc_now/0 | |
match_other_defaults? true | |
allow_nil? false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment