read action returning a boolean

Hi, I'm trying to make a resource to represent some global boolean flags for some features in my application. I'm just having trouble figuring out how to writing an action to return a boolean.
defmodule Foo.Bar.LiveFeatureFlags do
use Ash.Resource, data_layer: AshPostgres.DataLayer

postgres do
table "live_feature_flags"
repo Repo
end

atributes do
uuid_primary_key :id

attribute :name, :string, allow_nil?: false
attribute :description, :string, allow_nil?: false
attribute :is_enabed, :boolean, allow_nil?: false
end

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

read :is_enabled? do
argument :name, :string do
allow_nil? false
end

filter expr(name == ^arg(:name))
get? true
end
end

code_interface do
define :create
define :read
define :update
define :destroy
end
end
defmodule Foo.Bar.LiveFeatureFlags do
use Ash.Resource, data_layer: AshPostgres.DataLayer

postgres do
table "live_feature_flags"
repo Repo
end

atributes do
uuid_primary_key :id

attribute :name, :string, allow_nil?: false
attribute :description, :string, allow_nil?: false
attribute :is_enabed, :boolean, allow_nil?: false
end

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

read :is_enabled? do
argument :name, :string do
allow_nil? false
end

filter expr(name == ^arg(:name))
get? true
end
end

code_interface do
define :create
define :read
define :update
define :destroy
end
end
The behaviour I'd like for is_enabled? is for it to return true if the feature of the specified name doesn't exist or return true if the feature of the specified name does exist and is_enabled is true, otherwise return false. How can I make an action return a boolean and how can I get it to have that custom logic? Cheers, Chris.
2 Replies
jart
jart2y ago
Hey Chris. You probably want split it into two actions - one read which gets the data you need and a generic action which can return an arbitrary type which calls the read action and returns the required Boolean.
Chris
ChrisOP2y ago
thanks, having a go now
defmodule Blah.Blah.LiveFeatureFlags do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
require Ash.Query

actions do
defaults([:create, :read, :destroy])
end

attributes do
uuid_primary_key(:id)
attribute(:name, :string, allow_nil?: false)
attribute(:is_enabled, :boolean, allow_nil?: false)
end

code_interface do
define_for(Blah.Blah)
define(:create)
define(:read)
define(:destroy)
end

def is_enabled?(name) do
__MODULE__
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(name == ^name)
|> Blah.Blah.read!()
|> case do
[%{is_enabled: is_enabled} | _] -> is_enabled
_ -> true
end
end
end
defmodule Blah.Blah.LiveFeatureFlags do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
require Ash.Query

actions do
defaults([:create, :read, :destroy])
end

attributes do
uuid_primary_key(:id)
attribute(:name, :string, allow_nil?: false)
attribute(:is_enabled, :boolean, allow_nil?: false)
end

code_interface do
define_for(Blah.Blah)
define(:create)
define(:read)
define(:destroy)
end

def is_enabled?(name) do
__MODULE__
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(name == ^name)
|> Blah.Blah.read!()
|> case do
[%{is_enabled: is_enabled} | _] -> is_enabled
_ -> true
end
end
end
I wasn't able to figure out how to use a generic action but this does the job

Did you find this page helpful?