General development - AlfredGranson/pcrm GitHub Wiki

Common tools

General commands

  • Running tests: MIX_ENV=test mix test

DB commands

Since pcrm employs phoenix it also employs ecto. It is encouraged to read the ecto docs, however, the most used tools for working with pcrm are documented here.

Generating a new resource

LiveView

  1. Generate a new live resource with mix phx.gen.live.

    ex: mix phx.gen.live Customers Customer customers family_name:string

  2. The console will provide you with a list of routes to include in the lib/pcrm_web/router.ex file under the / scope.
  3. Before migrating, update the new migration file to correspond with pcrm's standards.
    1. Ensure citext is enabled after the def change do:

      ex: execute "CREATE EXTENSION IF NOT EXISTS citext", "DROP EXTENSION IF EXISTS citext"

    2. Ensure the table's id is a uuid.
      1. Indicate primary_key: false in the create table attributes

        ex: create table(:customers, primary_key: false) do

      2. Add the uuid as a field.

        ex: add :id, :binary_id, primary_key: true

      3. Update fields to reflect types
        1. Text fields that require sorting should be changed from type :string to type :citext
        2. Required fields should have null: false added to their attributes.

          ex: add :family_name, :citext, null: false

  4. Update the new schema file to correspond with pcrm's standards.
    1. Indicate the id is a uuid.

      ex: @primary_key {:id, :binary_id, autogenerate: true}

    2. Remove non-required fields from the validate_required function call.

      ex: |> validate_required([:family_name])

  5. Several .html.heex files will be generated in lib/pcrm_web/[resource name]_live/.
    1. All generated text custom to the resource should be translated using dgettext provided the resource's name as a domain (lower case and plural)

      ex: <%= dgettext "customers", "Family name"%>

    2. All universal generated text should be translated using gettext

      ex: <%= gettext "Welcome to %{name}!", name: "pcrm" %>

    3. Run mix gettext.extract --merge inside the container. This will update the translation files inside of priv/gettext. Any strings that require translated should be done so at this point.
  6. The generated controller (ex. lib/pcrm/customers.ex) will need to have it's inserts/updates/deleted functions updated to use PaperTrail instead of Repo

    ex: |> PaperTrail.insert()

  7. The generated live test(s) (ex. test/pcrm_web/live/customer_live_test.exs) require updating.
    1. All tests that require authentication need to utilize the register_and_log_in_user function to pass.

      ex: %{conn: conn} = register_and_log_in_user(%{conn: conn})

    2. Create/update/delete functions require updated pattern matching for Paper Trail.

      ex: assert {:ok, %{model: %Customer{}} = %{model: customer}}

  8. You can now run mix ecto.migrate inside the container to migrate the database.