View Source Pyro.Resource.Info (Pyro v0.0.2)
Helpers to introspect the Pyro.Resource
Ash extension. Intended for use in components that automatically build UI from resource configuration.
Link to this section Summary
Functions
The label of the action as defined in the Pyro.Resource
extension, defaulting to a humanized version of the action name.
Convert a list of actions into a format compatible with Phoenix.HTML.Form.select/4
options
.
The same as primary_actions/2
, but filtered by those authorized for access for the specified action/actor.
Returns the list of :create
type actions of the given resource.
The same as create_actions/1
, but filtered by those authorized for access for the specified action/actor.
The default card fields to display as defined in the Pyro.Resource
extension, with fallback to all public fields.
Same as default_card_fields/1
, but filtered by those authorized for access for the specified action/actor.
The default display mode of the resource as defined in the Pyro.Resource
extension, defaulting to :data_table
.
Returns the field defined in the Pyro.Resource
extension as a default label, falling back to the first single-key identity of the resource, further falling back to the primary key if it's single-keyed.
Return the default page for the given resource/action.
The first single-keyed identity, or the primary ID of the given resource.
The default sort of resource for the given action, falling back to the default provided by the Pyro.Resource
extension.
The default table columns to display as defined in the Pyro.Resource
extension, with fallback to all public fields.
Same as default_table_columns/1
, but filtered by those authorized for access for the specified action/actor.
Returns the list of :destroy
type actions of the given resource.
The same as destroy_actions/1
, but filtered by those authorized for access for the specified action/actor.
Check if a given field is authorized to be viewed by the actor.
Returns the given field's description.
WIP label for resource fields. In the future it will pull from a resource field property. This will allow different labels based on context.
Returns the form fields defined in the Pyro.Resource
extension for the given action.
This is only used for the primary user input. This provides consistency between the input component and other parts of the UI that may want ot provide a link to that user input e.g. to click an error and focus the related input.
Return a boolean indicating if resource is a page as configured in the Pyro.Resource
extension.
Returns the list of :read
type actions of the given resource, excluding actions that are get?: true
.
The same as list_actions/1
, but filtered by those authorized for access for the specified action/actor.
Filter the list of fields to those which can be loaded (as opposed to those which can be selected). Basically just filters out attributes from a list of fields.
Return the page limit options for the given resource/action if pagination is configured.
The custom page module of the action as defined in the Pyro.Resource
extension, if the resource is configured as a custom page.
The page routing path of the resource as defined in the Pyro.Resource
extension, if the resource is configured as a page.
Returns the list of actions of the given resource.
Lists all the public fields of resource.
The same as public_fields/1
, but filtered by those authorized for access for the specified action/actor.
Returns the list of :read
type actions of the given resource.
The same as read_actions/1
, but filtered by those authorized for access for the specified action/actor.
The label of the resource as defined in the Pyro.Resource
extension, defaulting to a humanized version of the module name.
Returns the list of :read
type actions intended for single-record reads of the given resource.
The same as show_actions/1
, but filtered by those authorized for access for the specified action/actor.
List the single-keyed identities of the given resource.
Check if a field is sortable.
Returns the list of :update
type actions of the given resource.
The same as update_actions/1
, but filtered by those authorized for access for the specified action/actor.
Link to this section Types
@type ash_action_or_name() :: Ash.Resource.Actions.action() | binary() | atom()
@type ash_actor() :: map() | nil
@type ash_resource_field() :: Ash.Resource.Attribute.t() | Ash.Resource.Aggregate.t() | Ash.Resource.Calculation.t() | Ash.Resource.Relationships.relationship()
@type ash_resource_field_or_name() :: ash_resource_field() | binary() | atom()
@type primary_actions_opt() :: [ {:exclude_types, [Ash.Resource.Actions.action_type()]} ]
Link to this section Functions
@spec action_label(Ash.Resource.t(), ash_action_or_name()) :: binary() | nil
The label of the action as defined in the Pyro.Resource
extension, defaulting to a humanized version of the action name.
examples
Examples
iex> action_label(Gage, "calibration_alerts")
"Calibration Alerts"
iex> action_label(Gage, :update)
"Update"
iex> action_label(Gage, %{name: :read})
"Read"
@spec actions_to_select_options([Ash.Resource.Actions.action()], Ash.Resource.t()) :: [select_option()]
Convert a list of actions into a format compatible with Phoenix.HTML.Form.select/4
options
.
examples
Examples
iex> actions_to_select_options(read_actions(Gage), Gage)
[
[key: "Calibration Alerts", value: "calibration_alerts"],
[key: "Read", value: "read"]
]
@spec authorized_primary_actions(Ash.Resource.t(), ash_actor(), [ Ash.Policy.Info.can_option() | primary_actions_opt() ]) :: [Ash.Resource.Actions.action()]
The same as primary_actions/2
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = authorized_primary_actions(User, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec create_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Create.t()]
Returns the list of :create
type actions of the given resource.
examples
Examples
iex> r = create_actions(Gage)
iex> r |> Enum.map(& &1.name)
[:create]
@spec create_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Create.t() ]
The same as create_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = create_actions(Gage, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec default_action_for_live_action( Ash.Resource.t(), Pyro.ResourceParams.live_action_type(), map() | nil ) :: Ash.Resource.Actions.action()
@spec default_card_fields(Ash.Resource.t()) :: [ash_resource_field()]
The default card fields to display as defined in the Pyro.Resource
extension, with fallback to all public fields.
examples
Examples
Explicitly configured
iex> r = default_card_fields(AttendanceRecord)
iex> r |> Enum.map(& &1.name)
[
:inserted_at,
:updated_at,
:in,
:out,
:duration_hours,
:type_label,
:employee_code,
:employee_name_formal,
:notes
]
@spec default_card_fields(Ash.Resource.t(), Ash.Resource.Action.t(), ash_actor()) :: [ ash_resource_field() ]
Same as default_card_fields/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = default_card_fields(Gage, :calibration_alerts, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec default_display_mode(Ash.Resource.t()) :: binary()
The default display mode of the resource as defined in the Pyro.Resource
extension, defaulting to :data_table
.
examples
Examples
iex> default_display_mode(User)
:card_grid
@spec default_foreign_label(Ash.Resource.t()) :: atom()
Returns the field defined in the Pyro.Resource
extension as a default label, falling back to the first single-key identity of the resource, further falling back to the primary key if it's single-keyed.
examples
Examples
iex> default_foreign_label(AttendanceType)
:label
iex> default_foreign_label(Gage)
:name
@spec default_page_limit(Ash.Resource.t(), atom() | binary()) :: pos_integer()
Return the default page for the given resource/action.
examples
Examples
iex> default_page_limit(AttendanceType, :read)
25
@spec default_single_field_identity_key(Ash.Resource.t()) :: :atom
The first single-keyed identity, or the primary ID of the given resource.
examples
Examples
iex> default_single_field_identity_key(User) :email
@spec default_sort(Ash.Resource.t(), atom()) :: binary()
The default sort of resource for the given action, falling back to the default provided by the Pyro.Resource
extension.
This is useful for extracting default sorts from preparations.
examples
Examples
iex> default_sort(Gage, :calibration_alerts)
"++calibration_status_expiration,calibration_status,name"
iex> default_sort(AttendanceRecord, :read)
"-in"
iex> default_sort(AttendanceRecord, :current_attendance)
"in"
@spec default_table_columns(Ash.Resource.t()) :: [ash_resource_field()]
The default table columns to display as defined in the Pyro.Resource
extension, with fallback to all public fields.
examples
Examples
Explicitly configured
iex> r = default_table_columns(User)
iex> r |> Enum.map(& &1.name)
[:name, :email, :notes]
@spec default_table_columns(Ash.Resource.t(), Ash.Resource.Action.t(), ash_actor()) :: [ ash_resource_field() ]
Same as default_table_columns/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = default_table_columns(Gage, :calibration_alerts, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec destroy_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Destroy.t()]
Returns the list of :destroy
type actions of the given resource.
examples
Examples
iex> r = destroy_actions(Gage)
iex> r |> Enum.map(& &1.name)
[:destroy]
@spec destroy_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Destroy.t() ]
The same as destroy_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = destroy_actions(Gage, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec field_authorized?( ash_resource_field_or_name(), Ash.Resource.t(), ash_action_or_name(), ash_actor(), keyword() ) :: boolean()
Check if a given field is authorized to be viewed by the actor.
examples
Examples
iex> field_authorized?("name", Gage, "read", nil)
false
iex> field_authorized?(:owner, Gage, :read, %{roles: [:Employee], active: true})
true
Note:
Currently, fields that may be authorized depending on actor's attributes are authorized. We may eventually want to more intelligently deal with this, because currently a
nil
actor will return true. This particular case will handled when #372 is closed. This is mostly UI concern, because the query will still be safely validated and would fail, but we don't want to clutter the UI with fields that can't actually be loaded.
iex> field_authorized?(:owner, Gage, :read, nil)
false
Note:
We are not currently handling field-level authz for create/update types. This can probably be done by leveraging
Ash.Generator
to seed changeset data for the given field. This should allow determine e.g. if a relationship is editable by the actor, allow forms to hide relationships that an actor can't edit.
@spec field_description( Ash.Resource.t(), ash_resource_field_or_name() ) :: atom()
Returns the given field's description.
examples
Examples
iex> field_description(AttendanceType, :clockable)
"Clockable types are available for employees to punch in/out at kiosks and workstations."
WIP label for resource fields. In the future it will pull from a resource field property. This will allow different labels based on context.
@spec form_for(Ash.Resource.t(), atom()) :: [ Pyro.Resource.Form.Field.t() | Pyro.Resource.Form.FieldGroup.t() ]
Returns the form fields defined in the Pyro.Resource
extension for the given action.
examples
Examples
iex> form_for(AttendanceRecord, :create) |> Enum.map(& &1.name)
[:notes, :employee_id, :type_id]
@spec input_id(AshPhoenix.Form.t(), atom()) :: binary()
This is only used for the primary user input. This provides consistency between the input component and other parts of the UI that may want ot provide a link to that user input e.g. to click an error and focus the related input.
examples
Examples
iex> form = AshPhoenix.Form.for_create(AttendanceType, :create)
iex> input_id(form, :label)
"form_label"
@spec is_page?(Ash.Resource.t()) :: boolean()
Return a boolean indicating if resource is a page as configured in the Pyro.Resource
extension.
examples
Examples
iex> is_page?(AttendanceType)
true
@spec list_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Read.t()]
Returns the list of :read
type actions of the given resource, excluding actions that are get?: true
.
examples
Examples
iex> r = list_actions(Gage)
iex> r |> Enum.map(& &1.name)
[:calibration_alerts, :read]
@spec list_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Read.t() ]
The same as list_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = list_actions(Gage, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec loadable_fields([ash_resource_field()], Ash.Resource.t()) :: [atom()]
Filter the list of fields to those which can be loaded (as opposed to those which can be selected). Basically just filters out attributes from a list of fields.
examples
Examples
Explicitly configured
iex> default_table_columns(User) |> loadable_fields(User)
[:best_friend]
@spec page_limit_options(Ash.Resource.t(), atom() | binary()) :: [pos_integer()]
Return the page limit options for the given resource/action if pagination is configured.
examples
Examples
iex> page_limit_options(AttendanceType, :read)
[10, 25, 50, 100, 250]
@spec page_module(Ash.Resource.t()) :: module() | nil
The custom page module of the action as defined in the Pyro.Resource
extension, if the resource is configured as a custom page.
examples
Examples
iex> page_module(User)
nil
@spec page_route_path(Ash.Resource.t()) :: binary() | nil
The page routing path of the resource as defined in the Pyro.Resource
extension, if the resource is configured as a page.
examples
Examples
iex> page_route_path(AttendanceType)
"attendance-types"
@spec primary_actions(Ash.Resource.t(), [ Ash.Policy.Info.can_option() | primary_actions_opt() ]) :: [ Ash.Resource.Actions.action() ]
Returns the list of actions of the given resource.
Passing exclude_types: [...]
will filter out the specified action types from the list.
examples
Examples
iex> r = primary_actions(User)
iex> r |> Enum.map(& &1.name)
[:update, :create, :read]
iex> r = primary_actions(User, exclude_types: [:create, :update])
iex> r |> Enum.map(& &1.name)
[:read]
@spec public_fields(Ash.Resource.t()) :: [ash_resource_field()]
Lists all the public fields of resource.
examples
Examples
iex> public_fields(AttendanceType) |> Enum.map(& &1.name)
[
:label,
:order,
:shortcut,
:notes,
:inserted_at,
:updated_at,
:id,
:records_count,
:records
]
@spec public_fields( Ash.Resource.t(), ash_action_or_name(), ash_actor() ) :: [ash_resource_field()]
The same as public_fields/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> public_fields(AttendanceType, :read, nil) |> Enum.map(& &1.name)
[]
@spec read_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Read.t()]
Returns the list of :read
type actions of the given resource.
examples
Examples
iex> r = read_actions(Gage)
iex> r |> Enum.map(& &1.name)
[:calibration_alerts, :read]
@spec read_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Read.t() ]
The same as read_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = read_actions(Gage, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec resource_by_path(Ash.Resource.t(), [atom() | binary()]) :: Ash.Resource.t()
@spec resource_label(Ash.Resource.t()) :: binary() | nil
The label of the resource as defined in the Pyro.Resource
extension, defaulting to a humanized version of the module name.
examples
Examples
iex> resource_label(User)
"User"
@spec show_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Read.t()]
Returns the list of :read
type actions intended for single-record reads of the given resource.
examples
Examples
iex> r = show_actions(Record)
iex> r |> Enum.map(& &1.name)
[:show]
@spec show_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Read.t() ]
The same as show_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = show_actions(Record, nil)
iex> r |> Enum.map(& &1.name)
[]
@spec single_field_identities(Ash.Resource.t()) :: [Ash.Resource.Identity.t()]
List the single-keyed identities of the given resource.
examples
Examples
iex> single_field_identities(User)
[
%Ash.Resource.Identity{
description: nil,
eager_check_with: nil,
keys: [:email],
name: :name,
pre_check_with: nil
}
]
@spec sortable_field?( Ash.Resource.t(), ash_resource_field_or_name() ) :: boolean()
Check if a field is sortable.
examples
Examples
iex> sortable_field?(User, field(User, :email))
true
iex> sortable_field?(User, :name)
true
@spec update_actions(Ash.Resource.t()) :: [Ash.Resource.Actions.Update.t()]
Returns the list of :update
type actions of the given resource.
examples
Examples
iex> r = update_actions(Gage)
iex> r |> Enum.map(& &1.name)
[:update]
@spec update_actions(Ash.Resource.t(), ash_actor(), [Ash.Policy.Info.can_option()]) :: [ Ash.Resource.Actions.Update.t() ]
The same as update_actions/1
, but filtered by those authorized for access for the specified action/actor.
examples
Examples
iex> r = update_actions(Gage, nil)
iex> r |> Enum.map(& &1.name)
[]