Ash FrameworkAF
Ash Framework3y ago
3 replies
Chris

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


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.
Was this page helpful?