SelectoComponents.FilterSetsBehaviour behaviour (selecto_components v0.4.5)
Behavior for implementing persistent filter sets in SelectoComponents.
This behavior defines a contract for saving, loading, and managing filter sets to persistent storage. It enables users to save complex filter combinations, share them with others, and quickly apply commonly used filters.
overview
Overview
The FilterSets behavior supports:
- Personal filter sets: User-specific saved filters
- Shared filter sets: Filters shared across users in a domain
- System filter sets: Admin-configured default filters
- Default filter sets: User's preferred default filter per domain
- Usage tracking: Analytics on filter set usage
database-schema
Database Schema
The typical database structure includes:
id: UUID primary keyname: String identifier for the filter setdescription: Optional descriptiondomain: String domain/context identifierfilters: Map/JSON containing the filter configurationuser_id: Owner of the filter setis_default: Boolean flag for user's defaultis_shared: Boolean flag for shared setsis_system: Boolean flag for system setsusage_count: Integer tracking usagetimestamps: Created/updated timestamps
integration-with-selectocomponents
Integration with SelectoComponents
SelectoComponents automatically integrates with filter sets when:
- A module implementing this behavior is configured
- The
filter_sets_adapteris set in the component assigns - Users can then save/load filter sets through the UI
example-implementation
Example Implementation
defmodule MyApp.FilterSets do
@behaviour SelectoComponents.FilterSetsBehaviour
def list_personal_filter_sets(user_id, domain) do
# Return user's personal filter sets for domain
end
def list_shared_filter_sets(user_id, domain) do
# Return shared filter sets for domain
end
def list_system_filter_sets(domain) do
# Return system filter sets for domain
end
# ... other callbacks
end
generator-support
Generator Support
Use the Mix generator to create the implementation:
mix selecto.gen.filter_sets MyAppThis generates:
- Migration for
filter_setstable - Ecto schema for filter sets
- Context module implementing this behavior
Link to this section Summary
Callbacks
Creates a new filter set.
Deletes a filter set.
Duplicates a filter set with a new name.
Gets the user's default filter set for a domain.
Gets a specific filter set by ID.
Increments the usage count for a filter set.
Lists personal filter sets for a user and domain.
Lists shared filter sets accessible to a user in a domain.
Lists system-provided filter sets for a domain.
Sets a filter set as the user's default for a domain.
Updates an existing filter set.
Link to this section Callbacks
create_filter_set(attrs)
Creates a new filter set.
parameters
Parameters
attrs: Map containing filter set attributes:name- Required string name:description- Optional description:domain- Required domain identifier:filters- Required map of filter configuration:user_id- Required owner ID:is_default- Optional boolean (default: false):is_shared- Optional boolean (default: false)
returns
Returns
{:ok, filter_set}on success{:error, changeset}on validation failure
delete_filter_set(id, user_id)
@callback delete_filter_set(id :: String.t(), user_id :: String.t()) :: {:ok, map()} | {:error, atom()}
Deletes a filter set.
parameters
Parameters
id: Filter set IDuser_id: User performing the deletion (for access control)
returns
Returns
{:ok, filter_set}on success{:error, :not_found}if not found{:error, :unauthorized}if user lacks permission
duplicate_filter_set(id, new_name, user_id)
@callback duplicate_filter_set( id :: String.t(), new_name :: String.t(), user_id :: String.t() ) :: {:ok, map()} | {:error, atom()}
Duplicates a filter set with a new name.
parameters
Parameters
id: Source filter set IDnew_name: Name for the duplicateuser_id: User creating the duplicate
returns
Returns
{:ok, filter_set}on success{:error, :not_found}if source not found{:error, :unauthorized}if user lacks access to source
get_default_filter_set(user_id, domain)
Gets the user's default filter set for a domain.
parameters
Parameters
user_id: User identifierdomain: Domain/context identifier
returns
Returns
- The default filter set if one exists
nilif no default is set
get_filter_set(id, user_id)
@callback get_filter_set(id :: String.t(), user_id :: String.t()) :: {:ok, map()} | {:error, atom()}
Gets a specific filter set by ID.
parameters
Parameters
id: Filter set IDuser_id: User requesting the filter set (for access control)
returns
Returns
{:ok, filter_set}if found and accessible{:error, :not_found}if not found{:error, :unauthorized}if user lacks access
increment_usage_count(id)
@callback increment_usage_count(id :: String.t()) :: :ok
Increments the usage count for a filter set.
parameters
Parameters
id: Filter set ID
returns
Returns
:okon success
list_personal_filter_sets(user_id, domain)
Lists personal filter sets for a user and domain.
parameters
Parameters
user_id: User identifierdomain: Domain/context identifier
returns
Returns
List of filter set records owned by the user for the domain.
list_system_filter_sets(domain)
Lists system-provided filter sets for a domain.
parameters
Parameters
domain: Domain/context identifier
returns
Returns
List of system filter set records for the domain.
set_default_filter_set(id, user_id)
@callback set_default_filter_set(id :: String.t(), user_id :: String.t()) :: {:ok, map()} | {:error, atom()}
Sets a filter set as the user's default for a domain.
parameters
Parameters
id: Filter set ID to make defaultuser_id: User setting the default
returns
Returns
{:ok, filter_set}on success{:error, :not_found}if not found{:error, :unauthorized}if user lacks permission
update_filter_set(id, attrs, user_id)
@callback update_filter_set(id :: String.t(), attrs :: map(), user_id :: String.t()) :: {:ok, map()} | {:error, atom() | any()}
Updates an existing filter set.
parameters
Parameters
id: Filter set IDattrs: Map of attributes to updateuser_id: User performing the update (for access control)
returns
Returns
{:ok, filter_set}on success{:error, :not_found}if not found{:error, :unauthorized}if user lacks permission{:error, changeset}on validation failure