AshScenario.Scenario.Helpers (ash_scenario v0.6.0)

View Source

Shared helper functions for resource creation, used by both Runner and StructBuilder.

Summary

Functions

Build a changeset for creating a resource with Ash.

Execute a custom creation function.

Find a referenced resource in the created resources map.

Get a create action for a resource, with fallback to any create action.

Infer the domain for a resource module.

Normalize prototype refs and merge overrides from multiple sources.

Get the related module for a relationship attribute.

Check if an attribute name corresponds to a relationship.

Resolve a single attribute value, handling relationship references.

Resolve all attributes, handling relationship references.

Track telemetry for a created resource.

Functions

build_changeset(resource_module, action_name, attributes, opts)

Build a changeset for creating a resource with Ash.

Returns {:ok, changeset, tenant_value} where tenant_value may be nil if the resource doesn't use attribute-based multitenancy.

execute_custom_function(fun, resolved_attributes, opts)

Execute a custom creation function.

find_referenced_resource(resource_name, related_module, created_resources)

Find a referenced resource in the created resources map.

get_create_action(resource_module, preferred_action)

Get a create action for a resource, with fallback to any create action.

infer_domain(resource_module)

Infer the domain for a resource module.

normalize_refs_and_overrides(prototype_refs, opts)

Normalize prototype refs and merge overrides from multiple sources.

Supports:

  • Per-ref tuple: {Module, :ref, %{attr => value}}
  • Top-level opts: overrides: %{{Module, :ref} => %{...}}
  • For a single prototype, a bare map is allowed: overrides: %{...}

relationship_attribute?(resource_module, attr_name)

Check if an attribute name corresponds to a relationship.

resolve_attribute_value(value, attr_name, resource_module, created_resources)

Resolve a single attribute value, handling relationship references.

For database mode: Resolves atom references to IDs from created resources. For struct mode: Keep atom references as-is (handled by caller).

resolve_attributes(attributes, resource_module, created_resources)

Resolve all attributes, handling relationship references.

track_created_resource(created_resource, prototype)

Track telemetry for a created resource.