Skip to content

Commit

Permalink
Feat: Update BooleanGroup for ecto 3.5 (#35)
Browse files Browse the repository at this point in the history
* Latest ecto returns a :parameterized type for embedded fields
* Update BooleanGroup module to handle this new field type
* Add tests for embedded feature flags on a post
* Update styles for BooleanGroup detail to use the same styles as the Boolean field
  • Loading branch information
alexandreaco committed Nov 4, 2020
1 parent a5f7261 commit 49228f0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
16 changes: 5 additions & 11 deletions assets/src/components/Detail/BooleanGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
<li
v-for="option in value"
:key="option.name"
class="mb-1"
class="mb-1 text-gray-darkest"
>
<span
:class="classes[option.checked]"
class="inline-flex items-center py-1 pl-2 pr-3 rounded-full font-bold text-sm leading-tight"
>
<fake-checkbox
:checked="option.checked"
width="20"
height="20"
/>
<span class="ml-1">{{ option.label }}</span>
</span>
:class="{ 'bg-success': option.checked, 'bg-danger': !option.checked }"
class="inline-block rounded-full w-2 h-2 mr-1"
/>
<span>{{ option.label }}</span>
</li>
</ul>
<span v-else>{{ field.options.no_value || "No Data" }}</span>
Expand Down
27 changes: 18 additions & 9 deletions lib/ex_teal/fields/boolean_group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ defmodule ExTeal.Fields.BooleanGroup do
field

{:embed, embedded} ->
fields = embedded.related.__schema__(:fields)
parameterize_an_embed(field, embedded)

opts =
(fields -- [:id])
|> Enum.into(%{}, fn f ->
field_string = Atom.to_string(f)
{field_string, field_string}
end)

options(field, opts)
{:parameterized, Ecto.Embedded, %Ecto.Embedded{cardinality: :one}} ->
field
end
end

Expand All @@ -122,4 +116,19 @@ defmodule ExTeal.Fields.BooleanGroup do
def no_value_text(field, text) do
%{field | options: Map.put(field.options, :no_value, text)}
end

defp parameterize_an_embed(field, embedded_schema) do
fields = embedded_schema.related.__schema__(:fields)
group_options = Map.get(field.options, :group_options, %{})

opts =
(fields -- [:id])
|> Enum.into(%{}, fn f ->
field_string = Atom.to_string(f)
{field_string, field_string}
end)

opts = Map.merge(opts, group_options)
options(field, opts)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule ExTeal.MixProject do
{:plug_cowboy, "~> 2.0"},
{:phoenix, "~> 1.4"},
{:phoenix_html, ">= 2.11.0"},
{:ecto, "~> 3.3"},
{:ecto, "~> 3.5"},
{:ecto_sql, "~> 3.3"},
{:ex_machina, "~> 2.2", only: :test},
{:credo, "~> 1.3", only: [:dev, :test], runtime: false},
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm", "1e1a3d176d52daebbecbbcdfd27c27726076567905c2a9d7398c54da9d225761"},
"credo": {:hex, :credo, "1.3.1", "082e8d9268a489becf8e7aa75671a7b9088b1277cd6c1b13f40a55554b3f5126", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0da816ed52fa520b9ea0e5d18a0d3ca269e0bd410b1174d88d8abd94be6cce3c"},
"db_connection": {:hex, :db_connection, "2.2.1", "caee17725495f5129cb7faebde001dc4406796f12a62b8949f4ac69315080566", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "2b02ece62d9f983fcd40954e443b7d9e6589664380e5546b2b9b523cd0fb59e1"},
"decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"},
"decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"ecto": {:hex, :ecto, "3.3.4", "95b05c82ae91361475e5491c9f3ac47632f940b3f92ae3988ac1aad04989c5bb", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "9b96cbb83a94713731461ea48521b178b0e3863d310a39a3948c807266eebd69"},
"ecto": {:hex, :ecto, "3.5.4", "73ee115deb10769c73fd2d27e19e36bc4af7c56711ad063616a86aec44f80f6f", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7f13f9c9c071bd2ca04652373ff3edd1d686364de573255096872a4abc471807"},
"ecto_sql": {:hex, :ecto_sql, "3.3.4", "aa18af12eb875fbcda2f75e608b3bd534ebf020fc4f6448e4672fcdcbb081244", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4 or ~> 3.3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5eccbdbf92e3c6f213007a82d5dbba4cd9bb659d1a21331f89f408e4c0efd7a8"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
Expand All @@ -22,7 +22,7 @@
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.3.0", "f005ad692b717691203f940c686208aa3d8ffd9dd4bb3699240096a51fa9564e", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "abfb393ad888d57700f4d0f119c2643c8a9d98856f9b8a92001be7efad1419d6"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"inflex": {:hex, :inflex, "2.0.0", "db69d542b8fdb23ac667f9bc0c2395a3983fa2da6ae2efa7ab5dc541928f7a75", [:mix], [], "hexpm", "c018852409bd48b03ad96ed53594186bc074bdd1519043a0ad1fa5697aac4399"},
"jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
Expand Down
6 changes: 3 additions & 3 deletions test/ex_teal/resource/export_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule ExTeal.Resource.ExportTest do
[header, p1_csv, p2_csv] = String.split(data, "\n", trim: true)

assert header ==
"id,name,body,published,published_at,deleted_at,user_id,inserted_at,updated_at"
"id,name,body,published,published_at,deleted_at,features,user_id,inserted_at,updated_at"

assert String.contains?(p1_csv, "#{p1.id},#{p1.name}")
assert String.contains?(p2_csv, "#{p2.id},#{p2.name}")
Expand Down Expand Up @@ -156,7 +156,7 @@ defmodule ExTeal.Resource.ExportTest do
[header, p1_csv, p2_csv] = String.split(data, "\n", trim: true)

assert header ==
"id,name,body,published,published_at,deleted_at,user_id,inserted_at,updated_at"
"id,name,body,published,published_at,deleted_at,features,user_id,inserted_at,updated_at"

assert String.contains?(p1_csv, "#{p2.id},#{p2.name}")
assert String.contains?(p2_csv, "#{p1.id},#{p1.name}")
Expand Down Expand Up @@ -194,7 +194,7 @@ defmodule ExTeal.Resource.ExportTest do
fields = TestExTeal.PostResource.export_fields()

expected =
~w(id name body published published_at deleted_at user_id inserted_at updated_at)a
~w(id name body published published_at deleted_at features user_id inserted_at updated_at)a

assert fields == expected
end
Expand Down
1 change: 1 addition & 0 deletions test/support/migrations.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule TestExTeal.Migrations do
create table(:posts) do
add(:name, :string)
add(:body, :string)
add(:features, :map)
add(:user_id, references(:users))
add(:published, :boolean, default: false, null: false)
add(:published_at, :naive_datetime)
Expand Down
7 changes: 6 additions & 1 deletion test/support/resources.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end

defmodule TestExTeal.PostResource do
use ExTeal.Resource
alias ExTeal.Fields.{BelongsTo, Boolean, DateTime, ID, ManyToMany, Text, TextArea}
alias ExTeal.Fields.{BelongsTo, Boolean, BooleanGroup, DateTime, ID, ManyToMany, Text, TextArea}

def model, do: TestExTeal.Post

Expand All @@ -43,6 +43,11 @@ defmodule TestExTeal.PostResource do
DateTime.make(:published_at),
DateTime.make(:deleted_at),
BelongsTo.make(:user),
BooleanGroup.make(:features)
|> BooleanGroup.options(%{
"show_cta" => "Show CTA",
"show_featured_image" => "Show Featured Image"
}),
ManyToMany.make(:tags, TestExTeal.Tag)
]

Expand Down
22 changes: 21 additions & 1 deletion test/support/schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ defmodule TestExTeal.User do
end
end

defmodule TestExTeal.Features do
use Ecto.Schema
import Ecto.Changeset

embedded_schema do
field(:show_featured_image, :boolean, default: true)
field(:show_cta, :boolean, default: true)
end

def changeset(field, params) do
cast(field, params, [
:show_featured_image,
:show_cta
])
end
end

defmodule TestExTeal.Post do
use Ecto.Schema
import Ecto.Changeset
alias TestExTeal.Post
alias TestExTeal.{Features, Post}

schema "posts" do
field(:name, :string)
Expand All @@ -23,6 +40,8 @@ defmodule TestExTeal.Post do
field(:published_at, :naive_datetime)
field(:deleted_at, :utc_datetime)

embeds_one(:features, Features, on_replace: :update)

many_to_many(:tags, TestExTeal.Tag, join_through: "posts_tags", on_replace: :delete)

belongs_to(:user, TestExTeal.User)
Expand All @@ -35,6 +54,7 @@ defmodule TestExTeal.Post do
def changeset(%Post{} = post, params \\ %{}) do
post
|> cast(params, @fields)
|> cast_embed(:features)
|> validate_required([:name])
end
end
Expand Down

0 comments on commit 49228f0

Please sign in to comment.