Traits

Let's flesh out our user model a little:

  • We've implemented email verification so users have a verified flag indicating whether their email has been verified

  • After some of our admins were harassed we've decided to give their accounts anonymized email addresses

  • We've also noticed that we're writing tests for users more frequently, so we want to change the default role in the factory to "user"

Now our email key is linked to the role of the user, so we might want to control these two together. We can use traits to do this. Traits are like little snippets of a template we can control with a single keyword. Let's update our factory definition a little:

(ns fab.tutorial
 (:require [fabrikk.alpha.core :as fab]))
 
(def user
  (fab/->factory
   ::user
   {:template {:name "John Smith"
               :email "john@example.org"
               :role "user"
               :verified true}
    :traits {:admin {:email "admin-0001@example.org"
                     :role "admin"}
             :unverified {:verified false}}}))

We've added a traits key to the factory definition, and added two traits admin and unverified. Now we can use the traits as shorthand when we build by specifying them in the build options:

(fab/build user)
;; => {:name "John Smith", :email "john@example.org", :role "user", :verified true}

(fab/build user {:traits [:admin]})
;; => {:name "John Smith", :email "admin-0001@example.org", :role "admin", :verified true}

Traits can be composed:

(fab/build user {:traits [:admin :unverified]})
;; => {:name "John Smith", :email "admin-0001@example.org", :role "admin", :verified false}

Last updated