From d467da2ca3aa53b44e77da9d2fed75ec6a13cfbc Mon Sep 17 00:00:00 2001 From: Arthur Clemens Date: Sun, 13 Aug 2023 19:32:30 +0200 Subject: [PATCH] Release 0.4.0 (#111) * Ensure correct order of theme menu items * Remove unused * Remove Ecto dependency --- lib/helpers/attribute_helpers.ex | 23 ++++++++++-- lib/helpers/form_helpers.ex | 2 +- lib/helpers/schema_helpers.ex | 35 ------------------- lib/theme/theme.ex | 10 ++++++ mix.exs | 13 ++++--- {lib/helpers => test/support}/test_helpers.ex | 0 .../support}/todos/todo.ex | 0 .../support}/todos/todos.ex | 0 8 files changed, 41 insertions(+), 42 deletions(-) rename {lib/helpers => test/support}/test_helpers.ex (100%) rename {lib/test_helpers => test/support}/todos/todo.ex (100%) rename {lib/test_helpers => test/support}/todos/todos.ex (100%) diff --git a/lib/helpers/attribute_helpers.ex b/lib/helpers/attribute_helpers.ex index 55f144ae..6431e280 100644 --- a/lib/helpers/attribute_helpers.ex +++ b/lib/helpers/attribute_helpers.ex @@ -310,9 +310,28 @@ defmodule PrimerLive.Helpers.AttributeHelpers do end @doc """ - Generates a random string. + From: https://gist.github.com/ahmadshah/8d978bbc550128cca12dd917a09ddfb7?permalink_comment_id=4178225#gistcomment-4178225 + + A helper functions to generate a random (printable) string of given `len`. + You could change the hard-coded ascii codes 32 and 126 to only + uppercase letters, digits, etc... """ - def random_string(), do: Ecto.UUID.generate() + def random_string(len \\ 12) when is_integer(len) and len > 0 do + (for _ <- 1..len, do: rand_uniform(32, 126)) + |> List.to_string() + end + + # Returns a random integer uniformly distributed in the range + # `n <= X <= m`. + # + # If the random variable `X` is uniformly distributed in the range + # `1 <= X <= m - n + 1`, then r.v `Y = X + n - 1` is uniformly + # distributed in the range `n <= Y <= m`. + # (Because we just shift X to the right). + defp rand_uniform(n, m) do + :rand.uniform(m - n + 1) # random X + |> Kernel.+(n - 1) # shift X to the right to get Y + end @doc """ Verifies if a slot should be handled as a link. diff --git a/lib/helpers/form_helpers.ex b/lib/helpers/form_helpers.ex index 4aaddef9..1c5fef82 100644 --- a/lib/helpers/form_helpers.ex +++ b/lib/helpers/form_helpers.ex @@ -140,7 +140,7 @@ defmodule PrimerLive.Helpers.FormHelpers do ...> }) %Ecto.Changeset{action: nil, changes: %{}, errors: [], data: nil, valid?: false} """ - def form_changeset(%Phoenix.HTML.Form{source: %Ecto.Changeset{}} = form) do + def form_changeset(%Phoenix.HTML.Form{source: _} = form) do form.source end diff --git a/lib/helpers/schema_helpers.ex b/lib/helpers/schema_helpers.ex index 3dc95a9d..c154d1c8 100644 --- a/lib/helpers/schema_helpers.ex +++ b/lib/helpers/schema_helpers.ex @@ -3,41 +3,6 @@ defmodule PrimerLive.Helpers.SchemaHelpers do use Phoenix.Component - import Ecto.Changeset - - @doc """ - Validates a changeset by verifying that at least one of the keys is present. - - ``` - changeset |> validate_one_of_present(attrs, [:toggle, :menu, :option, :divider]) - ``` - """ - - def validate_one_of_present(changeset, attrs, keys) do - attr_keys = Map.keys(attrs) - - in_list_count = - keys - |> Enum.reduce(0, fn key, acc -> - case Enum.member?(attr_keys, key) do - true -> acc + 1 - false -> acc - end - end) - - case in_list_count == 0 do - true -> - add_error( - changeset, - :dropdown_item, - "must contain one attribute from these options: #{keys |> Enum.join(", ")}" - ) - - false -> - changeset - end - end - @doc """ Validates attribute `form`. Allowed values: diff --git a/lib/theme/theme.ex b/lib/theme/theme.ex index 75fa277d..2cbab453 100644 --- a/lib/theme/theme.ex +++ b/lib/theme/theme.ex @@ -191,6 +191,16 @@ defmodule PrimerLive.Theme do |> Enum.map(&move_options_to_field/1) |> Enum.map(&add_selected_state(&1, theme)) |> Enum.map(&add_labels(&1, menu_labels)) + |> Enum.sort_by(fn {key, _item} -> + order = + case key do + :color_mode -> 0 + :light_theme -> 1 + :dark_theme -> 2 + end + + order + end) end defp move_options_to_field({key, options}) do diff --git a/mix.exs b/mix.exs index 77cab9bd..2f68cd15 100644 --- a/mix.exs +++ b/mix.exs @@ -11,10 +11,15 @@ defmodule PrimerLive.MixProject do aliases: aliases(), name: "PrimerLive", deps: deps(), - docs: docs() + docs: docs(), + elixirc_paths: elixirc_paths(Mix.env()) ] end + # Specifies which paths to compile per environment. + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_), do: ["lib"] + defp description() do "A collection of function components that implements GitHub's Primer Design System." end @@ -30,11 +35,11 @@ defmodule PrimerLive.MixProject do defp deps do [ {:credo, "~> 1.7", only: [:dev, :test], runtime: false}, - {:ecto, "~> 3.10", only: [:dev, :test], runtime: false}, - {:ecto_sql, "~> 3.10", only: [:dev, :test], runtime: false}, + {:ecto, "~> 3.10", only: :test, runtime: false}, + {:ecto_sql, "~> 3.10", only: :test, runtime: false}, {:esbuild, "~> 0.7", only: :dev}, {:ex_doc, "~> 0.30", only: :dev}, - {:phoenix_ecto, "~> 4.4", only: [:dev, :test], runtime: false}, + {:phoenix_ecto, "~> 4.4", only: :test, runtime: false}, {:phoenix_html, "~> 3.3"}, {:phoenix_live_view, "~> 0.19"}, {:jason, "~> 1.4"} diff --git a/lib/helpers/test_helpers.ex b/test/support/test_helpers.ex similarity index 100% rename from lib/helpers/test_helpers.ex rename to test/support/test_helpers.ex diff --git a/lib/test_helpers/todos/todo.ex b/test/support/todos/todo.ex similarity index 100% rename from lib/test_helpers/todos/todo.ex rename to test/support/todos/todo.ex diff --git a/lib/test_helpers/todos/todos.ex b/test/support/todos/todos.ex similarity index 100% rename from lib/test_helpers/todos/todos.ex rename to test/support/todos/todos.ex