AE
Ash Elixir•7h ago
AngyL75

AshArchival

Dear team, I am currently trying to implement AshArchival. In order to be able to read the archived and none archived value. the documentation says I need to create a separate resource. Shall this separate resource have everything same as the original one, except it does not have the 'extensions: [AshArchival.Resource]' and instead have the
attributes do
attribute(:archived_at, :utc_datetime_usec, public?: true)
end
attributes do
attribute(:archived_at, :utc_datetime_usec, public?: true)
end
Should I also duplicate all the policies, calculation etc... too ? Regards, Angy.
6 Replies
AngyL75
AngyL75OP•7h ago
To add to my previous post : should ai only duplicate the attributes and that is all ?
Chaz Watkins
Chaz Watkins•7h ago
You shouldn't have to create a separate resource for the archived items.
postgres do
table "foos"
repo MyApp.Repo
base_filter_sql "(archived_at IS NULL)"
end

archive do
exclude_read_actions [:read_archived, :read_all]
base_filter? true
end

resource do
base_filter expr(is_nil(archived_at))
end

actions do
defaults [:read, :update, :create, :destroy]

read :read_all

read :read_archived do
filter expr(not is_nil(archived_at))
end

update :unarchive do
change set_attribute(:archived_at, nil)
atomic_upgrade_with :read_archived
end
end
postgres do
table "foos"
repo MyApp.Repo
base_filter_sql "(archived_at IS NULL)"
end

archive do
exclude_read_actions [:read_archived, :read_all]
base_filter? true
end

resource do
base_filter expr(is_nil(archived_at))
end

actions do
defaults [:read, :update, :create, :destroy]

read :read_all

read :read_archived do
filter expr(not is_nil(archived_at))
end

update :unarchive do
change set_attribute(:archived_at, nil)
atomic_upgrade_with :read_archived
end
end
Then set up your policies to allow some user groups to read_archived, read_all, and unarchive. The base filter means the default read will always filter out archived records The archive block specifies which actions are excluded from the base filter. I forgot why I have base_filter_sql in the postgres block and base_filter in the resource block. Did it awhile back, but seems to work ok. But, it all depends on how you want to model your domain. If you want to have a Foo resource and an ArchivedFoo resource pointing to the same table with different base filters, that works too Sometimes it is helpful to break them apart, but for my use case I didn't
AngyL75
AngyL75OP•7h ago
How, thanks. I will try to implement it 🙂 and will let you know my progress 🙂
ZachDaniel
ZachDaniel•7h ago
Are you sure that works? I didn't think the base filter was compatible with the exclude read actions option I think you have to do it without the base filter?
Chaz Watkins
Chaz Watkins•6h ago
I’d have to ask Rebecca since she fought with the archival stuff for a while on my app. Pretty sure your only need to do the archival block base filter. I’ll check with her when she some online and post the answer here.
ZachDaniel
ZachDaniel•6h ago
I don't know of a way to bypass the base filter on a resource is all 😂

Did you find this page helpful?