Skip to content

Instantly share code, notes, and snippets.

@bamorim
Last active May 2, 2024 19:25
Show Gist options
  • Save bamorim/10ad7ff200deccea67dde8ea54366fb4 to your computer and use it in GitHub Desktop.
Save bamorim/10ad7ff200deccea67dde8ea54366fb4 to your computer and use it in GitHub Desktop.

Embeds One Default Demo

Success Cases

defmodule Post do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    field(:title, :string, default: "Untitled")
  end
end
{:module, Post, <<70, 79, 82, 49, 0, 0, 12, ...>>,
 [__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
defmodule User do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    embeds_one(:post, Post)

    embeds_one :settings, Settings, primary_key: false do
      field(:dark_mode, :boolean, default: true)
    end
  end
end
{:module, User, <<70, 79, 82, 49, 0, 0, 14, ...>>,
 [__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%User{}
%User{post: nil, settings: nil}
defmodule UserWithDefaults do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    embeds_one(:post, Post, default: %Post{})

    embeds_one :settings, Settings, primary_key: false, default: %Settings{} do
      field(:dark_mode, :boolean, default: true)
    end
  end
end
{:module, UserWithDefaults, <<70, 79, 82, 49, 0, 0, 15, ...>>,
 [__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%UserWithDefaults{}
%UserWithDefaults{
  post: %Post{title: "Untitled"},
  settings: %UserWithDefaults.Settings{dark_mode: true}
}
defmodule UserWithCustomDefaults do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    embeds_one(:post, Post, default: %Post{title: "Nice Title"})

    embeds_one :settings, Settings, primary_key: false, default: %Settings{} do
      field(:dark_mode, :boolean, default: true)
    end
  end
end
{:module, UserWithCustomDefaults, <<70, 79, 82, 49, 0, 0, 15, ...>>,
 [__schema__: 1, __schema__: 1, __schema__: 1, __schema__: 1, __schema__: 2, __schema__: 2, ...]}
%UserWithCustomDefaults{}
%UserWithCustomDefaults{
  post: %Post{title: "Nice Title"},
  settings: %UserWithCustomDefaults.Settings{dark_mode: true}
}

Error Cases

try do
  defmodule Error1 do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      embeds_one(:post, Post, default: true)

      embeds_one :settings, Settings, primary_key: false do
        field(:dark_mode, :boolean, default: true)
      end
    end
  end
rescue
  e in ArgumentError ->
    IO.puts(e.message)
end
invalid `:default` option for :post. The only valid option is a struct value like `%Post{}`
:ok
try do
  defmodule Error2 do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      embeds_one(:post, Post, default: %User{})

      embeds_one :settings, Settings, primary_key: false do
        field(:dark_mode, :boolean, default: true)
      end
    end
  end
rescue
  e in ArgumentError ->
    IO.puts(e.message)
end
invalid `:default` option for :post. The only valid option is a struct value like `%Post{}`
:ok
try do
  defmodule Error3 do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      embeds_one(:post, Post)

      embeds_one :settings, Settings, primary_key: false, default: true do
        field(:dark_mode, :boolean, default: true)
      end
    end
  end
rescue
  e in ArgumentError ->
    IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
:ok
try do
  defmodule Error4 do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      embeds_one(:post, Post)

      embeds_one :settings, Settings, primary_key: false, default: %User{} do
        field(:dark_mode, :boolean, default: true)
      end
    end
  end
rescue
  e in ArgumentError ->
    IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
:ok
try do
  defmodule Error5 do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      embeds_one(:post, Post)

      embeds_one :settings, Settings, primary_key: false, default: %Settings{dark_mode: false} do
        field(:dark_mode, :boolean, default: true)
      end
    end
  end
rescue
  e in ArgumentError ->
    IO.puts(e.message)
end
invalid `:default` option for :settings. The only valid option is a struct value like `%Settings{}`
  With inline embeds you also cannot override any defaults, so %Settings{dark_mode: false} is also invalid.
  If you want to set defaults set it at the field definition or change a non-inline embed.
:ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment