AshBackpex.Filters.Range (Ash Backpex v0.1.2)

View Source

A Range filter for AshBackpex that renders min/max input fields for numeric filtering.

This module provides both the UI rendering (via Backpex.Filters.Range) and the Ash.Expr generation (via AshBackpex.Filters.Filter behavior) for filtering numeric attributes with greater-than-or-equal and less-than-or-equal comparisons.

Usage

The Range filter is automatically derived for :integer, :float, and :decimal type attributes when using AshBackpex.LiveResource. You can also use it directly:

defmodule MyApp.Filters.PriceRange do
  use AshBackpex.Filters.Range

  @impl Backpex.Filter
  def label, do: "Price"

  @impl Backpex.Filters.Range
  def type, do: :number
end

Filter Values

The filter receives values as a map with "start" and "end" keys:

  • %{"start" => "10", "end" => "100"} - Filter for field >= 10 AND field <= 100
  • %{"start" => "10", "end" => ""} - Filter for field >= 10
  • %{"start" => "", "end" => "100"} - Filter for field <= 100
  • %{"start" => "", "end" => ""} - No filter applied
  • nil or %{} - No filter applied

Types

The Range filter supports three types via the type/0 callback:

  • :number - For integer and float attributes (default)
  • :date - For date attributes
  • :datetime - For datetime attributes

Note: Date and datetime filtering uses the same Ash expressions but with date/datetime values instead of numbers.

Options

You must implement the type/0 callback to specify the input type:

@impl Backpex.Filters.Range
def type, do: :number

Note: Unlike Backpex's Ecto-based filtering which uses the query/3 callback, AshBackpex uses to_ash_expr/3 to generate Ash expressions for filtering.

Summary

Functions

Converts a range filter value to an Ash.Expr expression.

Functions

to_ash_expr(field, value, assigns)

Converts a range filter value to an Ash.Expr expression.

Parameters

  • field - The atom name of the attribute being filtered
  • value - The filter value (map with "start" and "end" keys)
  • assigns - The LiveView assigns (unused by this filter)

Returns

  • Ash.Expr.t() - When at least one of start or end is a valid value
  • nil - When no filter should be applied (both empty, nil, or invalid)

Examples

iex> Range.to_ash_expr(:price, %{"start" => "10", "end" => "100"}, %{})
#Ash.Expr<price >= 10 and price <= 100>

iex> Range.to_ash_expr(:price, %{"start" => "10", "end" => ""}, %{})
#Ash.Expr<price >= 10>

iex> Range.to_ash_expr(:price, %{"start" => "", "end" => "100"}, %{})
#Ash.Expr<price <= 100>

iex> Range.to_ash_expr(:price, %{"start" => "", "end" => ""}, %{})
nil

iex> Range.to_ash_expr(:price, nil, %{})
nil