Skip to content

User ​

The host application's User model, into which Trek injects authentication (has_secure_password and database-backed sessions), roles, theme & locale preferences and the invitation workflow.

Columns added by Trek ​

ColumnTypeNotes
namestringdisplay name
roleintegerenum, default guest
themeintegerenum, default auto
localestringadmin UI language
password_digeststringhas_secure_password (bcrypt), required
last_activity_atdatetimeupdated after each signed-in admin request

When the app has no User model yet, Trek creates the users table with its email column. Signed-in browsers are UserSession records (user_agent, ip_address) in a user_sessions table, found through a signed session_id cookie. Password reset (15 minutes) and invitation (1 week) links carry tokens from generates_token_for, so no column stores them.

Roles ​

Trek::Users::Roles defines the role enum and access levels:

ruby
enum :role, { admin: 5, editor: 4, user: 3, reader: 2, guest: 1 }, default: "guest"
  • privileged? — true for admin and editor; this is what the default policies check
  • User.human_roles — translated role names for form selects

Themes ​

Trek::Users::Themes lets each user pick the admin theme:

ruby
enum :theme, { auto: 0, light: 1, dark: 2 }, default: "auto"

User.human_themes returns translated theme names.

Locales ​

Trek::Users::Locales provides User.human_locales — the available admin UI languages with translated labels, sourced from I18n.available_locales.

Invitations ​

Trek::Users::Invitable adds a transient send_invite flag: when set on creation, the user receives an invitation email with a password-reset link.

ruby
User.create(email: "new@example.com", role: :editor, send_invite: true)

Validations ​

Trek::Users::Validations enforces:

  • email — presence, uniqueness, valid format
  • password — minimum 8 characters (on creation or password change)

Scopes ​

Trek::Users::Scopes adds User.ordered — alphabetical by name, then email.

Form models ​

Two non-persisted ActiveModel::Model companions handle the auth flows:

  • Trek::UserSession — email + password, used by the login form
  • Trek::UserPasswordReset — email, looks up the user to send a reset link to

Released under the MIT License.