8000 refactor: codebase cleanup (indexes, helper extraction, misc) by mwoods79 · Pull Request #112 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/huddlz/communities/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Huddlz.Communities.Group do
repo Huddlz.Repo

custom_indexes do
index [:owner_id]

index "ST_MakePoint(longitude, latitude)",
name: "groups_location_gist_index",
using: "GIST",
Expand All @@ -34,7 +36,7 @@ defmodule Huddlz.Communities.Group do
change Huddlz.Communities.Group.Changes.AddOwnerAsMember
change Huddlz.Communities.Group.Changes.GenerateSlug
change Huddlz.Geocoding.ApplyProvidedCoordinates
change Huddlz.Communities.Group.Changes.GeocodeLocation
change {Huddlz.Geocoding.GeocodeChange, field: :location}
end

read :search do
Expand Down Expand Up @@ -90,7 +92,7 @@ defmodule Huddlz.Communities.Group do
argument :provided_longitude, :float, allow_nil?: true, public?: false

change Huddlz.Geocoding.ApplyProvidedCoordinates
change Huddlz.Communities.Group.Changes.GeocodeLocation
change {Huddlz.Geocoding.GeocodeChange, field: :location}
end
end

Expand Down
11 changes: 0 additions & 11 deletions lib/huddlz/communities/group/changes/geocode_location.ex

This file was deleted.

4 changes: 4 additions & 0 deletions lib/huddlz/communities/group_member.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ defmodule Huddlz.Communities.GroupMember do
postgres do
table "group_members"
repo Huddlz.Repo

custom_indexes do
index [:user_id]
end
end

actions do
Expand Down
4 changes: 2 additions & 2 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule Huddlz.Communities.Huddl 8000 do
change Huddlz.Communities.Huddl.Changes.ForcePrivateForPrivateGroups
change Huddlz.Communities.Huddl.Changes.AddHuddlTemplate
change Huddlz.Geocoding.ApplyProvidedCoordinates
change Huddlz.Communities.Huddl.Changes.GeocodeLocation
change {Huddlz.Geocoding.GeocodeChange, field: :physical_location}
change Huddlz.Communities.Huddl.Changes.DefaultLocationFromGroup
end

Expand Down Expand Up @@ -142,7 +142,7 @@ defmodule Huddlz.Communities.Huddl do
change Huddlz.Communities.Huddl.Changes.CalculateDateTimeFromInputs
change Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz
change Huddlz.Geocoding.ApplyProvidedCoordinates
change Huddlz.Communities.Huddl.Changes.GeocodeLocation
change {Huddlz.Geocoding.GeocodeChange, field: :physical_location}
change Huddlz.Communities.Huddl.Changes.DefaultLocationFromGroup
end

Expand Down
11 changes: 0 additions & 11 deletions lib/huddlz/communities/huddl/changes/geocode_location.ex

This file was deleted.

4 changes: 4 additions & 0 deletions lib/huddlz/communities/huddl_attendee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Huddlz.Communities.HuddlAttendee do
postgres do
table "huddl_attendees"
repo Huddlz.Repo

custom_indexes do
index [:user_id]
end
end

actions do
Expand Down
15 changes: 8 additions & 7 deletions lib/huddlz/geocoding/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ defmodule Huddlz.Geocoding.Change do
require Logger

def geocode_if_changed(changeset, location_attribute) do
if provided_coordinates?(changeset) do
changeset
else
if Ash.Changeset.changing_attribute?(changeset, location_attribute) do
cond do
provided_coordinates?(changeset) ->
changeset

not Ash.Changeset.changing_attribute?(changeset, location_attribute) ->
changeset

true ->
changeset
|> Ash.Changeset.get_attribute(location_attribute)
|> geocode_and_apply(changeset)
else
changeset
end
end
end

Expand Down
17 changes: 17 additions & 0 deletions lib/huddlz/geocoding/geocode_change.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Huddlz.Geocoding.GeocodeChange do
@moduledoc """
Parameterized Ash change that geocodes a location attribute to
latitude/longitude on create/update.

Pass the attribute to geocode via the `:field` option:

change {Huddlz.Geocoding.GeocodeChange, field: :location}
"""
use Ash.Resource.Change

@impl true
def change(changeset, opts, _context) do
field = Keyword.fetch!(opts, :field)
Huddlz.Geocoding.Change.geocode_if_changed(changeset, field)
end
end
8 changes: 4 additions & 4 deletions lib/huddlz_web/components/community_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule HuddlzWeb.CommunityComponents do
use Phoenix.Component
use HuddlzWeb, :verified_routes

import HuddlzWeb.CoreComponents, only: [avatar: 1, icon: 1]
import HuddlzWeb.CoreComponents, only: [avatar: 1, humanize: 1, icon: 1]

alias Huddlz.Storage.GroupImages
alias Huddlz.Storage.HuddlImages
Expand Down Expand Up @@ -169,7 +169,7 @@ defmodule HuddlzWeb.CommunityComponents do
<div class="flex flex-wrap gap-x-3 gap-y-1 mt-3 text-xs text-base-content/50">
<span class="flex items-center gap-1">
<.icon name={type_icon(@huddl.event_type)} class="h-3.5 w-3.5" />
{@huddl.event_type |> to_string() |> String.replace("_", " ") |> String.capitalize()}
{humanize(@huddl.event_type)}
</span>
<span class="flex items-center gap-1">
<.icon name="hero-calendar" class="h-3.5 w-3.5" />
Expand Down Expand Up @@ -212,7 +212,7 @@ defmodule HuddlzWeb.CommunityComponents do
status_badge_class(@status),
@class
]}>
{@status |> to_string() |> String.replace("_", " ") |> String.capitalize()}
{humanize(@status)}
</span>
"""
end
Expand All @@ -231,7 +231,7 @@ defmodule HuddlzWeb.CommunityComponents do
@class
]}>
<.icon name={type_icon(@type)} class="h-3 w-3" />
{@type |> to_string() |> String.replace("_", " ") |> String.capitalize()}
{humanize(@type)}
</span>
"""
end
Expand Down
13 changes: 13 additions & 0 deletions lib/huddlz_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,19 @@ defmodule HuddlzWeb.CoreComponents do
|> JS.pop_focus()
end

@doc """
Renders an atom or string as a human-readable label.

Underscores become spaces and the first letter is capitalized.
Use for enum values like `:in_person` → `"In person"`.
"""
def humanize(value) do
value
|> to_string()
|> String.replace("_", " ")
|> String.capitalize()
end

@doc """
Translates an error message using gettext.
"""
Expand Down
103 changes: 25 additions & 78 deletions lib/huddlz_web/live/group_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule HuddlzWeb.GroupLive.Edit do
alias Huddlz.Communities.GroupImage
alias Huddlz.Storage.GroupImages
alias HuddlzWeb.Layouts
alias HuddlzWeb.Live.Helpers.ImageUploadPipeline
alias HuddlzWeb.Live.Helpers.ModalLocationHelpers

on_mount {HuddlzWeb.LiveUserAuth, :live_user_required}

Expand Down Expand Up @@ -60,9 +62,7 @@ defmodule HuddlzWeb.GroupLive.Edit do
|> assign(:pending_image_id, nil)
|> assign(:pending_preview_url, nil)
|> assign(:selected_location_data, build_initial_location_data(group))
|> assign(:modal_location_address, nil)
|> assign(:modal_location_lat, nil)
|> assign(:modal_location_lng, nil)
|> ModalLocationHelpers.init()
|> assign(:upload_processing, false)
|> allow_upload(:group_image,
accept: ~w(.jpg .jpeg .png .webp),
Expand All @@ -77,14 +77,8 @@ defmodule HuddlzWeb.GroupLive.Edit do
def handle_params(_params, _uri, socket) do
socket =
case socket.assigns.live_action do
:new_location ->
socket
|> assign(:modal_location_address, nil)
|> assign(:modal_location_lat, nil)
|> assign(:modal_location_lng, nil)

_ ->
socket
:new_location -> ModalLocationHelpers.clear(socket)
_ -> socket
end

{:noreply, socket}
Expand All @@ -98,30 +92,22 @@ defmodule HuddlzWeb.GroupLive.Edit do
end
end

defp process_eager_upload(socket) do
# Clean up previous pending image if user re-uploads
socket = cleanup_pending_image(socket)
socket = assign(socket, :upload_processing, true)
defp process_eager_upload(socket),
do: ImageUploadPipeline.process_eager_upload(socket, upload_config())

result =
consume_uploaded_entries(socket, :group_image, fn %{path: path}, entry ->
store_and_create_pending_image(path, entry, socket.assigns.current_user)
end)
defp cleanup_pending_image(socket),
do: ImageUploadPipeline.cleanup_pending_image(socket, upload_config())

socket = assign(socket, :upload_processing, false)
apply_upload_result(socket, result)
end

defp store_and_create_pending_image(path, entry, user) do
with {:ok, metadata} <- GroupImages.store_pending(path, entry.client_name, entry.client_type),
{:ok, image} <- create_pending_image_record(entry, metadata, user) do
{:ok, {:success, image.id, metadata.thumbnail_path}}
else
{:error, reason} -> {:ok, {:error, reason}}
end
defp upload_config do
%{
upload_name: :group_image,
storage: GroupImages,
create_pending: &create_pending_group_image/3,
cleanup: &soft_delete_pending_group_image/2
}
end

defp create_pending_image_record(entry, metadata, user) do
defp create_pending_group_image(socket, entry, metadata) do
Communities.create_pending_group_image(
%{
filename: entry.client_name,
Expand All @@ -130,39 +116,14 @@ defmodule HuddlzWeb.GroupLive.Edit do
storage_path: metadata.storage_path,
thumbnail_path: metadata.thumbnail_path
},
actor: user
actor: socket.assigns.current_user
)
end

defp apply_upload_result(socket, result) do
case result do
[{:success, image_id, thumbnail_path}] ->
socket
|> assign(:pending_image_id, image_id)
|> assign(:pending_preview_url, GroupImages.url(thumbnail_path))
|> assign(:image_error, nil)

[{:error, reason}] ->
assign(socket, :image_error, format_upload_error(reason))

[] ->
socket
end
end

defp cleanup_pending_image(socket) do
case socket.assigns[:pending_image_id] do
nil ->
socket

image_id ->
# Soft-delete previous pending image (will be cleaned up by Oban job)
with {:ok, image} <- Ash.get(GroupImage, image_id),
true <- is_nil(image.group_id) do
Communities.soft_delete_group_image(image, actor: socket.assigns.current_user)
end

assign(socket, pending_image_id: nil, pending_preview_url: nil)
defp soft_delete_pending_group_image(socket, image_id) do
with {:ok, image} <- Ash.get(GroupImage, image_id),
true <- is_nil(image.group_id) do
Communities.soft_delete_group_image(image, actor: socket.assigns.current_user)
end
end

Expand Down Expand Up @@ -541,27 +502,13 @@ defmodule HuddlzWeb.GroupLive.Edit do
end

@impl true
def handle_info(
{:location_selected, "modal-location-autocomplete",
%{display_text: text, latitude: lat, longitude: lng}},
socket
) do
{:noreply,
assign(socket,
modal_location_address: text,
modal_location_lat: lat,
modal_location_lng: lng
)}
def handle_info({:location_selected, "modal-location-autocomplete", payload}, socket) do
{:noreply, ModalLocationHelpers.apply_selected(socket, payload)}
end

@impl true
def handle_info({:location_cleared, "modal-location-autocomplete"}, socket) do
{:noreply,
assign(socket,
modal_location_address: nil,
modal_location_lat: nil,
modal_location_lng: nil
)}
{:noreply, ModalLocationHelpers.clear(socket)}
end

defp assign_pending_image_to_group(socket, group) do
Expand Down
Loading
Loading
0