forked from shibao/cannery
		
	update gettext schema and use macros for cannery app
This commit is contained in:
		@@ -6,4 +6,34 @@ defmodule Cannery do
 | 
			
		||||
  Contexts are also responsible for managing your data, regardless
 | 
			
		||||
  if it comes from the database, an external API or others.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  def context do
 | 
			
		||||
    quote do
 | 
			
		||||
      use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
      import Ecto.Query
 | 
			
		||||
      alias Cannery.Accounts.User
 | 
			
		||||
      alias Cannery.Repo
 | 
			
		||||
      alias Ecto.{Changeset, Multi, Queryable, UUID}
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def schema do
 | 
			
		||||
    quote do
 | 
			
		||||
      use Ecto.Schema
 | 
			
		||||
      use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
      import Ecto.{Changeset, Query}
 | 
			
		||||
      alias Cannery.Accounts.User
 | 
			
		||||
      alias Ecto.{Association, Changeset, Queryable, UUID}
 | 
			
		||||
 | 
			
		||||
      @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
      @foreign_key_type :binary_id
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @doc """
 | 
			
		||||
  When used, dispatch to the appropriate context/schema/etc.
 | 
			
		||||
  """
 | 
			
		||||
  defmacro __using__(which) when is_atom(which) do
 | 
			
		||||
    apply(__MODULE__, which, [])
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,9 @@ defmodule Cannery.Accounts do
 | 
			
		||||
  The Accounts context.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  import Ecto.Query, warn: false
 | 
			
		||||
  alias Cannery.{Mailer, Repo}
 | 
			
		||||
  alias Cannery.Accounts.{Invite, Invites, User, UserToken}
 | 
			
		||||
  alias Ecto.{Changeset, Multi}
 | 
			
		||||
  use Cannery, :context
 | 
			
		||||
  alias Cannery.Mailer
 | 
			
		||||
  alias Cannery.Accounts.{Invite, Invites, UserToken}
 | 
			
		||||
  alias Oban.Job
 | 
			
		||||
 | 
			
		||||
  ## Database getters
 | 
			
		||||
 
 | 
			
		||||
@@ -5,13 +5,8 @@ defmodule Cannery.Accounts.Invite do
 | 
			
		||||
  `:uses_left` is defined.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Cannery.Accounts.User
 | 
			
		||||
  alias Ecto.{Association, Changeset, UUID}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "invites" do
 | 
			
		||||
    field :name, :string
 | 
			
		||||
    field :token, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,8 @@ defmodule Cannery.Accounts.Invites do
 | 
			
		||||
  The Invites context.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  import Ecto.Query, warn: false
 | 
			
		||||
  alias Ecto.Multi
 | 
			
		||||
  alias Cannery.Accounts.{Invite, User}
 | 
			
		||||
  alias Cannery.Repo
 | 
			
		||||
  use Cannery, :context
 | 
			
		||||
  alias Cannery.Accounts.Invite
 | 
			
		||||
 | 
			
		||||
  @invite_token_length 20
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,11 +3,8 @@ defmodule Cannery.Accounts.User do
 | 
			
		||||
  A Cannery user
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Ecto.{Association, Changeset, UUID}
 | 
			
		||||
  alias Cannery.Accounts.{Invite, User}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.Accounts.Invite
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
@@ -20,8 +17,6 @@ defmodule Cannery.Accounts.User do
 | 
			
		||||
             :updated_at
 | 
			
		||||
           ]}
 | 
			
		||||
  @derive {Inspect, except: [:password]}
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "users" do
 | 
			
		||||
    field :email, :string
 | 
			
		||||
    field :password, :string, virtual: true
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,7 @@ defmodule Cannery.Accounts.UserToken do
 | 
			
		||||
  Schema for a user's session token
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Query
 | 
			
		||||
  alias Cannery.Accounts.User
 | 
			
		||||
  alias Ecto.{Association, UUID}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
 | 
			
		||||
  @hash_algorithm :sha256
 | 
			
		||||
  @rand_size 32
 | 
			
		||||
@@ -18,8 +15,6 @@ defmodule Cannery.Accounts.UserToken do
 | 
			
		||||
  @change_email_validity_in_days 7
 | 
			
		||||
  @session_validity_in_days 60
 | 
			
		||||
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "users_tokens" do
 | 
			
		||||
    field :token, :binary
 | 
			
		||||
    field :context, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,8 @@ defmodule Cannery.ActivityLog do
 | 
			
		||||
  The ActivityLog context.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  import Ecto.Query, warn: false
 | 
			
		||||
  alias Cannery.Ammo.{Pack, Type}
 | 
			
		||||
  alias Cannery.{Accounts.User, ActivityLog.ShotRecord, Repo}
 | 
			
		||||
  alias Ecto.{Multi, Queryable}
 | 
			
		||||
  use Cannery, :context
 | 
			
		||||
  alias Cannery.{ActivityLog.ShotRecord, Ammo.Pack, Ammo.Type}
 | 
			
		||||
 | 
			
		||||
  @type list_shot_records_option ::
 | 
			
		||||
          {:search, String.t() | nil}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,11 +3,8 @@ defmodule Cannery.ActivityLog.ShotRecord do
 | 
			
		||||
  A shot record records a group of ammo shot during a range trip
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Cannery.{Accounts.User, Ammo, Ammo.Pack}
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.{Ammo, Ammo.Pack}
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
 
 | 
			
		||||
@@ -3,13 +3,11 @@ defmodule Cannery.Ammo do
 | 
			
		||||
  The Ammo context.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  import Ecto.Query, warn: false
 | 
			
		||||
  alias Cannery.{Accounts.User, Containers, Repo}
 | 
			
		||||
  use Cannery, :context
 | 
			
		||||
  alias Cannery.Containers
 | 
			
		||||
  alias Cannery.Containers.{Container, ContainerTag, Tag}
 | 
			
		||||
  alias Cannery.{ActivityLog, ActivityLog.ShotRecord}
 | 
			
		||||
  alias Cannery.Ammo.{Pack, Type}
 | 
			
		||||
  alias Ecto.{Changeset, Queryable}
 | 
			
		||||
 | 
			
		||||
  @pack_create_limit 10_000
 | 
			
		||||
  @pack_preloads [:type]
 | 
			
		||||
 
 | 
			
		||||
@@ -6,12 +6,8 @@ defmodule Cannery.Ammo.Pack do
 | 
			
		||||
  amount paid for that ammunition, or what condition it is in
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Cannery.Ammo.Type
 | 
			
		||||
  alias Cannery.{Accounts.User, Containers, Containers.Container}
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.{Ammo.Type, Containers, Containers.Container}
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
@@ -24,8 +20,6 @@ defmodule Cannery.Ammo.Pack do
 | 
			
		||||
             :type_id,
 | 
			
		||||
             :container_id
 | 
			
		||||
           ]}
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "packs" do
 | 
			
		||||
    field :count, :integer
 | 
			
		||||
    field :notes, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -5,11 +5,8 @@ defmodule Cannery.Ammo.Type do
 | 
			
		||||
  Contains statistical information about the ammunition.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Cannery.Accounts.User
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.Ammo.Pack
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
@@ -46,8 +43,6 @@ defmodule Cannery.Ammo.Type do
 | 
			
		||||
             :shot_charge_weight,
 | 
			
		||||
             :dram_equivalent
 | 
			
		||||
           ]}
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "types" do
 | 
			
		||||
    field :name, :string
 | 
			
		||||
    field :desc, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -3,11 +3,9 @@ defmodule Cannery.Containers do
 | 
			
		||||
  The Containers context.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  import Ecto.Query, warn: false
 | 
			
		||||
  alias Cannery.{Accounts.User, Ammo.Pack, Repo}
 | 
			
		||||
  use Cannery, :context
 | 
			
		||||
  alias Cannery.Ammo.Pack
 | 
			
		||||
  alias Cannery.Containers.{Container, ContainerTag, Tag}
 | 
			
		||||
  alias Ecto.{Changeset, Queryable}
 | 
			
		||||
 | 
			
		||||
  @container_preloads [:tags]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,8 @@ defmodule Cannery.Containers.Container do
 | 
			
		||||
  A container that holds ammunition and belongs to a user.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
  alias Cannery.{Accounts.User, Containers.ContainerTag, Containers.Tag}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.{Containers.ContainerTag, Containers.Tag}
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
@@ -17,8 +15,6 @@ defmodule Cannery.Containers.Container do
 | 
			
		||||
             :type,
 | 
			
		||||
             :tags
 | 
			
		||||
           ]}
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "containers" do
 | 
			
		||||
    field :name, :string
 | 
			
		||||
    field :desc, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -4,13 +4,9 @@ defmodule Cannery.Containers.ContainerTag do
 | 
			
		||||
  Cannery.Containers.Tag.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
  alias Cannery.Containers.{Container, Tag}
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "container_tags" do
 | 
			
		||||
    belongs_to :container, Container
 | 
			
		||||
    belongs_to :tag, Tag
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,7 @@ defmodule Cannery.Containers.Tag do
 | 
			
		||||
  text and bg colors.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Ecto.Schema
 | 
			
		||||
  import Ecto.Changeset
 | 
			
		||||
  alias Cannery.Accounts.User
 | 
			
		||||
  alias Ecto.{Changeset, UUID}
 | 
			
		||||
  use Cannery, :schema
 | 
			
		||||
 | 
			
		||||
  @derive {Jason.Encoder,
 | 
			
		||||
           only: [
 | 
			
		||||
@@ -16,8 +13,6 @@ defmodule Cannery.Containers.Tag do
 | 
			
		||||
             :bg_color,
 | 
			
		||||
             :text_color
 | 
			
		||||
           ]}
 | 
			
		||||
  @primary_key {:id, :binary_id, autogenerate: true}
 | 
			
		||||
  @foreign_key_type :binary_id
 | 
			
		||||
  schema "tags" do
 | 
			
		||||
    field :name, :string
 | 
			
		||||
    field :bg_color, :string
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,8 @@ defmodule Cannery.Email do
 | 
			
		||||
  `lib/cannery_web/components/layouts/email_text.txt.eex` for text emails.
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
  import Swoosh.Email
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  import Phoenix.Template
 | 
			
		||||
  alias Cannery.Accounts.User
 | 
			
		||||
  alias CanneryWeb.{EmailHTML, Layouts}
 | 
			
		||||
 
 | 
			
		||||
@@ -42,9 +42,10 @@ defmodule CanneryWeb do
 | 
			
		||||
        formats: [:html, :json],
 | 
			
		||||
        layouts: [html: CanneryWeb.Layouts]
 | 
			
		||||
 | 
			
		||||
      use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
      # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
 | 
			
		||||
      import Plug.Conn
 | 
			
		||||
      import CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
      unquote(verified_routes())
 | 
			
		||||
    end
 | 
			
		||||
@@ -84,8 +85,9 @@ defmodule CanneryWeb do
 | 
			
		||||
  defp html_helpers do
 | 
			
		||||
    quote do
 | 
			
		||||
      use PhoenixHTMLHelpers
 | 
			
		||||
      use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
      import Phoenix.{Component, HTML, HTML.Form}
 | 
			
		||||
      import CanneryWeb.{ErrorHelpers, Gettext, CoreComponents, HTMLHelpers}
 | 
			
		||||
      import CanneryWeb.{ErrorHelpers, CoreComponents, HTMLHelpers}
 | 
			
		||||
 | 
			
		||||
      # Shortcut for generating JS commands
 | 
			
		||||
      alias Phoenix.LiveView.JS
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,8 @@ defmodule CanneryWeb.CoreComponents do
 | 
			
		||||
  """
 | 
			
		||||
  use Phoenix.Component
 | 
			
		||||
  use CanneryWeb, :verified_routes
 | 
			
		||||
  import CanneryWeb.{Gettext, HTMLHelpers}
 | 
			
		||||
  use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
  import CanneryWeb.HTMLHelpers
 | 
			
		||||
  alias Cannery.{Accounts, Accounts.Invite, Accounts.User}
 | 
			
		||||
  alias Cannery.{Ammo, Ammo.Pack}
 | 
			
		||||
  alias Cannery.{Containers.Container, Containers.Tag}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
defmodule CanneryWeb.ErrorJSON do
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
  def render(template, _assigns) do
 | 
			
		||||
    error_string =
 | 
			
		||||
 
 | 
			
		||||
@@ -4,9 +4,9 @@ defmodule CanneryWeb.UserAuth do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb, :verified_routes
 | 
			
		||||
  use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
  import Plug.Conn
 | 
			
		||||
  import Phoenix.Controller
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.{Accounts, Accounts.User}
 | 
			
		||||
 | 
			
		||||
  # Make the remember me cookie valid for 60 days.
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,5 @@
 | 
			
		||||
defmodule CanneryWeb.UserConfirmationController do
 | 
			
		||||
  use CanneryWeb, :controller
 | 
			
		||||
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.Accounts
 | 
			
		||||
 | 
			
		||||
  def new(conn, _params) do
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
defmodule CanneryWeb.UserRegistrationController do
 | 
			
		||||
  use CanneryWeb, :controller
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.{Accounts, Accounts.Invites}
 | 
			
		||||
  alias Ecto.Changeset
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
defmodule CanneryWeb.UserSettingsController do
 | 
			
		||||
  use CanneryWeb, :controller
 | 
			
		||||
  import CanneryWeb.Gettext
 | 
			
		||||
  alias Cannery.Accounts
 | 
			
		||||
  alias CanneryWeb.UserAuth
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ defmodule CanneryWeb.Gettext do
 | 
			
		||||
  By using [Gettext](https://hexdocs.pm/gettext),
 | 
			
		||||
  your module gains a set of macros for translations, for example:
 | 
			
		||||
 | 
			
		||||
      import CanneryWeb.Gettext
 | 
			
		||||
      use Gettext, backend: CanneryWeb.Gettext
 | 
			
		||||
 | 
			
		||||
      # Simple translation
 | 
			
		||||
      gettext("Here is the string to translate")
 | 
			
		||||
@@ -20,5 +20,5 @@ defmodule CanneryWeb.Gettext do
 | 
			
		||||
 | 
			
		||||
  See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
 | 
			
		||||
  """
 | 
			
		||||
  use Gettext, otp_app: :cannery
 | 
			
		||||
  use Gettext.Backend, otp_app: :cannery
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user