Skip to content

Commit

Permalink
Clean up global settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Feb 28, 2024
1 parent 7ad0ab5 commit c94a69e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/polar/globals.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ defmodule Polar.Globals do
module = Map.fetch!(@mappings, key)

case module.parse(value) do
{:ok, params} ->
{:ok, params} = result ->
value =
Map.from_struct(params)
|> :erlang.term_to_binary()

Repo.insert(%Setting{key: key, value: value})
Repo.insert!(%Setting{key: key, value: value})

result

error ->
error
Expand All @@ -53,14 +55,16 @@ defmodule Polar.Globals do
module = Map.fetch!(@mappings, setting.key)

case module.parse(value) do
{:ok, params} ->
{:ok, params} = result ->
value =
Map.from_struct(params)
|> :erlang.term_to_binary()

setting
|> Setting.changeset(%{value: value})
|> Repo.update()
|> Repo.update!()

result

error ->
error
Expand Down
5 changes: 2 additions & 3 deletions test/polar/globals_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ defmodule Polar.GlobalsTest do
test "can create basic setting" do
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 3})

assert setting.key == "basic"
assert %{versions_per_product: 3} = :erlang.binary_to_term(setting.value)
assert %Basic{versions_per_product: 3} = setting
end
end

Expand All @@ -34,7 +33,7 @@ defmodule Polar.GlobalsTest do
test "can change basic setting" do
{:ok, setting} = Polar.Globals.save("basic", %{versions_per_product: 2})

assert %{versions_per_product: 2} = :erlang.binary_to_term(setting.value)
assert %Basic{versions_per_product: 2} = setting
end
end
end

0 comments on commit c94a69e

Please sign in to comment.