PhoenixKit.Users.FormatCountry (phoenix_kit v2.64.1)

Copy Markdown View Source

Which country's formats a user reads: resolve/1.

The first of these that names a country BeamLabCountries knows:

  1. the user's own country (a person's, or a company account's own);
  2. the country of the company the user belongs to (organization_uuid);
  3. the registration_country the account signed up from;
  4. the site default, Settings → General → format_country.

nil means nothing names one, and callers fall back to what they used before (for dates, the site-wide date_format setting).

The viewer

A page is written for whoever looks at it. Mounting or refreshing the scope sets the viewer (put_viewer/1); their country is resolved on first use and kept in the process dictionary, as the Gettext locale is. A site-default fallback follows the settings cache rather than being kept for the session. PhoenixKit.Utils.Date reads it (viewer/0) so every date a page prints follows the viewer without the caller passing anything. With no viewer set (a background job, a signed-out visitor) viewer/0 is the site default.

The functions of PhoenixKit.Utils.Date that take a user ask for_user/1 instead: the user given is the one whose format is wanted, not the one in the process. A viewer's own record reuses the answer already resolved for them.

A host that renders for someone other than the process's viewer calls for_user/1 and passes the code on; it never has to touch the process dictionary.

Summary

Functions

Forgets the viewer, so viewer/0 is the site default again.

resolve/1 for a user that is not necessarily the viewer: the viewer's own record reuses the country already resolved for the process.

code as the upper-case alpha-2 code of a country BeamLabCountries knows, else nil. Blank, a name instead of a code, a retired or made-up code are all nil: a bad value in one source must not stop the next from being tried.

Sets the viewer of this process. Called when a scope is mounted and again after the viewer saves their profile.

The country whose formats user reads, as an upper-case alpha-2 code, or nil. A nil user (nobody signed in), or anything that is not a user row, gets the site default.

The site default country (Settings → General), or nil when none is chosen. Read from the settings cache.

The viewer's country: what put_viewer/1 named, or the site default when nobody was set for this process. Only the user's country sources are memoized; the site default is read from the settings cache so a settings change applies to an already-mounted viewer too.

Functions

clear_viewer()

@spec clear_viewer() :: :ok

Forgets the viewer, so viewer/0 is the site default again.

for_user(user)

@spec for_user(PhoenixKit.Users.Auth.User.t() | term()) :: String.t() | nil

resolve/1 for a user that is not necessarily the viewer: the viewer's own record reuses the country already resolved for the process.

normalize(code)

@spec normalize(term()) :: String.t() | nil

code as the upper-case alpha-2 code of a country BeamLabCountries knows, else nil. Blank, a name instead of a code, a retired or made-up code are all nil: a bad value in one source must not stop the next from being tried.

put_viewer(user)

@spec put_viewer(PhoenixKit.Users.Auth.User.t() | nil) :: :ok

Sets the viewer of this process. Called when a scope is mounted and again after the viewer saves their profile.

The country is not looked up here: most requests never print a date, and the company lookup is a query. viewer/0 resolves it the first time it is asked.

resolve(user)

@spec resolve(PhoenixKit.Users.Auth.User.t() | term()) :: String.t() | nil

The country whose formats user reads, as an upper-case alpha-2 code, or nil. A nil user (nobody signed in), or anything that is not a user row, gets the site default.

site_default()

@spec site_default() :: String.t() | nil

The site default country (Settings → General), or nil when none is chosen. Read from the settings cache.

viewer()

@spec viewer() :: String.t() | nil

The viewer's country: what put_viewer/1 named, or the site default when nobody was set for this process. Only the user's country sources are memoized; the site default is read from the settings cache so a settings change applies to an already-mounted viewer too.