add home channel setting, first pass of public routesfix identity tests
This commit is contained in:
parent
d374880152
commit
c969e6e90f
21 changed files with 118 additions and 52 deletions
|
@ -8,7 +8,9 @@ defmodule Chiya.Channels.Channel do
|
||||||
field :slug, :string
|
field :slug, :string
|
||||||
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
|
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
|
||||||
|
|
||||||
many_to_many :notes, Chiya.Notes.Note, join_through: "channels_notes"
|
many_to_many :notes, Chiya.Notes.Note,
|
||||||
|
join_through: "channels_notes",
|
||||||
|
join_keys: [note: :id, channel: :id]
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -65,6 +65,9 @@ defmodule Chiya.Notes do
|
||||||
"""
|
"""
|
||||||
def get_note_preloaded!(id), do: Repo.get!(Note, id) |> preload_note()
|
def get_note_preloaded!(id), do: Repo.get!(Note, id) |> preload_note()
|
||||||
|
|
||||||
|
def get_note_by_slug_preloaded!(slug), do: Repo.get_by!(Note, slug: slug) |> preload_note()
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a note.
|
Creates a note.
|
||||||
|
|
||||||
|
|
|
@ -9,17 +9,27 @@ defmodule Chiya.Site.Setting do
|
||||||
field :custom_css, :string, default: ""
|
field :custom_css, :string, default: ""
|
||||||
field :custom_html, :string, default: ""
|
field :custom_html, :string, default: ""
|
||||||
|
|
||||||
field :theme, Ecto.Enum, values: [:default]
|
field :theme, Ecto.Enum, values: [:default], default: :default
|
||||||
|
|
||||||
field :user_agent, :string, default: "Chiya/0.x +https://inhji.de"
|
field :user_agent, :string, default: "Chiya/0.x +https://inhji.de"
|
||||||
|
|
||||||
|
belongs_to :home_channel, Chiya.Channels.Channel
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def changeset(setting, attrs) do
|
def changeset(setting, attrs) do
|
||||||
setting
|
setting
|
||||||
|> cast(attrs, [:title, :subtitle, :theme, :user_agent, :custom_css, :custom_html])
|
|> cast(attrs, [
|
||||||
|
:title,
|
||||||
|
:subtitle,
|
||||||
|
:theme,
|
||||||
|
:user_agent,
|
||||||
|
:custom_css,
|
||||||
|
:custom_html,
|
||||||
|
:home_channel_id
|
||||||
|
])
|
||||||
|> validate_required([:title, :subtitle, :theme, :user_agent])
|
|> validate_required([:title, :subtitle, :theme, :user_agent])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,11 +4,26 @@ defmodule ChiyaWeb.PageController do
|
||||||
def home(conn, _params) do
|
def home(conn, _params) do
|
||||||
# The home page is often custom made,
|
# The home page is often custom made,
|
||||||
# so skip the default app layout.
|
# so skip the default app layout.
|
||||||
render(conn, :home, layout: {ChiyaWeb.Layouts, "public.html"})
|
settings = conn.assigns.settings
|
||||||
|
|
||||||
|
channel =
|
||||||
|
if settings.home_channel_id != nil do
|
||||||
|
IO.inspect(settings)
|
||||||
|
Chiya.Channels.get_channel_preloaded!(settings.home_channel_id)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
render(conn, :home, layout: {ChiyaWeb.Layouts, "public.html"}, channel: channel)
|
||||||
end
|
end
|
||||||
|
|
||||||
def channel(conn, %{"slug" => channel_slug}) do
|
def channel(conn, %{"slug" => channel_slug}) do
|
||||||
channel = Chiya.Channels.get_channel_by_slug_preloaded!(channel_slug)
|
channel = Chiya.Channels.get_channel_by_slug_preloaded!(channel_slug)
|
||||||
render(conn, :channel, layout: {ChiyaWeb.Layouts, "public.html"}, channel: channel)
|
render(conn, :channel, layout: {ChiyaWeb.Layouts, "public.html"}, channel: channel)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def note(conn, %{"slug" => note_slug}) do
|
||||||
|
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug)
|
||||||
|
render(conn, :note, layout: {ChiyaWeb.Layouts, "public.html"}, note: note)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,17 @@
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<%= if @channel do %>
|
||||||
|
<div class="w-full mt-6 sm:w-auto">
|
||||||
|
<%= for note <- @channel.notes do %>
|
||||||
|
<a href={~p"/n/#{note.slug}"}>
|
||||||
|
<.header>
|
||||||
|
<%= note.name %>
|
||||||
|
<:subtitle><%= note.published_at %></:subtitle>
|
||||||
|
</.header>
|
||||||
|
</a>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
3
lib/chiya_web/controllers/page_html/note.html.heex
Normal file
3
lib/chiya_web/controllers/page_html/note.html.heex
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<.header>
|
||||||
|
<%= @note.name %>
|
||||||
|
</.header>
|
|
@ -6,7 +6,7 @@ defmodule ChiyaWeb.SettingController do
|
||||||
|
|
||||||
def new(conn, _params) do
|
def new(conn, _params) do
|
||||||
changeset = Site.change_setting(%Setting{})
|
changeset = Site.change_setting(%Setting{})
|
||||||
render(conn, :new, changeset: changeset)
|
render(conn, :new, changeset: changeset, channels: channels())
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(conn, %{"setting" => setting_params}) do
|
def create(conn, %{"setting" => setting_params}) do
|
||||||
|
@ -17,7 +17,7 @@ defmodule ChiyaWeb.SettingController do
|
||||||
|> redirect(to: ~p"/admin/settings")
|
|> redirect(to: ~p"/admin/settings")
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
render(conn, :new, changeset: changeset)
|
render(conn, :new, changeset: changeset, channels: channels())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ defmodule ChiyaWeb.SettingController do
|
||||||
def edit(conn, _params) do
|
def edit(conn, _params) do
|
||||||
setting = Site.get_settings()
|
setting = Site.get_settings()
|
||||||
changeset = Site.change_setting(setting)
|
changeset = Site.change_setting(setting)
|
||||||
render(conn, :edit, setting: setting, changeset: changeset)
|
render(conn, :edit, setting: setting, changeset: changeset, channels: channels())
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(conn, %{"setting" => setting_params}) do
|
def update(conn, %{"setting" => setting_params}) do
|
||||||
|
@ -42,7 +42,9 @@ defmodule ChiyaWeb.SettingController do
|
||||||
|> redirect(to: ~p"/admin/settings")
|
|> redirect(to: ~p"/admin/settings")
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
render(conn, :edit, setting: setting, changeset: changeset)
|
render(conn, :edit, setting: setting, changeset: changeset, channels: channels())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp channels(), do: Chiya.Channels.list_channels() |> Enum.map(fn c -> {c.name, c.id} end)
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
<:subtitle>Use this form to manage setting records in your database.</:subtitle>
|
<:subtitle>Use this form to manage setting records in your database.</:subtitle>
|
||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
<.setting_form changeset={@changeset} action={~p"/admin/settings"} />
|
<.setting_form changeset={@changeset} action={~p"/admin/settings"} channels={@channels} />
|
||||||
|
|
||||||
<.back navigate={~p"/admin/settings"}>Back to settings</.back>
|
<.back navigate={~p"/admin/settings"}>Back to settings</.back>
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
<:subtitle>Use this form to manage setting records in your database.</:subtitle>
|
<:subtitle>Use this form to manage setting records in your database.</:subtitle>
|
||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
<.setting_form changeset={@changeset} action={~p"/admin/settings"} />
|
<.setting_form changeset={@changeset} action={~p"/admin/settings"} channels={@channels} />
|
||||||
|
|
||||||
<.back navigate={~p"/admin/settings"}>Back to settings</.back>
|
<.back navigate={~p"/admin/settings"}>Back to settings</.back>
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
<.input field={f[:user_agent]} type="text" label="User agent" />
|
<.input field={f[:user_agent]} type="text" label="User agent" />
|
||||||
<.input field={f[:custom_css]} type="textarea" label="Custom css" />
|
<.input field={f[:custom_css]} type="textarea" label="Custom css" />
|
||||||
<.input field={f[:custom_html]} type="textarea" label="Custom html" />
|
<.input field={f[:custom_html]} type="textarea" label="Custom html" />
|
||||||
|
<.input field={f[:home_channel_id]} type="select" label="Home Channel" options={@channels} />
|
||||||
<:actions>
|
<:actions>
|
||||||
<.button>Save Setting</.button>
|
<.button>Save Setting</.button>
|
||||||
</:actions>
|
</:actions>
|
||||||
|
|
|
@ -104,6 +104,7 @@ defmodule ChiyaWeb.Router do
|
||||||
scope "/", ChiyaWeb do
|
scope "/", ChiyaWeb do
|
||||||
pipe_through [:browser, :public]
|
pipe_through [:browser, :public]
|
||||||
|
|
||||||
|
get "/n/:slug", PageController, :note
|
||||||
get "/:slug", PageController, :channel
|
get "/:slug", PageController, :channel
|
||||||
get "/", PageController, :home
|
get "/", PageController, :home
|
||||||
end
|
end
|
||||||
|
|
15
priv/repo/migrations/20230310202848_add_default_settings.exs
Normal file
15
priv/repo/migrations/20230310202848_add_default_settings.exs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
defmodule Chiya.Repo.Migrations.AddDefaultSettings do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
alias Chiya.Site.Setting
|
||||||
|
|
||||||
|
def up do
|
||||||
|
if not repo().exists?(Setting) do
|
||||||
|
repo().insert(%Setting{})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down do
|
||||||
|
repo().delete_all(Setting)
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,9 @@
|
||||||
|
defmodule Chiya.Repo.Migrations.AddHomeChannelSetting do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table(:settings) do
|
||||||
|
add :home_channel_id, references(:channels, on_delete: :nothing)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -17,9 +17,11 @@ defmodule Chiya.SiteTest do
|
||||||
user_agent: nil
|
user_agent: nil
|
||||||
}
|
}
|
||||||
|
|
||||||
test "get_setting!/1 returns the setting with given id" do
|
test "get_setting!/1 returns the setting" do
|
||||||
setting = setting_fixture()
|
# setting = setting_fixture()
|
||||||
assert Site.get_settings() == setting
|
setting = Site.get_settings()
|
||||||
|
assert setting.title == "Title"
|
||||||
|
assert setting.subtitle == "Subtitle"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_setting/1 with valid data creates a setting" do
|
test "create_setting/1 with valid data creates a setting" do
|
||||||
|
@ -46,7 +48,7 @@ defmodule Chiya.SiteTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update_setting/2 with valid data updates the setting" do
|
test "update_setting/2 with valid data updates the setting" do
|
||||||
setting = setting_fixture()
|
setting = Site.get_settings()
|
||||||
|
|
||||||
update_attrs = %{
|
update_attrs = %{
|
||||||
custom_css: "some updated custom_css",
|
custom_css: "some updated custom_css",
|
||||||
|
@ -67,7 +69,7 @@ defmodule Chiya.SiteTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update_setting/2 with invalid data returns error changeset" do
|
test "update_setting/2 with invalid data returns error changeset" do
|
||||||
setting = setting_fixture()
|
setting = Site.get_settings()
|
||||||
assert {:error, %Ecto.Changeset{}} = Site.update_setting(setting, @invalid_attrs)
|
assert {:error, %Ecto.Changeset{}} = Site.update_setting(setting, @invalid_attrs)
|
||||||
assert setting == Site.get_settings()
|
assert setting == Site.get_settings()
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,8 @@ defmodule ChiyaWeb.IdentityControllerTest do
|
||||||
}
|
}
|
||||||
@invalid_attrs %{active: nil, name: nil, public: nil, rel: nil, url: nil}
|
@invalid_attrs %{active: nil, name: nil, public: nil, rel: nil, url: nil}
|
||||||
|
|
||||||
|
setup [:register_and_log_in_user]
|
||||||
|
|
||||||
describe "index" do
|
describe "index" do
|
||||||
test "lists all identities", %{conn: conn} do
|
test "lists all identities", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/admin/identities")
|
conn = get(conn, ~p"/admin/identities")
|
||||||
|
|
|
@ -3,6 +3,7 @@ defmodule ChiyaWeb.PageControllerTest do
|
||||||
|
|
||||||
test "GET /", %{conn: conn} do
|
test "GET /", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/")
|
conn = get(conn, ~p"/")
|
||||||
assert html_response(conn, 200) =~ "Peace of mind from prototype to production"
|
assert html_response(conn, 200) =~ "Title"
|
||||||
|
assert html_response(conn, 200) =~ "Subtitle"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
defmodule ChiyaWeb.SettingControllerTest do
|
defmodule ChiyaWeb.SettingControllerTest do
|
||||||
use ChiyaWeb.ConnCase
|
use ChiyaWeb.ConnCase
|
||||||
|
|
||||||
import Chiya.SiteFixtures
|
|
||||||
|
|
||||||
@create_attrs %{
|
@create_attrs %{
|
||||||
custom_css: "some custom_css",
|
custom_css: "some custom_css",
|
||||||
custom_html: "some custom_html",
|
custom_html: "some custom_html",
|
||||||
|
@ -50,8 +48,6 @@ defmodule ChiyaWeb.SettingControllerTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "edit setting" do
|
describe "edit setting" do
|
||||||
setup [:create_setting]
|
|
||||||
|
|
||||||
test "renders form for editing chosen setting", %{conn: conn} do
|
test "renders form for editing chosen setting", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/admin/settings/edit")
|
conn = get(conn, ~p"/admin/settings/edit")
|
||||||
assert html_response(conn, 200) =~ "Edit Setting"
|
assert html_response(conn, 200) =~ "Edit Setting"
|
||||||
|
@ -59,8 +55,6 @@ defmodule ChiyaWeb.SettingControllerTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "update setting" do
|
describe "update setting" do
|
||||||
setup [:create_setting]
|
|
||||||
|
|
||||||
test "redirects when data is valid", %{conn: conn} do
|
test "redirects when data is valid", %{conn: conn} do
|
||||||
conn = put(conn, ~p"/admin/settings", setting: @update_attrs)
|
conn = put(conn, ~p"/admin/settings", setting: @update_attrs)
|
||||||
assert redirected_to(conn) == ~p"/admin/settings"
|
assert redirected_to(conn) == ~p"/admin/settings"
|
||||||
|
@ -74,9 +68,4 @@ defmodule ChiyaWeb.SettingControllerTest do
|
||||||
assert html_response(conn, 200) =~ "Edit Setting"
|
assert html_response(conn, 200) =~ "Edit Setting"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp create_setting(_) do
|
|
||||||
setting = setting_fixture()
|
|
||||||
%{setting: setting}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -51,7 +51,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
|
||||||
conn = get(conn, "/")
|
conn = get(conn, "/")
|
||||||
response = html_response(conn, 200)
|
response = html_response(conn, 200)
|
||||||
assert response =~ email
|
assert response =~ email
|
||||||
assert response =~ "Settings"
|
assert response =~ "Admin"
|
||||||
assert response =~ "Log out"
|
assert response =~ "Log out"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue