Skip to content

Commit

Permalink
test: encode the struct in the template
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillegas98 committed Nov 13, 2024
1 parent ba35158 commit 647553e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule ConfiguratorWeb.MapConfigurationHTML do
embeds
|> Enum.map(&embed_to_string/1)
|> Enum.reject(&(&1 == nil))
|> json_encode
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end

def embed_to_string(nil), do: nil
Expand All @@ -33,10 +34,4 @@ defmodule ConfiguratorWeb.MapConfigurationHTML do
def embed_to_string(struct) when is_map(struct) do
struct
end

def json_encode(value) do
value
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
<% else %>
<.button type="button" phx-click={show_modal("square-wall-#{map_configuration.id}")}>Show</.button>
<.modal id={"square-wall-#{map_configuration.id}"}>
<p class="whitespace-pre"><%= json_encode(map_configuration.square_wall) %></p>
<p class="whitespace-pre">
<%= embed_to_string(map_configuration.square_wall) |> Jason.encode!() |> Jason.Formatter.pretty_print() %>
</p>
</.modal>
<% end %>
</:col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@
<.input
field={f[:version_id]}
type="select"
options={Enum.map(GameBackend.Configuration.list_versions(), fn version -> {version.name, version.id} end)}
options={
Enum.map(GameBackend.Configuration.list_versions(), fn version ->
{version.name, version.id}
end)
}
label="Version"
/> <.input field={f[:name]} type="text" label="Name" />
/>
<.input field={f[:name]} type="text" label="Name" />
<.input field={f[:radius]} type="number" label="Radius" step="any" />
<.input
field={f[:initial_positions]}
type="textarea"
label="Initial positions"
value={embed_to_string(f[:initial_positions].value)}
/>
<.input field={f[:square_wall]} type="textarea" label="Square Wall" value={json_encode(f[:square_wall].value)} />
<.input
field={f[:square_wall]}
type="textarea"
label="Square Wall"
value={embed_to_string(f[:square_wall].value) |> Jason.encode!() |> Jason.Formatter.pretty_print()}
/>
<.input field={f[:obstacles]} type="textarea" label="Obstacles" value={embed_to_string(f[:obstacles].value)} />
<.input field={f[:bushes]} type="textarea" label="Bushes" value={embed_to_string(f[:bushes].value)} />
<.input field={f[:pools]} type="textarea" label="Pools" value={embed_to_string(f[:pools].value)} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
<% else %>
<.button type="button" phx-click={show_modal("square-wall-#{@map_configuration.id}")}>Show</.button>
<.modal id={"square-wall-#{@map_configuration.id}"}>
<p class="whitespace-pre"><%= json_encode(@map_configuration.square_wall) %></p>
<p class="whitespace-pre">
<%= embed_to_string(@map_configuration.square_wall) |> Jason.encode!() |> Jason.Formatter.pretty_print() %>
</p>
</.modal>
<% end %>
</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule ConfiguratorWeb.MapConfigurationControllerTest do
bushes: "",
pools: "",
crates: "",
active: true
active: true,
square_wall: ""
}
@update_attrs %{
name: "another_map",
Expand All @@ -23,7 +24,8 @@ defmodule ConfiguratorWeb.MapConfigurationControllerTest do
bushes: "",
pools: "",
crates: "",
active: false
active: false,
square_wall: nil
}
@invalid_attrs %{
name: nil,
Expand All @@ -33,7 +35,8 @@ defmodule ConfiguratorWeb.MapConfigurationControllerTest do
bushes: nil,
pools: nil,
crates: nil,
active: nil
active: nil,
square_wall: ""
}

describe "index" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ defmodule GameBackend.CurseOfMirra.MapConfiguration do
def changeset(square_wall, attrs) do
square_wall
|> cast(attrs, [:right, :left, :top, :bottom])
|> validate_required([:right, :left, :top, :bottom])
end
end

Expand Down

0 comments on commit 647553e

Please sign in to comment.