From ce450b77d9df17bc655ab5eca29259c7bb8cb442 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 29 Nov 2019 01:35:15 -0500 Subject: [PATCH] static pages --- lib/philomena/static_pages.ex | 104 ++++++++++++++++++ lib/philomena/static_pages/static_page.ex | 21 ++++ .../controllers/page_controller.ex | 8 +- lib/philomena_web/router.ex | 1 + .../templates/page/show.html.slime | 12 ++ test/philomena/static_pages_test.exs | 62 +++++++++++ 6 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 lib/philomena/static_pages.ex create mode 100644 lib/philomena/static_pages/static_page.ex create mode 100644 lib/philomena_web/templates/page/show.html.slime create mode 100644 test/philomena/static_pages_test.exs diff --git a/lib/philomena/static_pages.ex b/lib/philomena/static_pages.ex new file mode 100644 index 00000000..edb63077 --- /dev/null +++ b/lib/philomena/static_pages.ex @@ -0,0 +1,104 @@ +defmodule Philomena.StaticPages do + @moduledoc """ + The StaticPages context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.StaticPages.StaticPage + + @doc """ + Returns the list of static_pages. + + ## Examples + + iex> list_static_pages() + [%StaticPage{}, ...] + + """ + def list_static_pages do + Repo.all(StaticPage) + end + + @doc """ + Gets a single static_page. + + Raises `Ecto.NoResultsError` if the Static page does not exist. + + ## Examples + + iex> get_static_page!(123) + %StaticPage{} + + iex> get_static_page!(456) + ** (Ecto.NoResultsError) + + """ + def get_static_page!(id), do: Repo.get!(StaticPage, id) + + @doc """ + Creates a static_page. + + ## Examples + + iex> create_static_page(%{field: value}) + {:ok, %StaticPage{}} + + iex> create_static_page(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_static_page(attrs \\ %{}) do + %StaticPage{} + |> StaticPage.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a static_page. + + ## Examples + + iex> update_static_page(static_page, %{field: new_value}) + {:ok, %StaticPage{}} + + iex> update_static_page(static_page, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_static_page(%StaticPage{} = static_page, attrs) do + static_page + |> StaticPage.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a StaticPage. + + ## Examples + + iex> delete_static_page(static_page) + {:ok, %StaticPage{}} + + iex> delete_static_page(static_page) + {:error, %Ecto.Changeset{}} + + """ + def delete_static_page(%StaticPage{} = static_page) do + Repo.delete(static_page) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking static_page changes. + + ## Examples + + iex> change_static_page(static_page) + %Ecto.Changeset{source: %StaticPage{}} + + """ + def change_static_page(%StaticPage{} = static_page) do + StaticPage.changeset(static_page, %{}) + end +end diff --git a/lib/philomena/static_pages/static_page.ex b/lib/philomena/static_pages/static_page.ex new file mode 100644 index 00000000..54c8bbbc --- /dev/null +++ b/lib/philomena/static_pages/static_page.ex @@ -0,0 +1,21 @@ +defmodule Philomena.StaticPages.StaticPage do + use Ecto.Schema + import Ecto.Changeset + + @derive {Phoenix.Param, key: :slug} + + schema "static_pages" do + field :title, :string + field :slug, :string + field :body, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(static_page, attrs) do + static_page + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena_web/controllers/page_controller.ex b/lib/philomena_web/controllers/page_controller.ex index c8e906bf..86a9ba97 100644 --- a/lib/philomena_web/controllers/page_controller.ex +++ b/lib/philomena_web/controllers/page_controller.ex @@ -1,7 +1,11 @@ defmodule PhilomenaWeb.PageController do use PhilomenaWeb, :controller - def index(conn, _params) do - render(conn, "index.html") + alias Philomena.StaticPages.StaticPage + + plug :load_resource, model: StaticPage, id_field: "slug" + + def show(conn, _params) do + render(conn, "show.html", static_page: conn.assigns.static_page) end end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 9e9498bf..6c9ef269 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -123,6 +123,7 @@ defmodule PhilomenaWeb.Router do resources "/commissions", CommissionController, only: [:index, :show] resources "/galleries", GalleryController, only: [:index, :show] resources "/adverts", AdvertController, only: [:show] + resources "/pages", PageController, only: [:show] get "/:id", ImageController, :show # get "/:forum_id", ForumController, :show # impossible to do without constraints diff --git a/lib/philomena_web/templates/page/show.html.slime b/lib/philomena_web/templates/page/show.html.slime new file mode 100644 index 00000000..79a23c35 --- /dev/null +++ b/lib/philomena_web/templates/page/show.html.slime @@ -0,0 +1,12 @@ +h1 = @static_page.title +p + ' Last updated + => pretty_time(@static_page.updated_at) + +== @static_page.body + +/= link_to 'Revision history', static_page_versions_path(@static_page) + +/- if can? :manage, StaticPage + br + => link_to 'Edit', edit_static_page_path(@static_page) diff --git a/test/philomena/static_pages_test.exs b/test/philomena/static_pages_test.exs new file mode 100644 index 00000000..33d1fb2c --- /dev/null +++ b/test/philomena/static_pages_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.StaticPagesTest do + use Philomena.DataCase + + alias Philomena.StaticPages + + describe "static_pages" do + alias Philomena.StaticPages.StaticPage + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def static_page_fixture(attrs \\ %{}) do + {:ok, static_page} = + attrs + |> Enum.into(@valid_attrs) + |> StaticPages.create_static_page() + + static_page + end + + test "list_static_pages/0 returns all static_pages" do + static_page = static_page_fixture() + assert StaticPages.list_static_pages() == [static_page] + end + + test "get_static_page!/1 returns the static_page with given id" do + static_page = static_page_fixture() + assert StaticPages.get_static_page!(static_page.id) == static_page + end + + test "create_static_page/1 with valid data creates a static_page" do + assert {:ok, %StaticPage{} = static_page} = StaticPages.create_static_page(@valid_attrs) + end + + test "create_static_page/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = StaticPages.create_static_page(@invalid_attrs) + end + + test "update_static_page/2 with valid data updates the static_page" do + static_page = static_page_fixture() + assert {:ok, %StaticPage{} = static_page} = StaticPages.update_static_page(static_page, @update_attrs) + end + + test "update_static_page/2 with invalid data returns error changeset" do + static_page = static_page_fixture() + assert {:error, %Ecto.Changeset{}} = StaticPages.update_static_page(static_page, @invalid_attrs) + assert static_page == StaticPages.get_static_page!(static_page.id) + end + + test "delete_static_page/1 deletes the static_page" do + static_page = static_page_fixture() + assert {:ok, %StaticPage{}} = StaticPages.delete_static_page(static_page) + assert_raise Ecto.NoResultsError, fn -> StaticPages.get_static_page!(static_page.id) end + end + + test "change_static_page/1 returns a static_page changeset" do + static_page = static_page_fixture() + assert %Ecto.Changeset{} = StaticPages.change_static_page(static_page) + end + end +end