From 54250a5a95aaf922805874bc8eeab3260c491663 Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Mon, 9 Jan 2023 21:09:46 +0100 Subject: [PATCH] Fix user/deepl_account relation --- lib/outlook/accounts/user.ex | 1 + lib/outlook/translators/deepl_account.ex | 7 +++---- lib/outlook_web/live/deepl_account_live/form_component.ex | 1 + lib/outlook_web/live/deepl_account_live/index.ex | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/outlook/accounts/user.ex b/lib/outlook/accounts/user.ex index 15a8310..62d2267 100644 --- a/lib/outlook/accounts/user.ex +++ b/lib/outlook/accounts/user.ex @@ -7,6 +7,7 @@ defmodule Outlook.Accounts.User do field :password, :string, virtual: true, redact: true field :hashed_password, :string, redact: true field :confirmed_at, :naive_datetime + has_one :deepl_account, Outlook.Translators.DeeplAccount timestamps() end diff --git a/lib/outlook/translators/deepl_account.ex b/lib/outlook/translators/deepl_account.ex index 2c8c764..153eaf3 100644 --- a/lib/outlook/translators/deepl_account.ex +++ b/lib/outlook/translators/deepl_account.ex @@ -9,8 +9,7 @@ defmodule Outlook.Translators.DeeplAccount do field :description, :string field :name, :string field :our_character_count, :integer - field :user_id, :id - belongs_to :users, User + belongs_to :user, Outlook.Accounts.User timestamps() end @@ -18,7 +17,7 @@ defmodule Outlook.Translators.DeeplAccount do @doc false def changeset(deepl_account, attrs) do deepl_account - |> cast(attrs, [:name, :description, :auth_key, :character_limit, :character_count, :our_character_count]) - |> validate_required([:name, :description, :auth_key, :character_limit]) + |> cast(attrs, [:name, :description, :auth_key, :character_limit, :character_count, :our_character_count, :user_id]) + |> validate_required([:name, :description, :auth_key, :character_limit, :user_id]) end end diff --git a/lib/outlook_web/live/deepl_account_live/form_component.ex b/lib/outlook_web/live/deepl_account_live/form_component.ex index 8aaf717..e9af2a4 100644 --- a/lib/outlook_web/live/deepl_account_live/form_component.ex +++ b/lib/outlook_web/live/deepl_account_live/form_component.ex @@ -20,6 +20,7 @@ defmodule OutlookWeb.DeeplAccountLive.FormComponent do phx-change="validate" phx-submit="save" > + <.input field={{f, :user_id}} type="hidden" /> <.input field={{f, :name}} type="text" label="name" /> <.input field={{f, :description}} type="text" label="description" /> <.input field={{f, :auth_key}} type="text" label="auth_key" /> diff --git a/lib/outlook_web/live/deepl_account_live/index.ex b/lib/outlook_web/live/deepl_account_live/index.ex index aefd01c..67f479e 100644 --- a/lib/outlook_web/live/deepl_account_live/index.ex +++ b/lib/outlook_web/live/deepl_account_live/index.ex @@ -23,7 +23,7 @@ defmodule OutlookWeb.DeeplAccountLive.Index do defp apply_action(socket, :new, _params) do socket |> assign(:page_title, "New Deepl account") - |> assign(:deepl_account, %DeeplAccount{}) + |> assign(:deepl_account, %DeeplAccount{user_id: socket.assigns.current_user}) end defp apply_action(socket, :index, _params) do