berrypod/lib/berrypod/site/nav_item.ex
jamey 7c07805df8
All checks were successful
deploy / deploy (push) Successful in 3m27s
add nav editors to Site tab with live preview
- Add header and footer nav editors to Site tab with drag-to-reorder,
  add/remove items, and destination picker (pages, collections, external)
- Live preview updates as you edit nav items
- Remove legacy /admin/navigation page and controller (was saving to
  Settings table, now uses nav_items table)
- Update error_html.ex and pages/editor.ex to load nav from nav_items table
- Update link_scanner to read from nav_items table, edit path now /?edit=site
- Add Site.default_header_nav/0 and default_footer_nav/0 for previews/errors
- Remove fallback logic from theme_hook.ex (database is now source of truth)
- Seed default nav items and social links during setup

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-28 22:19:48 +00:00

43 lines
1.0 KiB
Elixir

defmodule Berrypod.Site.NavItem do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@locations ~w(header footer)
schema "nav_items" do
field :location, :string
field :label, :string
field :url, :string
field :position, :integer, default: 0
belongs_to :page, Berrypod.Pages.Page
timestamps(type: :utc_datetime)
end
def locations, do: @locations
@doc false
def changeset(nav_item, attrs) do
nav_item
|> cast(attrs, [:location, :label, :url, :page_id, :position])
|> validate_required([:location, :label])
|> validate_inclusion(:location, @locations)
|> validate_length(:label, min: 1, max: 50)
|> foreign_key_constraint(:page_id)
|> default_url()
end
# Default to empty string if URL not provided (allows pending page selection)
defp default_url(changeset) do
if get_field(changeset, :url) == nil do
put_change(changeset, :url, "")
else
changeset
end
end
end