Handle address is invalid while rendering event preview
Closes #996 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
a99d66b68f
commit
2bdce8b2fc
|
@ -7,7 +7,7 @@ defmodule Mobilizon.Service.Address do
|
||||||
|
|
||||||
@type address :: %{name: String.t(), alternative_name: String.t()}
|
@type address :: %{name: String.t(), alternative_name: String.t()}
|
||||||
|
|
||||||
@spec render_address(AddressModel.t()) :: String.t() | no_return
|
@spec render_address(AddressModel.t()) :: String.t() | nil
|
||||||
def render_address(%AddressModel{} = address) do
|
def render_address(%AddressModel{} = address) do
|
||||||
%{name: name, alternative_name: alternative_name} = render_names(address)
|
%{name: name, alternative_name: alternative_name} = render_names(address)
|
||||||
|
|
||||||
|
@ -22,7 +22,18 @@ defmodule Mobilizon.Service.Address do
|
||||||
alternative_name
|
alternative_name
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec render_address!(AddressModel.t()) :: String.t() | no_return
|
||||||
|
def render_address!(%AddressModel{} = address) do
|
||||||
|
case render_address(address) do
|
||||||
|
nil ->
|
||||||
raise ArgumentError, message: "Invalid address"
|
raise ArgumentError, message: "Invalid address"
|
||||||
|
|
||||||
|
address ->
|
||||||
|
address
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||||
|
|
||||||
import Mobilizon.Service.Metadata.Utils,
|
import Mobilizon.Service.Metadata.Utils,
|
||||||
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address: 1]
|
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address!: 1]
|
||||||
|
|
||||||
def build_tags(%Event{} = event, locale \\ "en") do
|
def build_tags(%Event{} = event, locale \\ "en") do
|
||||||
formatted_description = description(event, locale)
|
formatted_description = description(event, locale)
|
||||||
|
@ -150,7 +150,13 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||||
|
|
||||||
@spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t())
|
@spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t())
|
||||||
defp maybe_build_address(elements, %Address{} = address) do
|
defp maybe_build_address(elements, %Address{} = address) do
|
||||||
elements ++ [render_address(address)]
|
elements ++ [render_address!(address)]
|
||||||
|
rescue
|
||||||
|
# If the address is not renderable
|
||||||
|
e in ArgumentError ->
|
||||||
|
require Logger
|
||||||
|
Logger.error(Exception.format(:error, e, __STACKTRACE__))
|
||||||
|
elements
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_build_address(elements, _address), do: elements
|
defp maybe_build_address(elements, _address), do: elements
|
||||||
|
|
|
@ -54,7 +54,7 @@ defmodule Mobilizon.Service.Metadata.Utils do
|
||||||
end
|
end
|
||||||
|
|
||||||
defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime
|
defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime
|
||||||
defdelegate render_address(address), to: Address
|
defdelegate render_address!(address), to: Address
|
||||||
|
|
||||||
defp maybe_slice(description, nil), do: description
|
defp maybe_slice(description, nil), do: description
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<!-- row for datetime end -->
|
<!-- row for datetime end -->
|
||||||
<%= if @event.physical_address do %>
|
<%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %>
|
||||||
<!-- venue block -->
|
<!-- venue block -->
|
||||||
<tr style="vertical-align: baseline; margin: 0; padding: 0; border: 0;" align="left">
|
<tr style="vertical-align: baseline; margin: 0; padding: 0; border: 0;" align="left">
|
||||||
<td style="word-wrap: break-word; border-collapse: collapse; color: rgb(10,10,10); font-family: Helvetica,Arial,sans-serif; font-weight: normal; line-height: 1.3; font-size: 16px; margin: 0; padding: 0 12px 0 0; border: 0;" valign="baseline" align="left">
|
<td style="word-wrap: break-word; border-collapse: collapse; color: rgb(10,10,10); font-family: Helvetica,Arial,sans-serif; font-weight: normal; line-height: 1.3; font-size: 16px; margin: 0; padding: 0 12px 0 0; border: 0;" valign="baseline" align="left">
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
<%= gettext("Date:") %> <%= render("date/event_tz_date_range.text", event: @event, start_date: datetime_tz_convert(@event.begins_on, @event.options.timezone), end_date: datetime_tz_convert(@event.ends_on, @event.options.timezone), timezone: @timezone, locale: @locale) %>
|
<%= gettext("Date:") %> <%= render("date/event_tz_date_range.text", event: @event, start_date: datetime_tz_convert(@event.begins_on, @event.options.timezone), end_date: datetime_tz_convert(@event.ends_on, @event.options.timezone), timezone: @timezone, locale: @locale) %>
|
||||||
<%= gettext("Address:") %> <%= if @event.physical_address do %><%= render_address(@event.physical_address) %><% end %><%= if @event.options.is_online do %><%= gettext "Online event" %><% end %>
|
<%= gettext("Address:") %> <%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %><%= render_address(@event.physical_address) %><% end %><%= if @event.options.is_online do %><%= gettext "Online event" %><% end %>
|
||||||
|
|
|
@ -125,7 +125,7 @@ th {
|
||||||
<dt><%= gettext("Ends on") %></dt>
|
<dt><%= gettext("Ends on") %></dt>
|
||||||
<dd><%= datetime_to_string(@event.ends_on, @locale, :long) %></dd>
|
<dd><%= datetime_to_string(@event.ends_on, @locale, :long) %></dd>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= if @event.physical_address do %>
|
<%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %>
|
||||||
<dt><%= gettext("Location") %></dt>
|
<dt><%= gettext("Location") %></dt>
|
||||||
<dd><%= render_address(@event.physical_address) %></dd>
|
<dd><%= render_address(@event.physical_address) %></dd>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -50,8 +50,10 @@ defmodule Mobilizon.Service.AddressTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with no data" do
|
test "with no data" do
|
||||||
|
assert AddressRenderer.render_address(%Address{}) == nil
|
||||||
|
|
||||||
assert_raise ArgumentError, "Invalid address", fn ->
|
assert_raise ArgumentError, "Invalid address", fn ->
|
||||||
AddressRenderer.render_address(%Address{})
|
AddressRenderer.render_address!(%Address{})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue