mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-03 16:17:59 +01:00
static pages
This commit is contained in:
parent
4a44199a39
commit
ce450b77d9
6 changed files with 206 additions and 2 deletions
104
lib/philomena/static_pages.ex
Normal file
104
lib/philomena/static_pages.ex
Normal file
|
@ -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
|
21
lib/philomena/static_pages/static_page.ex
Normal file
21
lib/philomena/static_pages/static_page.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
12
lib/philomena_web/templates/page/show.html.slime
Normal file
12
lib/philomena_web/templates/page/show.html.slime
Normal file
|
@ -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)
|
62
test/philomena/static_pages_test.exs
Normal file
62
test/philomena/static_pages_test.exs
Normal file
|
@ -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
|
Loading…
Reference in a new issue