From d05d272ebc8a86238b9cdf008150d15de0353724 Mon Sep 17 00:00:00 2001 From: Angelika Tyborska Date: Wed, 24 Jul 2024 09:35:59 +0200 Subject: [PATCH] Chain class renames --- lib/bitstyles_phoenix/bitstyles.ex | 58 +++++++++++++++-------- test/bitstyles_phoenix/bitstyles_test.exs | 2 + 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lib/bitstyles_phoenix/bitstyles.ex b/lib/bitstyles_phoenix/bitstyles.ex index 78d9177..1759efa 100644 --- a/lib/bitstyles_phoenix/bitstyles.ex +++ b/lib/bitstyles_phoenix/bitstyles.ex @@ -4,6 +4,9 @@ defmodule BitstylesPhoenix.Bitstyles do @default_version "5.0.1" @cdn_url "https://cdn.jsdelivr.net/npm/bitstyles" + defguard should_downgrade_from(version, target_version, current_version) + when target_version < version and current_version >= version + def cdn_url do "#{@cdn_url}@#{version(:string)}" end @@ -14,9 +17,8 @@ defmodule BitstylesPhoenix.Bitstyles do """ def classname(name), do: classname(name, version(:tuple)) - def classname(class, version) when is_tuple(version) and version > {5, 0, 1} do - IO.warn("Version #{version_to_string(version)} of bitstyles is not yet supported") - class + def classname(class, version) when is_tuple(version) do + downgrade_classname(class, version, version(:tuple)) end # Note about class renaming: @@ -25,11 +27,24 @@ defmodule BitstylesPhoenix.Bitstyles do # If it does exist, then doing this renaming in the classname/2 function would make it impossible # for users of older bitstyles to use the "class-name-A" classname. - def classname(class, version) when is_tuple(version) and version >= {5, 0, 0} do + defp downgrade_classname(class, target_version, _current_version) + when target_version > {5, 0, 1} do + IO.warn("Version #{version_to_string(target_version)} of bitstyles is not yet supported") + class + end + + defp downgrade_classname(class, target_version, _current_version) + when target_version >= {5, 0, 0} do class end - def classname(class, version) when is_tuple(version) and version >= {4, 2, 0} do + defp downgrade_classname(class, target_version, current_version) + when target_version >= current_version do + class + end + + defp downgrade_classname(class, target_version, current_version) + when should_downgrade_from({5, 0, 0}, target_version, current_version) do sizes_renaming = %{ "3xs" => "xxxs", "2xs" => "xxs", @@ -56,7 +71,6 @@ defmodule BitstylesPhoenix.Bitstyles do String.replace(acc, new_border_color, old_border_color) end) - # class from version 5 getting renamed to work for < 5 class = case class do "u-list-none" -> "a-list-reset" @@ -68,20 +82,22 @@ defmodule BitstylesPhoenix.Bitstyles do class -> class end - classname(class, {5, 0, 0}) + downgrade_classname(class, target_version, {4, 2, 0}) end - def classname(class, version) when is_tuple(version) and version >= {4, 0, 0} do + defp downgrade_classname(class, target_version, current_version) + when should_downgrade_from({4, 2, 0}, target_version, current_version) do mapping = case class do "u-border-radius-" <> variant -> "u-round-#{variant}" _ -> class end - classname(mapping, {4, 3, 0}) + downgrade_classname(mapping, target_version, {4, 0, 0}) end - def classname(class, version) when is_tuple(version) and version >= {2, 0, 0} do + defp downgrade_classname(class, target_version, current_version) + when should_downgrade_from({4, 0, 0}, target_version, current_version) do # credo:disable-for-previous-line Credo.Check.Refactor.CyclomaticComplexity mapping = @@ -103,42 +119,46 @@ defmodule BitstylesPhoenix.Bitstyles do "u-border-radius-" <> variant -> "u-round--#{variant}" "u-overflow-x-auto" -> "u-overflow--x" "u-overflow-y-auto" -> "u-overflow--y" - "u-version-3" -> "u-version-1-5" + "u-version-4" -> "u-version-2" _ -> class end - classname(mapping, {4, 0, 0}) + downgrade_classname(mapping, target_version, {2, 0, 0}) end - def classname(class, version) when is_tuple(version) and version >= {1, 5, 0} do + defp downgrade_classname(class, target_version, current_version) + when should_downgrade_from({2, 0, 0}, target_version, current_version) do mapping = case class do "u-flex-shrink-" <> number -> "u-flex__shrink-#{number}" "u-flex-grow-" <> number -> "u-flex__grow-#{number}" "u-flex-wrap" -> "u-flex--wrap" "u-flex-col" -> "u-flex--col" - "u-version-1-5" -> "u-version-1-3" + "u-version-2" -> "u-version-1-5" _ -> class end - classname(mapping, {2, 0, 0}) + downgrade_classname(mapping, target_version, {1, 5, 0}) end - def classname(class, version) when is_tuple(version) and version >= {1, 3, 0} do + defp downgrade_classname(class, target_version, current_version) + when should_downgrade_from({1, 5, 0}, target_version, current_version) do mapping = case class do "u-grid-cols-" <> number -> "u-grid--#{number}-col" "u-col-span-" <> number -> "u-grid__col-span-#{number}" "u-col-start-" <> number -> "u-grid__col-#{number}" + "u-version-1-5" -> "u-version-1-3" _ -> class end - classname(mapping, {1, 5, 0}) + downgrade_classname(mapping, target_version, {1, 3, 0}) end - def classname(_class, version) when is_tuple(version) do + defp downgrade_classname(_class, target_version, current_version) + when should_downgrade_from({1, 3, 0}, target_version, current_version) do raise(""" - The version #{version_to_string(version)} of bitstyles is not supported. The helpers will produce incorrect classes. + The version #{version_to_string(target_version)} of bitstyles is not supported. The helpers will produce incorrect classes. Please upgrade bitsyles and set the `bitsyles_version` to the updated version. """) end diff --git a/test/bitstyles_phoenix/bitstyles_test.exs b/test/bitstyles_phoenix/bitstyles_test.exs index 2d86e01..08512a6 100644 --- a/test/bitstyles_phoenix/bitstyles_test.exs +++ b/test/bitstyles_phoenix/bitstyles_test.exs @@ -11,7 +11,9 @@ defmodule BitstylesPhoenix.BitstylesTest do ) end + # TODO: delete test "chained version" do + assert classname("u-version-5-0-0", {5, 0, 1}) == "u-version-5-0-0" assert classname("u-version-5-0-0", {5, 0, 0}) == "u-version-5-0-0" assert classname("u-version-5-0-0", {4, 9, 0}) == "u-version-4" assert classname("u-version-5-0-0", {4, 4, 0}) == "u-version-4"