AshBackpex.LiveResource.Transformers.GenerateBackpex (Ash Backpex v0.3.4)

View Source

Spark DSL transformer that generates Backpex LiveResource code at compile time.

This transformer is the core of AshBackpex - it reads your backpex DSL configuration and generates all the necessary code to create a fully functional Backpex LiveResource backed by your Ash resource.

What Gets Generated

When you use AshBackpex.LiveResource with a backpex block, this transformer generates:

Backpex Configuration

  • use Backpex.LiveResource with the AshBackpex.Adapter
  • Adapter configuration with resource, repo, and action settings
  • Layout, pagination, sorting, and other Backpex options

Callback Implementations

  • fields/0 - Returns the field configurations with auto-derived modules
  • filters/0 - Returns the filter configurations
  • item_actions/1 - Returns item actions (with default stripping support)
  • singular_name/0 - Returns the singular display name
  • plural_name/0 - Returns the plural display name
  • panels/0 - Returns panel configurations

Authorization

  • can?/3 - Checks Ash authorization for each action type:
    • :new checks create action authorization
    • :index / :show check read action authorization
    • :edit checks update action authorization
    • :delete checks destroy action authorization
    • Custom actions check for matching Ash actions

Helper Functions

  • load/3 - Returns configured loads for the adapter
  • maybe_default_options/1 - Derives select options from one_of constraints

Field Type Derivation

The transformer automatically maps Ash types to Backpex field modules:

Ash TypeBackpex Field
Ash.Type.StringBackpex.Fields.Text
Ash.Type.AtomBackpex.Fields.Text (or Select with one_of)
Ash.Type.BooleanBackpex.Fields.Boolean
Ash.Type.IntegerBackpex.Fields.Number
Ash.Type.FloatBackpex.Fields.Number
Ash.Type.DateBackpex.Fields.Date
Ash.Type.TimeBackpex.Fields.Time
Ash.Type.DateTimeBackpex.Fields.DateTime
:belongs_toBackpex.Fields.BelongsTo
:has_manyBackpex.Fields.HasMany
:many_to_manyBackpex.Fields.HasMany
{:array, EmbeddedResource} with InlineCRUDAshBackpex.Fields.InlineCRUD (type: :embed)
singular EmbeddedResourceAshBackpex.Fields.Embedded (explicit)
{:array, _}Backpex.Fields.MultiSelect
Aggregates (:count, :sum, etc.)Backpex.Fields.Number or Boolean

Constraint Handling

  • Attributes with one_of constraints automatically use Backpex.Fields.Select
  • Array attributes with one_of constraints use Backpex.Fields.MultiSelect
  • Options are auto-derived from constraint values with title-cased labels
  • Relationship filters and sorts are auto-derived into Backpex options_query

Error Handling

The transformer raises helpful errors when:

  • A field doesn't exist on the Ash resource
  • A field type can't be derived (suggests using module option)
  • Recursive child_fields do not resolve to an immediate child Ash resource
  • InlineCRUD is used with a cardinality other than has_many or an embedded-resource array
  • The resource lacks a primary key
  • The LiveResource defines can?/3, fields/0, filters/0, item_actions/1, or layout/1 itself. The generated callback would silently replace it, so the error points to the Ash policy or DSL option to use instead.

Internal Use

This module is invoked automatically by Spark during compilation. You don't need to call it directly - just define your backpex block and the transformer handles the rest.

Summary

Functions

after?(_)

Callback implementation for Spark.Dsl.Transformer.after?/1.

after_compile?()

Callback implementation for Spark.Dsl.Transformer.after_compile?/0.

before?(_)

Callback implementation for Spark.Dsl.Transformer.before?/1.

transform(dsl_state)

Callback implementation for Spark.Dsl.Transformer.transform/1.