Current section

23 Versions

Jump to

Compare versions

4 files changed
+12 additions
-42 deletions
  @@ -7,9 +7,7 @@
7 7 [<<"lib/ecto_discriminator">>,
8 8 <<"lib/ecto_discriminator/discriminator_type.ex">>,
9 9 <<"lib/ecto_discriminator/discriminator_changeset.ex">>,
10 - <<"lib/ecto_discriminator/schema.ex">>,
11 - <<"lib/ecto_discriminator/discriminator_query.ex">>,<<"mix.exs">>,
12 - <<"README.md">>]}.
10 + <<"lib/ecto_discriminator/schema.ex">>,<<"mix.exs">>,<<"README.md">>]}.
13 11 {<<"licenses">>,[<<"MIT">>]}.
14 12 {<<"links">>,
15 13 [{<<"github">>,<<"https://github.com/cichacz/ecto_discriminator">>}]}.
  @@ -20,4 +18,4 @@
20 18 {<<"optional">>,false},
21 19 {<<"repository">>,<<"hexpm">>},
22 20 {<<"requirement">>,<<"~> 3.0">>}]]}.
23 - {<<"version">>,<<"0.3.2">>}.
21 + {<<"version">>,<<"0.3.3">>}.
  @@ -95,8 +95,15 @@ defimpl EctoDiscriminator.DiscriminatorChangeset, for: Ecto.Changeset do
95 95 Ecto.Changeset.get_field(changeset, discriminator) ||
96 96 struct
97 97
98 - # have to drop __meta__ because it comes from base schema
99 - data = data |> Map.from_struct() |> Map.delete(:__meta__)
98 + data =
99 + data
100 + |> Map.from_struct()
101 + # have to drop __meta__ because it comes from base schema
102 + |> Map.delete(:__meta__)
103 + # take only items that hold some value
104 + |> Enum.reject(fn {_, value} -> match?(%Ecto.Association.NotLoaded{}, value) end)
105 + |> Enum.into(%{})
106 +
100 107 data = struct(diverged_schema, data)
101 108
102 109 # just call changeset from the derived schema and hope it calls cast_base to pull fields from the base schema
  @@ -1,35 +0,0 @@
1 - defmodule EctoDiscriminator.DiscriminatorQuery do
2 - @moduledoc """
3 - Extension for `Ecto.Query` that simplifies building queries for diverged schemas
4 -
5 - ## Basic Querying
6 -
7 - Diverged schemas have some logic injected that allows very simple querying:
8 -
9 - MyApp.Repo.all(SomeTable.Foo)
10 -
11 - This will generate SQL similar to this:
12 -
13 - SELECT ... FROM some_table WHERE discriminator = "Elixir.SomeTable.Foo"
14 -
15 - If the injected `where` condition causes some issues (eg. in some advanced SQL) you can exclude it on the beginning:
16 -
17 - SomeTable.Foo
18 - |> exclude(:where)
19 - |> MyApp.Repo.all()
20 - #=> SELECT ... FROM some_table
21 -
22 - ## Polymorphic Querying
23 -
24 - When some module has an association that uses `EctoDiscriminator.Schema` then this module comes to play.
25 -
26 - ### `assoc/3`
27 -
28 - You can use `assoc/3` to specify type of desired data, only if the type is known upfront. Go to `struct/3` If type needs to be determined dynamically.
29 -
30 - """
31 -
32 - def assoc() do
33 -
34 - end
35 - end
  @@ -2,7 +2,7 @@ defmodule EctoDiscriminator.MixProject do
2 2 use Mix.Project
3 3
4 4 @source_url "https://github.com/cichacz/ecto_discriminator"
5 - @version "0.3.2"
5 + @version "0.3.3"
6 6
7 7 def project do
8 8 [