Merge branch 'content-refactors' into 'master'

feat: Add comments to admin

See merge request mythic-insight/legendary!10
This commit is contained in:
Robert Prehn 2020-07-29 13:53:44 +00:00
commit 8683083ce0
61 changed files with 461 additions and 1298 deletions

View file

@ -23,6 +23,7 @@
</div> </div>
<%= error_tag f, :password, class: "ui error message" %> <%= error_tag f, :password, class: "ui error message" %>
</div> </div>
<%= hidden_input f, :persistent_session, value: "true" %>
<%= submit "Login", class: "ui fluid large teal submit button" %> <%= submit "Login", class: "ui fluid large teal submit button" %>
</div> </div>
<% end %> <% end %>

View file

@ -11,7 +11,7 @@ defmodule Content.Attachment do
|> Post.metas_map |> Post.metas_map
deserialization_results = deserialization_results =
meta["_wp_attachment_metadata"] meta["attachment_metadata"]
|> PhpSerializer.unserialize |> PhpSerializer.unserialize
case deserialization_results do case deserialization_results do

View file

@ -1,178 +0,0 @@
defmodule Content.UpdateMenu do
alias Content.{Menu, Post, Postmeta, Repo, TermRelationship}
alias Ecto.Multi
import Ecto.Query
def run(id, new_menu_params) do
current_posts = Menu.nav_menu_items_for_id(id)
post_ids_in_new_menu = recursive_post_ids(new_menu_params)
deleted_post_ids =
current_posts
|> Enum.reject(& &1.id in post_ids_in_new_menu)
|> Enum.map(& &1.id)
Multi.new()
|> process_nodes(id, 0, new_menu_params |> add_order())
|> Multi.delete_all(:stale_nodes, from(p in Post, where: p.id in ^deleted_post_ids))
|> Repo.transaction()
end
def add_order(tree) do
{_next_order, nodes} = add_order_starting_at(tree, 1)
nodes
end
def add_order_starting_at(tree, starting_at) do
tree
|> Enum.reduce({starting_at, []}, fn node, {order, new_nodes} ->
{next_order, new_child_nodes} = add_order_starting_at(node["children"], order + 1)
new_node =
node
|> Map.merge(%{
"order" => order,
"children" => new_child_nodes,
})
{next_order, new_nodes ++ [new_node]}
end)
end
defp process_nodes(multi, menu_id, parent_id, nodes) do
nodes
|> Enum.reduce(multi, fn node, m ->
case node["post_id"] do
nil ->
create_node(m, menu_id, parent_id, node)
_id ->
update_node(m, menu_id, parent_id, node)
end
end)
end
defp create_node(multi, menu_id, parent_id, node) do
post =
Post.changeset(
%Post{},
%{
post_author: 1,
post_title: node["title"],
post_status: "publish",
comment_status: "closed",
ping_status: "closed",
menu_order: node["order"],
post_type: "nav_menu_item",
comment_count: 0,
}
)
step_name = "#{parent_id}.create_node.#{node["order"]}"
multi
|> Multi.insert(step_name, post)
|> Multi.run("#{step_name}.term_relationship", fn _repo, %{^step_name => post} ->
tr =
TermRelationship.changeset(
%TermRelationship{},
%{
object_id: post.id,
term_taxonomy_id: menu_id,
term_order: 0,
}
)
Repo.insert(tr)
end)
|> Multi.merge(fn %{^step_name => post} ->
Multi.new()
|> insert_metas(type_of_node(node), post, parent_id, node)
end)
|> Multi.merge(fn %{^step_name => post} ->
Multi.new()
|> process_nodes(menu_id, post.id, node["children"])
end)
end
defp insert_metas(multi, "post", post, parent_id, node) do
multi
|> update_meta(post.id, "_menu_item_type", "post_type")
|> update_meta(post.id, "_menu_item_object", "page")
|> update_meta(post.id, "_menu_item_object_id", node["target_id"])
|> update_meta(post.id, "_menu_item_menu_item_parent", parent_id)
end
defp insert_metas(multi, "category", post, parent_id, node) do
multi
|> update_meta(post.id, "_menu_item_type", "taxonomy")
|> update_meta(post.id, "_menu_item_object", "category")
|> update_meta(post.id, "_menu_item_object_id", node["target_id"])
|> update_meta(post.id, "_menu_item_menu_item_parent", parent_id)
end
defp insert_metas(multi, "link", post, parent_id, node) do
multi
|> update_meta(post.id, "_menu_item_type", "custom")
|> update_meta(post.id, "_menu_item_object", "custom")
|> update_meta(post.id, "_menu_item_object_id", post.id)
|> update_meta(post.id, "_menu_item_url", node["url"])
|> update_meta(post.id, "_menu_item_menu_item_parent", parent_id)
end
defp type_of_node(%{"url" => url}) when url != nil, do: "link"
defp type_of_node(%{"related_item" => %{"resource" => "posts"}}), do: "post"
defp type_of_node(%{"related_item" => %{"resource" => "category"}}), do: "category"
defp update_node(multi, menu_id, parent_id, node) do
multi
|> update_meta(node["post_id"], "_menu_item_menu_item_parent", parent_id)
|> update_order(node["post_id"], node["order"])
|> process_nodes(menu_id, node["post_id"], node["children"])
end
defp update_meta(multi, post_id, meta_key, new_value) when is_integer(new_value),
do: update_meta(multi, post_id, meta_key, new_value |> Integer.to_string())
defp update_meta(multi, post_id, meta_key, new_value) do
step_name = "#{post_id}.update_meta.#{meta_key}"
type = Postmeta.__schema__(:type, :meta_value)
cast_value = Ecto.Type.cast(type, new_value)
Postmeta
|> where([pm], pm.meta_key == ^meta_key)
|> where([pm], pm.post_id == ^post_id)
|> Repo.one()
|> case do
nil ->
multi
|> Multi.insert(
step_name,
Postmeta.changeset(
%Postmeta{},
%{
post_id: post_id,
meta_key: meta_key,
meta_value: new_value
}
)
)
%{meta_value: ^cast_value} ->
# No change needed
multi
meta ->
multi
|> Multi.update(step_name, Postmeta.changeset(meta, %{meta_value: new_value}))
end
end
defp update_order(multi, post_id, new_order) do
step_name = "#{post_id}.update_order"
multi
|> Multi.update_all(step_name, from(p in Post, where: p.id == ^post_id), [set: [menu_order: new_order]])
end
defp recursive_post_ids(params) do
params
|> Enum.flat_map(& [&1["post_id"]|recursive_post_ids(&1["children"])])
end
end

View file

@ -6,49 +6,47 @@ defmodule Content.Comment do
import Ecto.Changeset import Ecto.Changeset
alias Content.{Post} alias Content.{Post}
@primary_key {:comment_id, :id, autogenerate: true} schema "comments" do
@derive {Phoenix.Param, key: :comment_id} belongs_to :post, Post
schema "wp_comments" do field :author, :string
belongs_to :post, Post, foreign_key: :comment_post_id, references: :id field :author_email, :string
field :comment_author, :string field :author_url, :string
field :comment_author_email, :string field :author_IP, :string
field :comment_author_url, :string field :date, :naive_datetime
field :comment_author_IP, :string field :date_gmt, :naive_datetime
field :comment_date, :naive_datetime field :content, :string
field :comment_date_gmt, :naive_datetime field :karma, :integer
field :comment_content, :string field :approved, :string
field :comment_karma, :integer field :agent, :string
field :comment_approved, :string field :type, :string
field :comment_agent, :string field :parent, :integer, default: 0
field :comment_type, :string
field :comment_parent, :integer, default: 0
field :user_id, :integer field :user_id, :integer
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> Map.merge(%{ |> Map.merge(%{
comment_date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
comment_date_gmt: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), date_gmt: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
comment_approved: "1" approved: "1"
}) })
|> cast(params, [ |> cast(params, [
:comment_id, :id,
:comment_post_id, :post_id,
:comment_author, :author,
:comment_author_email, :author_email,
:comment_author_url, :author_url,
:comment_author_IP, :author_IP,
:comment_date, :date,
:comment_date_gmt, :date_gmt,
:comment_content, :content,
:comment_karma, :karma,
:comment_approved, :approved,
:comment_agent, :agent,
:comment_type, :type,
:comment_parent, :parent,
:user_id :user_id
]) ])
|> validate_required([:comment_content]) |> validate_required([:content])
end end
end end

View file

@ -0,0 +1,26 @@
defmodule Content.CommentAdmin do
def index(_) do
[
id: nil,
author: nil,
author_email: nil,
author_url: nil,
content: nil,
approved: nil,
date: nil,
date_gmt: nil,
]
end
def form_fields(_) do
[
author: nil,
author_email: nil,
author_url: nil,
content: %{type: :textarea, rows: 8},
date: nil,
date_gmt: nil,
approved: nil,
]
end
end

View file

@ -5,15 +5,14 @@ defmodule Content.Commentmeta do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:meta_id, :id, autogenerate: true} schema "commentmeta" do
schema "wp_commentmeta" do
field :comment_id, :integer field :comment_id, :integer
field :meta_key, :string field :key, :string
field :meta_value, :string field :value, :string
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:meta_id, :comment_id, :meta_key, :meta_value]) |> cast(params, [:id, :comment_id, :key, :value])
end end
end end

View file

@ -6,9 +6,9 @@ defmodule Content.Comments do
alias Content.Comment alias Content.Comment
alias Content.Repo alias Content.Repo
def children(parent_comment_id, array_of_comments) do def children(parent_id, array_of_comments) do
array_of_comments array_of_comments
|> Enum.filter(&(&1.comment_parent == parent_comment_id)) |> Enum.filter(&(&1.parent == parent_id))
end end
@doc """ @doc """

View file

@ -5,38 +5,37 @@ defmodule Content.Link do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:link_id, :id, autogenerate: true} schema "links" do
schema "wp_links" do field :url, :string
field :link_url, :string field :name, :string
field :link_name, :string field :image, :string
field :link_image, :string field :target, :string
field :link_target, :string field :description, :string
field :link_description, :string field :visible, :string
field :link_visible, :string field :owner, :integer
field :link_owner, :integer field :rating, :integer
field :link_rating, :integer field :updated, :naive_datetime
field :link_updated, :naive_datetime field :rel, :string
field :link_rel, :string field :notes, :string
field :link_notes, :string field :rss, :string
field :link_rss, :string
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [ |> cast(params, [
:link_id, :id,
:link_url, :url,
:link_name, :name,
:link_image, :image,
:link_target, :target,
:link_description, :description,
:link_visible, :visible,
:link_owner, :owner,
:link_rating, :rating,
:link_updated, :updated,
:link_rel, :rel,
:link_notes, :notes,
:link_rss :rss
]) ])
end end
end end

View file

@ -1,167 +0,0 @@
defmodule Content.Menu do
@moduledoc """
Module for retrieving, manipulating, and processing navigation menus.
"""
alias Content.{Option, Post, Repo, Term, TermRelationship}
import Ecto.Query
def put_menu_option(option_name, location_name, menu_id) do
option =
Option
|> where(option_name: ^option_name)
|> Repo.one()
|> Kernel.||(%Option{option_name: option_name, option_value: "a:0:{}"})
value =
option
|> Option.parse_option_value
nav_menu_locations =
value
|> Enum.find(fn {key, _value} -> key == "nav_menu_locations" end)
|> Kernel.||({"nav_menu_locations", []})
|> elem(1)
new_nav_menu_locations =
nav_menu_locations
|> Enum.filter(fn {key, _value} -> key != location_name end)
|> Kernel.++([{location_name, menu_id}])
new_value =
value
|> Enum.filter(fn {key, _value} -> key != "nav_menu_locations" end)
|> Kernel.++([{"nav_menu_locations", new_nav_menu_locations}])
option
|> Option.put_new_value(new_value)
end
def get_menu_from_option_and_location(option_name, location_name) do
option =
Option
|> where(option_name: ^option_name)
|> Repo.one()
|> Kernel.||(%Option{option_name: option_name, option_value: "a:0:{}"})
value =
option
|> Option.parse_option_value
menu_pair =
value
|> Enum.find(fn {key, _value} -> key == "nav_menu_locations" end)
|> Kernel.||({"nav_menu_locations", []})
|> elem(1)
|> Enum.find(fn {key, _value} -> key == location_name end)
case menu_pair do
{^location_name, menu_id} ->
menu_id |> get_menu_from_id()
nil ->
nil
end
end
def get_menu_from_id(menu_id) do
menu_id
|> nav_menu_items_for_id()
|> arrange_menu_item_posts()
end
def nav_menu_items_for_id(menu_id) do
Post
|> join(
:inner,
[p],
tr in TermRelationship,
on: p.id == tr.object_id
)
|> order_by(:menu_order)
|> preload(:metas)
|> where([p, tr], tr.term_taxonomy_id == ^menu_id)
|> Repo.all()
end
defp arrange_menu_item_posts(nav_posts, parent_id \\ "0", nav_to_post_map \\ nil) do
nav_to_post_map = nav_to_post_map || make_nav_to_post_map(nav_posts)
nav_posts
|> Enum.filter(fn post ->
meta_map = post |> Post.metas_map
meta_map["_menu_item_menu_item_parent"] == parent_id
end)
|> Enum.map(fn post ->
meta_map = post |> Post.metas_map
related_item =
if meta_map["_menu_item_object"] == "category" do
item = nav_to_post_map["category/#{meta_map["_menu_item_object_id"]}"] || %Term{}
%{
title: item.name,
slug: item.slug,
resource: "category",
}
else
item = nav_to_post_map["post/#{meta_map["_menu_item_object_id"]}"] || %Post{}
%{
title: item.post_title,
slug: item.post_name,
resource: "posts",
}
end
%{
post_id: post.id,
type: meta_map["_menu_item_object"],
target_id: meta_map["_menu_item_object_id"],
parent_id: meta_map["_menu_item_menu_item_parent"],
url: meta_map["_menu_item_url"],
related_item: related_item,
children: arrange_menu_item_posts(nav_posts, Integer.to_string(post.id), nav_to_post_map),
}
end)
end
defp make_nav_to_post_map(nav_posts) do
nav_post_meta_map = nav_posts |> Post.metas_map()
linked_post_ids =
nav_post_meta_map
|> Enum.filter(fn {_key, value} ->
value["_menu_item_object"] != "category"
end)
|> Enum.map(fn {_key, value} ->
value["_menu_item_object_id"]
end)
nav_to_post_map =
Post
|> where([p], p.id in ^linked_post_ids)
|> Repo.all()
|> Enum.map(fn post ->
{"post/#{post.id}", post}
end)
|> Map.new
linked_category_ids =
nav_post_meta_map
|> Enum.filter(fn {_key, value} ->
value["_menu_item_object"] == "category"
end)
|> Enum.map(fn {_key, value} ->
value["_menu_item_object_id"]
end)
nav_to_category_map =
Term
|> where([t], t.term_id in ^linked_category_ids)
|> Repo.all()
|> Enum.map(fn category ->
{"category/#{category.term_id}", category}
end)
|> Map.new
nav_to_post_map |> Map.merge(nav_to_category_map)
end
end

View file

@ -5,20 +5,19 @@ defmodule Content.Option do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:option_id, :id, autogenerate: true} schema "options" do
schema "wp_options" do field :name, :string
field :option_name, :string
field :autoload, :string field :autoload, :string
field :option_value, :string field :value, :string
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:option_id, :option_name, :option_value, :autoload]) |> cast(params, [:id, :name, :value, :autoload])
end end
def parse_option_value(struct) do def parse_value(struct) do
case PhpSerializer.unserialize(struct.option_value) do case PhpSerializer.unserialize(struct.value) do
{:ok, values} -> {:ok, values} ->
values values
end end
@ -26,6 +25,6 @@ defmodule Content.Option do
def put_new_value(struct, value) do def put_new_value(struct, value) do
struct struct
|> change(%{option_value: PhpSerializer.serialize(value)}) |> change(%{value: PhpSerializer.serialize(value)})
end end
end end

View file

@ -8,13 +8,13 @@ defmodule Content.Options do
def put(key, value) do def put(key, value) do
%Option{} %Option{}
|> Option.changeset(%{ |> Option.changeset(%{
option_name: key, name: key,
option_value: value, value: value,
}) })
|> Repo.insert() |> Repo.insert()
end end
def get(key), do: Option |> Repo.get_by(option_name: key) def get(key), do: Option |> Repo.get_by(name: key)
def get_value(key) do def get_value(key) do
case get(key) do case get(key) do
@ -22,7 +22,7 @@ defmodule Content.Options do
nil nil
opt -> opt ->
opt opt
|> (&(&1.option_value)).() |> (&(&1.value)).()
end end
end end

View file

@ -6,38 +6,37 @@ defmodule Content.Post do
import Ecto.Changeset import Ecto.Changeset
alias Content.Slugs alias Content.Slugs
@primary_key {:id, :id, autogenerate: true} @derive {Phoenix.Param, key: :name}
@derive {Phoenix.Param, key: :post_name} schema "posts" do
schema "wp_posts" do field :date, :naive_datetime
field :post_date, :naive_datetime field :date_gmt, :naive_datetime
field :post_date_gmt, :naive_datetime field :content, :string, default: ""
field :post_content, :string, default: "" field :title, :string
field :post_title, :string field :excerpt, :string
field :post_excerpt, :string field :status, :string
field :post_status, :string
field :comment_status, :string field :comment_status, :string
field :ping_status, :string field :ping_status, :string
field :post_password, :string, default: "" field :password, :string, default: ""
field :post_name, :string field :name, :string
field :to_ping, :string, default: "" field :to_ping, :string, default: ""
field :pinged, :string, default: "" field :pinged, :string, default: ""
field :post_modified, :naive_datetime field :modified, :naive_datetime
field :post_modified_gmt, :naive_datetime field :modified_gmt, :naive_datetime
field :post_content_filtered, :string, default: "" field :content_filtered, :string, default: ""
field :post_parent, :integer field :parent, :integer
field :guid, :string field :guid, :string
field :menu_order, :integer field :menu_order, :integer
field :post_type, :string field :type, :string
field :post_mime_type, :string field :mime_type, :string
field :comment_count, :integer field :comment_count, :integer
field :sticky, :boolean, [virtual: true, default: false] field :sticky, :boolean, [virtual: true, default: false]
has_many :metas, Content.Postmeta, foreign_key: :post_id has_many :metas, Content.Postmeta
has_many :comments, Content.Comment, foreign_key: :comment_post_id has_many :comments, Content.Comment
has_many :term_relationships, Content.TermRelationship, foreign_key: :object_id has_many :term_relationships, Content.TermRelationship, foreign_key: :object_id
has_many :categories, through: [:term_relationships, :category, :term] has_many :categories, through: [:term_relationships, :category, :term]
has_many :tags, through: [:term_relationships, :tag, :term] has_many :tags, through: [:term_relationships, :tag, :term]
has_one :post_format, through: [:term_relationships, :post_format, :term] has_one :format, through: [:term_relationships, :format, :term]
belongs_to :author, Auth.User, foreign_key: :post_author belongs_to :author, Auth.User
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
@ -46,35 +45,35 @@ defmodule Content.Post do
params, params,
[ [
:id, :id,
:post_author, :author_id,
:post_date, :date,
:post_date_gmt, :date_gmt,
:post_content, :content,
:post_title, :title,
:post_excerpt, :excerpt,
:post_status, :status,
:comment_status, :comment_status,
:ping_status, :ping_status,
:post_password, :password,
:post_name, :name,
:to_ping, :to_ping,
:pinged, :pinged,
:post_content_filtered, :content_filtered,
:post_parent, :parent,
:menu_order, :menu_order,
:post_type, :type,
:post_mime_type, :mime_type,
:comment_count, :comment_count,
:sticky, :sticky,
]) ])
|> put_default(:post_date, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> put_default(:date, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
|> put_default(:post_date_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> put_default(:date_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
|> put_change(:post_modified, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> put_change(:modified, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
|> put_change(:post_modified_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> put_change(:modified_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> maybe_put_guid() |> maybe_put_guid()
|> validate_required([:post_name, :post_status]) |> validate_required([:name, :status])
|> validate_inclusion(:post_status, ["publish", "future", "draft", "pending", "private", "trash", "auto-draft", "inherit"]) |> validate_inclusion(:status, ["publish", "future", "draft", "pending", "private", "trash", "auto-draft", "inherit"])
end end
def put_default(changeset, key, value) do def put_default(changeset, key, value) do
@ -93,14 +92,14 @@ defmodule Content.Post do
end end
def content_page(struct, page) do def content_page(struct, page) do
(struct.post_content || "") (struct.content || "")
|> String.split("<!--nextpage-->") |> String.split("<!--nextpage-->")
|> Enum.at(page - 1) |> Enum.at(page - 1)
|> Kernel.||("") |> Kernel.||("")
end end
def content_page_count(struct) do def content_page_count(struct) do
(struct.post_content || "") (struct.content || "")
|> String.split("<!--nextpage-->") |> String.split("<!--nextpage-->")
|> Enum.count |> Enum.count
end end
@ -118,13 +117,13 @@ defmodule Content.Post do
end end
def metas_map(%Content.Post{} = struct) do def metas_map(%Content.Post{} = struct) do
struct.metas struct.metas
|> Enum.map(&({&1.meta_key, &1.meta_value})) |> Enum.map(&({&1.key, &1.value}))
|> Map.new |> Map.new
end end
def maybe_put_guid(changeset) do def maybe_put_guid(changeset) do
import Content.Router.Helpers, only: [posts_url: 3] import Content.Router.Helpers, only: [posts_url: 3]
slug = changeset |> get_field(:post_name) slug = changeset |> get_field(:name)
case slug do case slug do
nil -> changeset nil -> changeset

View file

@ -20,12 +20,12 @@ defmodule Content.PostAdmin do
def index(_) do def index(_) do
[ [
id: nil, id: nil,
post_type: nil, type: nil,
post_name: nil, name: nil,
post_title: nil, title: nil,
post_status: nil, status: nil,
post_date_gmt: nil, date_gmt: nil,
post_modified_gmt: nil, modified_gmt: nil,
] ]
end end
@ -43,17 +43,17 @@ defmodule Content.PostAdmin do
end) end)
[ [
post_type: %{choices: [{"Blog Post", :post}, {"Page", :page}]}, type: %{choices: [{"Blog Post", :post}, {"Page", :page}]},
post_name: %{label: "Slug"}, name: %{label: "Slug"},
post_title: nil, title: nil,
post_content: %{type: :textarea, rows: 32}, content: %{type: :textarea, rows: 32},
post_status: %{choices: [{"Publish", :publish}, {"Draft", :draft}]}, status: %{choices: [{"Publish", :publish}, {"Draft", :draft}]},
post_author: %{choices: authors}, author_id: %{choices: authors},
post_excerpt: %{type: :textarea, rows: 4}, excerpt: %{type: :textarea, rows: 4},
sticky: nil, sticky: nil,
comment_status: %{choices: [{"open", :open}, {"closed", :closed}]}, comment_status: %{choices: [{"open", :open}, {"closed", :closed}]},
ping_status: %{choices: [{"open", :open}, {"closed", :closed}]}, ping_status: %{choices: [{"open", :open}, {"closed", :closed}]},
post_password: nil, password: nil,
menu_order: nil, menu_order: nil,
] ]
end end

View file

@ -5,15 +5,14 @@ defmodule Content.Postmeta do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:meta_id, :id, autogenerate: true} schema "postmeta" do
schema "wp_postmeta" do
belongs_to :post, Content.Post belongs_to :post, Content.Post
field :meta_key, :string field :key, :string
field :meta_value, :string field :value, :string
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:meta_id, :post_id, :meta_key, :meta_value]) |> cast(params, [:id, :post_id, :key, :value])
end end
end end

View file

@ -12,7 +12,7 @@ defmodule Content.Posts do
alias Content.Post alias Content.Post
alias Ecto.Changeset alias Ecto.Changeset
@preloads [:metas, :author, :categories, :tags, :comments, :post_format] @preloads [:metas, :author, :categories, :tags, :comments, :format]
@doc """ @doc """
Returns the lisdpt of posts for admin interface. Returns the lisdpt of posts for admin interface.
@ -23,14 +23,14 @@ defmodule Content.Posts do
[%Post{}, ...] [%Post{}, ...]
""" """
def list_admin_posts(page, post_type \\ "post") do def list_admin_posts(page, type \\ "post") do
post_type = post_type || "post" type = type || "post"
Repo.all( Repo.all(
from p in Post, from p in Post,
where: p.post_type == ^post_type, where: p.type == ^type,
where: p.post_status in ["publish", "future", "draft", "pending", "private", "inherit"], where: p.status in ["publish", "future", "draft", "pending", "private", "inherit"],
preload: ^@preloads, preload: ^@preloads,
order_by: [desc: p.post_date], order_by: [desc: p.date],
limit: @page_size, limit: @page_size,
offset: ^(@page_size * (String.to_integer(page) - 1)) offset: ^(@page_size * (String.to_integer(page) - 1))
) )
@ -59,12 +59,12 @@ defmodule Content.Posts do
end end
def post_scope_for_params(params) do def post_scope_for_params(params) do
post_type = params |> Map.get("post_type", "post") type = params |> Map.get("type", "post")
category = params |> Map.get("category") category = params |> Map.get("category")
query = query =
post_scope() post_scope()
|> where([p], p.post_type == ^post_type) |> where([p], p.type == ^type)
if category do if category do
query |> join(:inner, [p], term in assoc(p, :categories), on: term.slug == ^category) query |> join(:inner, [p], term in assoc(p, :categories), on: term.slug == ^category)
@ -75,15 +75,15 @@ defmodule Content.Posts do
def post_scope do def post_scope do
from p in Post, from p in Post,
where: p.post_status == "publish", where: p.status == "publish",
preload: ^@preloads, preload: ^@preloads,
order_by: [desc: p.post_date] order_by: [desc: p.date]
end end
def post_scope_with_drafts do def post_scope_with_drafts do
from p in Post, from p in Post,
preload: ^@preloads, preload: ^@preloads,
order_by: [desc: p.post_date] order_by: [desc: p.date]
end end
def sticky_posts_for_page(%{"page" => "1"} = params) do def sticky_posts_for_page(%{"page" => "1"} = params) do
@ -103,12 +103,12 @@ defmodule Content.Posts do
def sticky_posts_for_page(_), do: [] def sticky_posts_for_page(_), do: []
defp sticky_ids do defp sticky_ids do
case Repo.one(from opt in Option, where: opt.option_name == "sticky_posts") do case Repo.one(from opt in Option, where: opt.name == "sticky_posts") do
nil -> nil ->
[] []
option -> option ->
option option
|> Option.parse_option_value |> Option.parse_value
|> Enum.map(&(elem(&1, 1))) |> Enum.map(&(elem(&1, 1)))
end end
end end
@ -175,14 +175,14 @@ defmodule Content.Posts do
case Integer.parse(id, 10) do case Integer.parse(id, 10) do
:error -> :error ->
scope |> where([p], p.post_name == ^id) scope |> where([p], p.name == ^id)
{int_id, _} -> {int_id, _} ->
scope |> where([p], p.id == ^int_id) scope |> where([p], p.id == ^int_id)
end end
end end
post_scope() post_scope()
|> where([p], p.post_type != "nav_menu_item") |> where([p], p.type != "nav_menu_item")
|> id_filter.(slug) |> id_filter.(slug)
end end
@ -205,16 +205,16 @@ defmodule Content.Posts do
case Integer.parse(id, 10) do case Integer.parse(id, 10) do
:error -> :error ->
scope |> where([p], p.post_name == ^id) scope |> where([p], p.name == ^id)
{int_id, ""} -> {int_id, ""} ->
scope |> where([p], p.id == ^int_id) scope |> where([p], p.id == ^int_id)
{_int_id, _} -> {_int_id, _} ->
scope |> where([p], p.post_name == ^id) scope |> where([p], p.name == ^id)
end end
end end
post_scope_with_drafts() post_scope_with_drafts()
|> where([p], p.post_type != "nav_menu_item") |> where([p], p.type != "nav_menu_item")
|> id_filter.(slug) |> id_filter.(slug)
|> Repo.one!() |> Repo.one!()
end end
@ -244,7 +244,7 @@ defmodule Content.Posts do
%Post{} %Post{}
|> Repo.preload(@preloads) |> Repo.preload(@preloads)
|> Post.changeset(attrs) |> Post.changeset(attrs)
|> Changeset.put_change(:post_name, "preview") |> Changeset.put_change(:name, "preview")
|> Changeset.apply_changes() |> Changeset.apply_changes()
end end

View file

@ -7,14 +7,14 @@ defmodule Content.Slugs do
def ensure_post_has_slug(changeset) do def ensure_post_has_slug(changeset) do
cond do cond do
!is_nil(changeset |> get_field(:post_name)) -> !is_nil(changeset |> get_field(:name)) ->
changeset changeset
is_nil(changeset |> get_field(:post_title)) -> is_nil(changeset |> get_field(:title)) ->
changeset changeset
|> put_change( |> put_change(
:post_name, :name,
changeset changeset
|> get_field(:post_date) |> get_field(:date)
|> Kernel.||(NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> Kernel.||(NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
|> Timex.format!("%F", :strftime) |> Timex.format!("%F", :strftime)
|> Slugger.slugify_downcase() |> Slugger.slugify_downcase()
@ -23,16 +23,16 @@ defmodule Content.Slugs do
true -> true ->
changeset changeset
|> put_change( |> put_change(
:post_name, :name,
changeset changeset
|> get_field(:post_title) |> get_field(:title)
|> Slugger.slugify_downcase() |> Slugger.slugify_downcase()
|> unique_slug(changeset |> get_field(:id)) |> unique_slug(changeset |> get_field(:id))
) )
end end
end end
defp unique_slug(proposed_slug, post_id, postfix_number \\ 0) do defp unique_slug(proposed_slug, id, postfix_number \\ 0) do
proposed_slug_with_postfix = proposed_slug_with_postfix =
if postfix_number == 0 do if postfix_number == 0 do
proposed_slug proposed_slug
@ -44,8 +44,8 @@ defmodule Content.Slugs do
Repo.aggregate( Repo.aggregate(
( (
Post Post
|> where([post], post.post_name == ^proposed_slug_with_postfix) |> where([post], post.name == ^proposed_slug_with_postfix)
|> post_id_match(post_id) |> post_id_match(id)
), ),
:count, :count,
:id :id
@ -54,7 +54,7 @@ defmodule Content.Slugs do
if competition_count == 0 do if competition_count == 0 do
proposed_slug_with_postfix proposed_slug_with_postfix
else else
unique_slug(proposed_slug, post_id, postfix_number + 1) unique_slug(proposed_slug, id, postfix_number + 1)
end end
end end

View file

@ -5,8 +5,7 @@ defmodule Content.Term do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:term_id, :id, autogenerate: true} schema "terms" do
schema "wp_terms" do
field :name, :string field :name, :string
field :slug, :string field :slug, :string
field :term_group, :integer field :term_group, :integer
@ -14,6 +13,6 @@ defmodule Content.Term do
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:term_id, :name, :slug, :term_group]) |> cast(params, [:id, :name, :slug, :term_group])
end end
end end

View file

@ -8,30 +8,26 @@ defmodule Content.TermRelationship do
@primary_key {:object_id, :integer, []} @primary_key {:object_id, :integer, []}
@primary_key {:term_taxonomy_id, :integer, []} @primary_key {:term_taxonomy_id, :integer, []}
schema "wp_term_relationships" do schema "term_relationships" do
field :term_order, :integer field :term_order, :integer
belongs_to :post, Post, foreign_key: :object_id, references: :id belongs_to :post, Post, foreign_key: :object_id, references: :id
belongs_to :term_taxonomy, belongs_to :term_taxonomy,
Content.TermTaxonomy, Content.TermTaxonomy,
foreign_key: :term_taxonomy_id, foreign_key: :term_taxonomy_id,
references: :term_taxonomy_id,
define_field: false define_field: false
belongs_to :category, belongs_to :category,
Content.TermTaxonomy, Content.TermTaxonomy,
foreign_key: :term_taxonomy_id, foreign_key: :term_taxonomy_id,
references: :term_taxonomy_id,
define_field: false, define_field: false,
where: [taxonomy: "category"] where: [taxonomy: "category"]
belongs_to :tag, belongs_to :tag,
Content.TermTaxonomy, Content.TermTaxonomy,
foreign_key: :term_taxonomy_id, foreign_key: :term_taxonomy_id,
references: :term_taxonomy_id,
define_field: false, define_field: false,
where: [taxonomy: "post_tag"] where: [taxonomy: "post_tag"]
belongs_to :post_format, belongs_to :format,
Content.TermTaxonomy, Content.TermTaxonomy,
foreign_key: :term_taxonomy_id, foreign_key: :term_taxonomy_id,
references: :term_taxonomy_id,
define_field: false, define_field: false,
where: [taxonomy: "post_format"] where: [taxonomy: "post_format"]
end end

View file

@ -5,17 +5,16 @@ defmodule Content.TermTaxonomy do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:term_taxonomy_id, :id, autogenerate: true} schema "term_taxonomy" do
schema "wp_term_taxonomy" do
field :taxonomy, :string field :taxonomy, :string
field :description, :string field :description, :string
field :parent, :integer field :parent, :integer
field :count, :integer field :count, :integer
belongs_to :term, Content.Term, foreign_key: :term_id, references: :term_id belongs_to :term, Content.Term
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:term_taxonomy_id, :term_id, :taxonomy, :description, :parent, :count]) |> cast(params, [:id, :term_id, :taxonomy, :description, :parent, :count])
end end
end end

View file

@ -5,15 +5,14 @@ defmodule Content.Termmeta do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@primary_key {:meta_id, :id, autogenerate: true} schema "termmeta" do
schema "wp_termmeta" do
field :term_id, :integer field :term_id, :integer
field :meta_key, :string field :key, :string
field :meta_value, :string field :value, :string
end end
def changeset(struct, params \\ %{}) do def changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:meta_id, :term_id, :meta_key, :meta_value]) |> cast(params, [:id, :term_id, :key, :value])
end end
end end

View file

@ -9,7 +9,7 @@ defmodule Content.Terms do
def categories do def categories do
from t in Content.Term, from t in Content.Term,
join: tt in Content.TermTaxonomy, join: tt in Content.TermTaxonomy,
on: t.term_id == tt.term_id, on: t.id == tt.term_id,
where: tt.taxonomy == "category" where: tt.taxonomy == "category"
end end
end end

View file

@ -15,7 +15,7 @@ defmodule Content.CommentController do
{:ok, comment} -> {:ok, comment} ->
post = post =
Post Post
|> where([p], p.id == ^comment.comment_post_id) |> where([p], p.id == ^comment.post_id)
|> Repo.one() |> Repo.one()
conn conn
@ -24,7 +24,7 @@ defmodule Content.CommentController do
{:error, _} -> {:error, _} ->
post = post =
Post Post
|> where([p], p.id == ^comment_params["comment_post_id"]) |> where([p], p.id == ^comment_params["post_id"])
|> Repo.one() |> Repo.one()
conn conn
@ -39,7 +39,7 @@ defmodule Content.CommentController do
{:ok, comment} -> {:ok, comment} ->
post = post =
Post Post
|> where([p], p.id == ^comment.comment_post_id) |> where([p], p.id == ^comment.post_id)
|> Repo.one() |> Repo.one()
conn conn
@ -48,7 +48,7 @@ defmodule Content.CommentController do
{:error, _} -> {:error, _} ->
post = post =
Post Post
|> where([p], p.id == ^comment_params["comment_post_id"]) |> where([p], p.id == ^comment_params["post_id"])
|> Repo.one() |> Repo.one()
conn conn
@ -61,7 +61,7 @@ defmodule Content.CommentController do
{:ok, comment} = Comments.delete_comment(comment) {:ok, comment} = Comments.delete_comment(comment)
post = post =
Post Post
|> where([p], p.id == ^comment.comment_post_id) |> where([p], p.id == ^comment.post_id)
|> Repo.one() |> Repo.one()
conn conn

View file

@ -1,39 +0,0 @@
defmodule Content.MenusController do
use Content, :controller
alias Content.Repo
def edit(conn, %{"id" => id}) do
menu = id |> Content.Menu.get_menu_from_id()
posts =
Content.Posts.post_scope
|> Repo.all()
|> Enum.map(fn post ->
post |> Map.take([:id, :post_title, :post_name])
end)
categories =
Content.Terms.categories
|> Repo.all()
|> Enum.map(fn cat ->
cat |> Map.take([:name, :slug, :term_group, :term_id])
end)
conn
|> render(
"edit.html",
[
id: id,
menu: menu,
posts: posts,
categories: categories,
]
)
end
def update(conn, %{"id" => id, "menu" => menu}) do
Content.UpdateMenu.run(id, menu |> Phoenix.json_library().decode!())
conn
|> redirect(to: Routes.menus_path(conn, :edit, id))
end
end

View file

@ -1,8 +1,7 @@
defmodule Content.PostsController do defmodule Content.PostsController do
use Content, :controller use Content, :controller
alias Auth.User alias Content.{Options, Posts}
alias Content.{Options, Post, Posts, Repo}
plug :put_layout, false when action in [:preview] plug :put_layout, false when action in [:preview]
@ -44,34 +43,6 @@ defmodule Content.PostsController do
show(conn, %{"id" => page_id}) show(conn, %{"id" => page_id})
end end
def new(conn, params) do
changeset = Posts.change_posts(%Post{})
render(
conn,
"new.html",
changeset: changeset,
post_type: params["post_type"] || "post",
author_options: User |> Repo.all()
)
end
def create(conn, %{"post" => post_params}) do
case Posts.create_posts(post_params) do
{:ok, post} ->
conn
|> put_flash(:info, "Posts created successfully.")
|> redirect(to: Routes.posts_path(conn, :show, post))
{:error, %Ecto.Changeset{} = changeset} ->
render(
conn,
"new.html",
changeset: changeset,
post_type: post_params["post_type"] || "post",
author_options: User |> Repo.all()
)
end
end
def preview(conn, %{"post" => post_params}) do def preview(conn, %{"post" => post_params}) do
post = Posts.preview_post(post_params) post = Posts.preview_post(post_params)
@ -84,8 +55,6 @@ defmodule Content.PostsController do
end end
def show(conn, %{"id" => id, "page" => page_string}) do def show(conn, %{"id" => id, "page" => page_string}) do
{page_id_for_posts, _} = Options.get_value_as_int("page_for_posts")
post = Posts.get_post(id) post = Posts.get_post(id)
if is_nil(post) do if is_nil(post) do
@ -126,57 +95,15 @@ defmodule Content.PostsController do
page = String.to_integer(page_string) page = String.to_integer(page_string)
thumbs = [post] |> Posts.thumbs_for_posts() thumbs = [post] |> Posts.thumbs_for_posts()
case post.post_type do case post.type do
"attachment" -> "attachment" ->
{:ok, decoded} = post.post_content |> Base.decode64 {:ok, decoded} = post.content |> Base.decode64
conn conn
|> put_resp_content_type(post.post_mime_type, "binary") |> put_resp_content_type(post.mime_type, "binary")
|> send_resp(conn.status || 200, decoded) |> send_resp(conn.status || 200, decoded)
_ -> _ ->
render(conn, template, post: post, page: page, thumbs: thumbs) render(conn, template, post: post, page: page, thumbs: thumbs)
end end
end end
def edit(conn, %{"id" => id}) do
posts = Posts.get_post_with_drafts!(id)
changeset = Posts.change_posts(posts)
render(
conn,
"edit.html",
posts: posts,
changeset: changeset,
post_type: posts.post_type || "post",
author_options: User |> Repo.all()
)
end
def update(conn, %{"id" => id, "post" => posts_params}) do
posts = Posts.get_post_with_drafts!(id)
case Posts.update_posts(posts, posts_params) do
{:ok, posts} ->
conn
|> put_flash(:info, "Posts updated successfully.")
|> redirect(to: Routes.posts_path(conn, :edit, posts))
{:error, %Ecto.Changeset{} = changeset} ->
render(
conn,
"edit.html",
posts: posts,
changeset: changeset,
post_type: posts.post_type || "post",
author_options: User |> Repo.all()
)
end
end
def delete(conn, %{"id" => id}) do
posts = Posts.get_post_with_drafts!(id)
{:ok, _posts} = Posts.delete_posts(posts)
conn
|> put_flash(:info, "Posts deleted successfully.")
|> redirect(to: Routes.admin_posts_path(conn, :index))
end
end end

View file

@ -8,7 +8,7 @@ defmodule Content.SitemapController do
def index(conn, _params) do def index(conn, _params) do
posts = posts =
Posts.post_scope Posts.post_scope
|> where([p], p.post_type not in ["nav_menu_item", "attachment"]) |> where([p], p.type not in ["nav_menu_item", "attachment"])
|> Repo.all() |> Repo.all()
categories = categories =

View file

@ -1 +0,0 @@
<h1>Dashboard</h1>

View file

@ -1,93 +0,0 @@
<div class="grid">
<div class="column">
<h1>
<%= humanize(@post_type) %>s
</h1>
</div>
<div class="column admin-actions">
<%= link "New " <> humanize(@post_type), to: Routes.posts_path(@conn, :new, post_type: @post_type), class: "button" %>
</div>
</div>
<table class="admin-table small">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Categories</th>
<th>Tags</th>
<th>Comments</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<%= for post <- @posts do %>
<tr>
<td>
<%= link post.post_title, to: Routes.posts_path(@conn, :edit, post) %>
</td>
<td>
<%= if !is_nil(post.author) do %>
<%= post.author.display_name %>
<% end %>
</td>
<td>
<%= post.categories |> Enum.map(&(&1.name)) |> Enum.join(", ") %>
</td>
<td>
<%= post.tags |> Enum.map(&(&1.name)) |> Enum.join(", ") %>
</td>
<td>
<%= post.comments |> Enum.count() %>
</td>
<td>
<%= case post.post_status do %>
<% "publish" -> %>
<%= "Published" %>
<% "future" -> %>
<%= "Scheduled" %>
<% "draft" -> %>
<%= "Last Modified" %>
<% "pending" -> %>
<%= "Scheduled" %>
<% "private" -> %>
<%= "Published Privately" %>
<% "inherit" -> %>
<%= "Inherit" %>
<% end %>
<div>
<%= post.post_date |> Timex.format!("%F", :strftime) %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<hr />
<%= if @page > 1 do %>
<%= link 1, to: Routes.admin_posts_path(@conn, :index, page: 1) %>
<% end %>
<%= if @page > 3 do %>
...
<% end %>
<%= if @page > 2 do %>
<%= link @page - 1, to: Routes.admin_posts_path(@conn, :index, page: @page - 1) %>
<% end %>
<%= @page %>
<%= if @page + 1 < @last_page do %>
<%= link @page + 1, to: Routes.admin_posts_path(@conn, :index, page: @page + 1) %>
<% end %>
<%= if @page + 2 < @last_page do %>
...
<% end %>
<%= if @page < @last_page do %>
<%= link @last_page, to: Routes.admin_posts_path(@conn, :index, page: @last_page) %>
<% end %>

View file

@ -8,12 +8,12 @@
<%= for post <- @posts do %> <%= for post <- @posts do %>
<item> <item>
<title><%= post.post_title |> HtmlSanitizeEx.strip_tags() %></title> <title><%= post.title |> HtmlSanitizeEx.strip_tags() %></title>
<description> <description>
<%= post.post_content |> process_content |> html_escape |> safe_to_string %> <%= post.content |> process_content |> html_escape |> safe_to_string %>
</description> </description>
<pubDate><%= <pubDate><%=
post.post_date post.date
|> DateTime.from_naive!("Etc/UTC") |> DateTime.from_naive!("Etc/UTC")
|> Timex.format!("{WDshort}, {D} {Mshort} {YYYY} {h24}:{m}:{s} {Z}") |> Timex.format!("{WDshort}, {D} {Mshort} {YYYY} {h24}:{m}:{s} {Z}")
%></pubDate> %></pubDate>

View file

@ -10,12 +10,6 @@
</head> </head>
<body> <body>
<main role="main" class="container"> <main role="main" class="container">
<%= for level <- [:info, :danger] do %>
<%= if get_flash(@conn, level) do %>
<p class="alert alert-<%= level %>" role="alert"><%= get_flash(@conn, level) %></p>
<% end %>
<% end %>
<%= render "_side_menu.html", assigns %> <%= render "_side_menu.html", assigns %>
<!-- Page Contents --> <!-- Page Contents -->
@ -25,6 +19,8 @@
</div> </div>
</div> </div>
<%= flash_block(@conn) %>
<%= @inner_content %> <%= @inner_content %>
</main> </main>
</body> </body>

View file

@ -2,19 +2,19 @@
<%= Enum.map(Content.Comments.children(@parent_id, @post.comments), fn comment -> %> <%= Enum.map(Content.Comments.children(@parent_id, @post.comments), fn comment -> %>
<div class="event"> <div class="event">
<div class="label"> <div class="label">
<img src="<%= comment.comment_author_email |> gravatar_url_for_email %>" /> <img src="<%= comment.author_email |> gravatar_url_for_email %>" />
</div> </div>
<div class="content"> <div class="content">
<div class="summary"> <div class="summary">
<span class="user"> <span class="user">
<%= comment.comment_author || "Anonymous" %> <%= comment.author || "Anonymous" %>
</span> </span>
<div class="date"> <div class="date">
<%= comment.comment_date |> Timex.format!("%F", :strftime) %> <%= comment.date |> Timex.format!("%F", :strftime) %>
</div> </div>
</div> </div>
<div class="extra text"> <div class="extra text">
<%= sanitize comment.comment_content |> auto_paragraph_tags |> elem(1) |> IO.iodata_to_binary() %> <%= sanitize comment.content |> auto_paragraph_tags |> elem(1) |> IO.iodata_to_binary() %>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1 +0,0 @@
<%= render "form.html", Map.put(assigns, :action, Routes.posts_path(@conn, :update, @posts)) %>

View file

@ -1,82 +0,0 @@
<%= form_for @changeset, @action, fn f -> %>
<div class="grid">
<div class="column three">
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<ul>
<%= for {error_key, error} <- @changeset.errors |> Keyword.drop([:post_title, :post_content]) do %>
<%= if error do %>
<li><%= error_key %>: <%= error_tag f, error_key %></li>
<% end %>
<% end %>
</ul>
<% end %>
<%= label f, :post_title do %>
Title
<%= text_input f, :post_title %>
<% end %>
<%= label f, :post_content, "data-react-class": "Editor" do %>
Content
<%= textarea f, :post_content, "data-simplemde": true %>
<% end %>
<div class="grid">
<div class="form-group input-group column uLeft">
<%= submit "Delete", class: "btn btn-warning", form: "deleteForm" %>
</div>
<div class="form-group input-group column">
<%= submit "Save", class: "btn btn-primary" %>
</div>
</div>
</div>
<div class="column post-admin-sidebar">
<%= label f, :post_status, class: "column input-group" do %>
<div>Status</div>
<%= select f, :post_status, [{"Publish", :publish}, {"Draft", :draft}] %>
<% end %>
<%= label f, :post_author, class: "input-group" do %>
<div>Author</div>
<%= select f, :post_author, @author_options |> Enum.map(&({&1.display_name, &1.id})) %>
<% end %>
<%= label f, :post_excerpt, class: "input-group" do %>
<div>Excerpt</div>
<%= textarea f, :post_excerpt %>
<% end %>
<%= label f, :sticky, class: "input-group" do %>
<div>Sticky?</div>
<%= checkbox f, :sticky %>
<% end %>
<%= label f, :comment_status, class: "input-group" do %>
<div>Comment Status</div>
<%= select f, :comment_status, ["open", "closed"] %>
<% end %>
<%= label f, :ping_status, class: "input-group" do %>
<div>Ping Status</div>
<%= select f, :ping_status, ["open", "closed"] %>
<% end %>
<%= label f, :post_password, class: "input-group" do %>
<div>Post Password</div>
<%= text_input f, :post_password %>
<% end %>
<%= label f, :post_name, class: "input-group" do %>
<div>Slug</div>
<%= text_input f, :post_name %>
<% end %>
<%= label f, :post_order, class: "input-group" do %>
<div>Post Order</div>
<%= number_input f, :post_order %>
<% end %>
</div>
</div>
<%= hidden_input f, :post_type, value: @post_type %>
<% end %>
<%= if assigns[:posts] do %>
<%= form_for @changeset, Routes.posts_path(@conn, :delete, @posts), [method: :delete, id: "deleteForm"], fn _f -> %>
<% end %>
<% end %>

View file

@ -2,12 +2,12 @@
<div class="uHidden"> <div class="uHidden">
<h1 class="p-name"> <h1 class="p-name">
<%= link to: Routes.posts_path(@conn, :show, @post), class: "u-url" do %> <%= link to: Routes.posts_path(@conn, :show, @post), class: "u-url" do %>
<%= raw @post.post_title %> <%= raw @post.title %>
<% end %> <% end %>
</h1> </h1>
<%= post_topmatter(@conn, @post) %> <%= post_topmatter(@conn, @post) %>
</div> </div>
<div class="Article-content <%= if @post.post_format, do: @post.post_format.slug %> e-content"> <div class="Article-content <%= if @post.format, do: @post.format.slug %> e-content">
<%= render "thumb.html", post: @post, thumbs: @thumbs %> <%= render "thumb.html", post: @post, thumbs: @thumbs %>
<%= @post |> Content.Post.content_page(@page) |> process_content |> raw %> <%= @post |> Content.Post.content_page(@page) |> process_content |> raw %>
</div> </div>

View file

@ -3,17 +3,17 @@
<div class="ui main padded text container"> <div class="ui main padded text container">
<h1 class="ui header p-name"> <h1 class="ui header p-name">
<%= link to: Routes.posts_path(@conn, :show, post), class: "u-url" do %> <%= link to: Routes.posts_path(@conn, :show, post), class: "u-url" do %>
<%= raw post.post_title %> <%= raw post.title %>
<% end %> <% end %>
</h1> </h1>
<%= post_topmatter(@conn, post) %> <%= post_topmatter(@conn, post) %>
</div> </div>
<div class="Article-content <%= if post.post_format, do: post.post_format.slug %> e-content"> <div class="Article-content <%= if post.format, do: post.format.slug %> e-content">
<%= if authenticated_for_post?(@conn, post) do %> <%= if authenticated_for_post?(@conn, post) do %>
<%= render "thumb.html", post: post, thumbs: @thumbs %> <%= render "thumb.html", post: post, thumbs: @thumbs %>
<div class="Article-content-words" style="padding-bottom: 4em; padding-top: 4em;"> <div class="Article-content-words" style="padding-bottom: 4em; padding-top: 4em;">
<%= raw post |> Content.Post.content_page(1) |> Content.Post.before_more |> process_content |> raw %> <%= raw post |> Content.Post.content_page(1) |> Content.Post.before_more |> process_content |> raw %>
<%= if post.post_content =~ "<!--more-->" do %> <%= if post.content =~ "<!--more-->" do %>
<p> <p>
<%= link "Keep Reading", to: Routes.posts_path(@conn, :show, post) %> <%= link "Keep Reading", to: Routes.posts_path(@conn, :show, post) %>
</p> </p>

View file

@ -1 +0,0 @@
<%= render "form.html", Map.put(assigns, :action, Routes.posts_path(@conn, :create)) %>

View file

@ -8,23 +8,23 @@
<%= changeset_error_block(@comment_changeset) %> <%= changeset_error_block(@comment_changeset) %>
<div class="ui stacked left aligned segment"> <div class="ui stacked left aligned segment">
<%= form_for @comment_changeset, Routes.comment_path(@conn, :create), [class: "ui form"], fn f -> %> <%= form_for @comment_changeset, Routes.comment_path(@conn, :create), [class: "ui form"], fn f -> %>
<%= hidden_input f, :comment_parent %> <%= hidden_input f, :parent %>
<%= hidden_input f, :comment_post_id %> <%= hidden_input f, :post_id %>
<%= label f, :comment_author do %> <%= label f, :author do %>
Your Name Your Name
<%= text_input f, :comment_author %> <%= text_input f, :author %>
<% end %> <% end %>
<%= label f, :comment_author_email do %> <%= label f, :author_email do %>
Your Email Your Email
<%= email_input f, :comment_author_email %> <%= email_input f, :author_email %>
<% end %> <% end %>
<%= label f, :comment_author_url do %> <%= label f, :author_url do %>
Your Website Your Website
<%= text_input f, :comment_author_url %> <%= text_input f, :author_url %>
<% end %> <% end %>
<%= label f, :comment_content do %> <%= label f, :content do %>
Comment Comment
<%= textarea f, :comment_content %> <%= textarea f, :content %>
<% end %> <% end %>
<div class="form-group"> <div class="form-group">
<%= submit "Leave My Comment", class: "ui fluid large teal submit button" %> <%= submit "Leave My Comment", class: "ui fluid large teal submit button" %>

View file

@ -2,12 +2,12 @@
<div class="ui main padded text container"> <div class="ui main padded text container">
<h1 class="ui header p-name"> <h1 class="ui header p-name">
<%= link to: Routes.posts_path(@conn, :show, @post), class: "u-url" do %> <%= link to: Routes.posts_path(@conn, :show, @post), class: "u-url" do %>
<%= raw @post.post_title %> <%= raw @post.title %>
<% end %> <% end %>
</h1> </h1>
<%= post_topmatter(@conn, @post) %> <%= post_topmatter(@conn, @post) %>
</div> </div>
<div class="ui text container <%= if @post.post_format, do: @post.post_format.slug %> e-content" style="padding-bottom: 4em; padding-top: 4em;"> <div class="ui text container <%= if @post.format, do: @post.format.slug %> e-content" style="padding-bottom: 4em; padding-top: 4em;">
<%= render "thumb.html", post: @post, thumbs: @thumbs %> <%= render "thumb.html", post: @post, thumbs: @thumbs %>
<%= @post |> Content.Post.content_page(@page) |> process_content |> raw %> <%= @post |> Content.Post.content_page(@page) |> process_content |> raw %>
<div class="ui grid"> <div class="ui grid">

View file

@ -4,7 +4,7 @@
<%= for post <- @posts do %> <%= for post <- @posts do %>
<li> <li>
<%= link to: Routes.posts_path(@conn, :show, post) do %> <%= link to: Routes.posts_path(@conn, :show, post) do %>
<%= raw post.post_title %> <%= raw post.title %>
<% end %> <% end %>
</li> </li>
<% end %> <% end %>

View file

@ -55,8 +55,8 @@ defmodule Content.FeedsView do
</h4> </h4>
<h5> <h5>
<%= link to: Routes.posts_path(conn, :show, post) do %> <%= link to: Routes.posts_path(conn, :show, post) do %>
<time class="dt-published" datetime="<%= post.post_date %>"> <time class="dt-published" datetime="<%= post.post %>">
<%= post.post_date |> Timex.format!("%F", :strftime) %> <%= post.post |> Timex.format!("%F", :strftime) %>
</time> </time>
<% end %> <% end %>
</h5> </h5>
@ -65,6 +65,6 @@ defmodule Content.FeedsView do
end end
def unauthenticated_post?(_conn, post) do def unauthenticated_post?(_conn, post) do
post.post_password == nil || String.length(post.post_password) == 0 post.password == nil || String.length(post.password) == 0
end end
end end

View file

@ -12,20 +12,20 @@ defmodule Content.LayoutView do
end end
def title(Content.PostsView, "show.html", assigns) do def title(Content.PostsView, "show.html", assigns) do
(assigns.post.post_title |> HtmlSanitizeEx.strip_tags()) <> " | " <> title(nil, nil, nil) (assigns.post.title |> HtmlSanitizeEx.strip_tags()) <> " | " <> title(nil, nil, nil)
end end
def title(_, _, _) do def title(_, _, _) do
case Options.get("blogname") do case Options.get("blogname") do
opt = %Option{} -> opt = %Option{} ->
opt.option_value opt.value
_ -> _ ->
I18n.t! "en", "site.title" I18n.t! "en", "site.title"
end end
end end
def excerpt(Content.PostsView, "show.html", assigns) do def excerpt(Content.PostsView, "show.html", assigns) do
assigns.post.post_excerpt assigns.post.excerpt
|> HtmlSanitizeEx.strip_tags() |> HtmlSanitizeEx.strip_tags()
end end
@ -36,7 +36,7 @@ defmodule Content.LayoutView do
def excerpt(_, _, _) do def excerpt(_, _, _) do
case Options.get("blogdescription") do case Options.get("blogdescription") do
opt = %Option{} -> opt = %Option{} ->
opt.option_value opt.value
_ -> _ ->
I18n.t! "en", "site.excerpt" I18n.t! "en", "site.excerpt"
end end

View file

@ -17,7 +17,7 @@ defmodule Content.PostsView do
end end
def authenticated_for_post?(conn, post) do def authenticated_for_post?(conn, post) do
post.post_password == nil || String.length(post.post_password) == 0 || get_session(conn, :post_password) == post.post_password post.password == nil || String.length(post.password) == 0 || get_session(conn, :post_password) == post.password
end end
def gravatar_url_for_email(email) do def gravatar_url_for_email(email) do
@ -33,15 +33,15 @@ defmodule Content.PostsView do
def comment_changeset_for_post(%Post{} = post) do def comment_changeset_for_post(%Post{} = post) do
%Comment{ %Comment{
comment_post_id: post.id post_id: post.id
} }
|> Comment.changeset() |> Comment.changeset()
end end
def comment_changeset_for_parent(%Comment{} = comment) do def comment_changeset_for_parent(%Comment{} = comment) do
%Comment{ %Comment{
comment_parent: comment.comment_id, parent: comment.id,
comment_post_id: comment.comment_post_id post_id: comment.post_id
} }
|> Comment.changeset() |> Comment.changeset()
end end
@ -83,8 +83,8 @@ defmodule Content.PostsView do
<span><%= author.display_name %></span> <span><%= author.display_name %></span>
&#8226; &#8226;
<%= link to: Routes.posts_path(conn, :show, post) do %> <%= link to: Routes.posts_path(conn, :show, post) do %>
<time class="dt-published" datetime="<%= post.post_date %>"> <time class="dt-published" datetime="<%= post.date %>">
<%= post.post_date |> Timex.format!("%F", :strftime) %> <%= post.date |> Timex.format!("%F", :strftime) %>
</time> </time>
<% end %> <% end %>
<% end %> <% end %>

View file

@ -0,0 +1,108 @@
defmodule Content.Repo.Migrations.CreateSchema do
use Ecto.Migration
def change do
create table("commentmeta") do
add :comment_id, :integer
add :key, :string
add :value, :text
end
create table("comments") do
add :post_id, :integer
add :author, :text
add :author_email, :text
add :author_url, :text
add :author_IP, :text
add :date, :naive_datetime
add :date_gmt, :naive_datetime
add :content, :text
add :karma, :integer
add :approved, :text
add :agent, :text
add :type, :text
add :parent, :integer
add :user_id, :integer
end
create table("links") do
add :url, :text
add :name, :text
add :image, :text
add :target, :text
add :description, :text
add :visible, :text
add :owner, :integer
add :rating, :integer
add :updated, :naive_datetime
add :rel, :text
add :rss, :text
add :notes, :text
end
create table("options") do
add :name, :text
add :autoload, :text
add :value, :text
end
create table("postmeta") do
add :post_id, :integer
add :key, :string
add :value, :text
end
create table("posts") do
add :author_id, :integer
add :date, :naive_datetime
add :date_gmt, :naive_datetime
add :content, :text
add :title, :string
add :excerpt, :string
add :status, :text
add :comment_status, :text
add :ping_status, :text
add :password, :text
add :name, :text
add :to_ping, :string
add :pinged, :string
add :modified, :naive_datetime
add :modified_gmt, :naive_datetime
add :content_filtered, :text
add :parent, :integer
add :guid, :text
add :menu_order, :integer
add :type, :text
add :mime_type, :text
add :comment_count, :integer, default: 0
end
create unique_index(:posts, ["name"])
create table("term_relationships", primary_key: false) do
add :object_id, :serial, primary_key: true
add :term_taxonomy_id, :integer, [:primary_key]
add :term_order, :integer
end
create table("term_taxonomy") do
add :term_id, :integer
add :taxonomy, :text
add :description, :text
add :parent, :integer
add :count, :integer
end
create table("termmeta") do
add :term_id, :integer
add :key, :string
add :value, :text
end
create table("terms") do
add :name, :text
add :slug, :text
add :term_group, :integer
end
end
end

View file

@ -1,120 +0,0 @@
defmodule Content.Repo.Migrations.CreateWpSchema do
use Ecto.Migration
def change do
create table("wp_commentmeta", primary_key: false) do
add :meta_id, :serial, primary_key: true
add :comment_id, :integer
add :meta_key, :text
end
create table("wp_comments", primary_key: false) do
add :comment_id, :serial, primary_key: true
add :comment_post_id, :integer
add :comment_author, :text
add :comment_author_email, :text
add :comment_author_url, :text
add :comment_author_IP, :text
add :comment_date, :naive_datetime
add :comment_date_gmt, :naive_datetime
add :comment_content, :string
add :comment_karma, :integer
add :comment_approved, :text
add :comment_agent, :text
add :comment_type, :text
add :comment_parent, :integer
add :user_id, :integer
end
create table("wp_links", primary_key: false) do
add :link_id, :serial, primary_key: true
add :link_url, :text
add :link_name, :text
add :link_image, :text
add :link_target, :text
add :link_description, :text
add :link_visible, :text
add :link_owner, :integer
add :link_rating, :integer
add :link_updated, :naive_datetime
add :link_rel, :text
add :link_rss, :text
end
create table("wp_options", primary_key: false) do
add :option_id, :serial, primary_key: true
add :option_name, :text
add :autoload, :text
add :option_value, :text
end
create table("wp_postmeta", primary_key: false) do
add :meta_id, :serial, primary_key: true
add :post_id, :integer
add :meta_key, :text
add :meta_value, :text
end
create table("wp_posts", primary_key: false) do
add :id, :serial, primary_key: true
add :post_author, :integer
add :post_date, :naive_datetime
add :post_date_gmt, :naive_datetime
add :post_content, :text
add :post_title, :string
add :post_excerpt, :string
add :post_status, :text
add :comment_status, :text
add :ping_status, :text
add :post_password, :text
add :post_name, :text
add :to_ping, :string
add :pinged, :string
add :post_modified, :naive_datetime
add :post_modified_gmt, :naive_datetime
add :post_content_filtered, :text
add :post_parent, :integer
add :guid, :text
add :menu_order, :integer
add :post_type, :text
add :post_mime_type, :text
add :comment_count, :integer
end
create table("wp_term_relationships", primary_key: false) do
add :object_id, :serial, primary_key: true
add :term_taxonomy_id, :integer, [:primary_key]
add :term_order, :integer
end
create table("wp_term_taxonomy", primary_key: false) do
add :term_taxonomy_id, :serial, primary_key: true
add :term_id, :integer
add :taxonomy, :text
add :description, :text
add :parent, :integer
add :count, :integer
end
create table("wp_termmeta", primary_key: false) do
add :meta_id, :serial, primary_key: true
add :term_id, :integer
add :meta_key, :text
add :meta_value, :text
end
create table("wp_terms", primary_key: false) do
add :term_id, :serial, primary_key: true
add :name, :text
add :slug, :text
add :term_group, :integer
end
create table("wp_usermeta", primary_key: false) do
add :umeta_id, :serial, primary_key: true
add :user_id, :integer
add :meta_key, :text
add :meta_value, :text
end
end
end

View file

@ -1,9 +0,0 @@
defmodule Content.Repo.Migrations.ChangeCommentContentToText do
use Ecto.Migration
def change do
alter table("wp_comments") do
modify :comment_content, :text
end
end
end

View file

@ -1,9 +0,0 @@
defmodule Content.Repo.Migrations.AddDefaultToCommentCount do
use Ecto.Migration
def change do
alter table("wp_posts") do
modify :comment_count, :integer, default: 0
end
end
end

View file

@ -1,9 +0,0 @@
defmodule Content.Repo.Migrations.AddMetaValueToWpCommentmetas do
use Ecto.Migration
def change do
alter table("wp_commentmeta") do
add :meta_value, :string
end
end
end

View file

@ -1,9 +0,0 @@
defmodule Content.Repo.Migrations.AddLinkNotesToWpLinks do
use Ecto.Migration
def change do
alter table("wp_links") do
add :link_notes, :string
end
end
end

View file

@ -1,7 +0,0 @@
defmodule Content.Repo.Migrations.AddUniqueIndexToPostName do
use Ecto.Migration
def change do
create unique_index(:wp_posts, ["post_name"])
end
end

View file

@ -5,12 +5,12 @@ defmodule Content.AttachmentTest do
@create_attrs %{ @create_attrs %{
id: 123, id: 123,
post_name: "my-attachment", name: "my-attachment",
post_title: "My Attachment", title: "My Attachment",
post_content: "", content: "",
post_status: "publish", status: "publish",
post_type: "attachment", type: "attachment",
post_date: "2018-01-01T00:00:00Z" date: "2018-01-01T00:00:00Z"
} }
def fixture(:wide_attachment) do def fixture(:wide_attachment) do
@ -18,8 +18,8 @@ defmodule Content.AttachmentTest do
{:ok, _meta} = {:ok, _meta} =
%Postmeta{ %Postmeta{
post_id: attachment.id, post_id: attachment.id,
meta_key: "_wp_attachment_metadata", key: "attachment_metadata",
meta_value: "a:2:{s:5:\"width\";i:640;s:6:\"height\";i:480;}" value: "a:2:{s:5:\"width\";i:640;s:6:\"height\";i:480;}"
} |> Repo.insert() } |> Repo.insert()
Content.Post Content.Post
@ -32,8 +32,8 @@ defmodule Content.AttachmentTest do
{:ok, _meta} = {:ok, _meta} =
%Postmeta{ %Postmeta{
post_id: attachment.id, post_id: attachment.id,
meta_key: "_wp_attachment_metadata", key: "attachment_metadata",
meta_value: "a:2:{s:5:\"width\";i:480;s:6:\"height\";i:640;}" value: "a:2:{s:5:\"width\";i:480;s:6:\"height\";i:640;}"
} |> Repo.insert() } |> Repo.insert()
Content.Post Content.Post
|> preload([:metas]) |> preload([:metas])

View file

@ -7,8 +7,8 @@ defmodule Content.CommentmetaTest do
%Commentmeta{} %Commentmeta{}
|> Commentmeta.changeset(%{ |> Commentmeta.changeset(%{
comment_id: 123, comment_id: 123,
meta_key: "testcommentmeta", key: "testcommentmeta",
meta_value: "some value", value: "some value",
}) })
|> Repo.insert!() |> Repo.insert!()
end end

View file

@ -6,19 +6,19 @@ defmodule Content.CommentsTest do
def fixture(:parent_comment) do def fixture(:parent_comment) do
%Comment{ %Comment{
comment_id: 123, id: 123,
comment_content: "Hello world", content: "Hello world",
comment_post_id: 456, post_id: 456,
} }
|> Repo.insert!() |> Repo.insert!()
end end
def fixture(:child_comment) do def fixture(:child_comment) do
%Comment{ %Comment{
comment_id: 456, id: 456,
comment_parent: 123, parent: 123,
comment_content: "Hello back", content: "Hello back",
comment_post_id: 456, post_id: 456,
} }
|> Repo.insert!() |> Repo.insert!()
end end
@ -28,14 +28,14 @@ defmodule Content.CommentsTest do
parent = fixture(:parent_comment) parent = fixture(:parent_comment)
kid = fixture(:child_comment) kid = fixture(:child_comment)
kids = Comments.children(parent.comment_id, Comments.list_comments) kids = Comments.children(parent.id, Comments.list_comments)
assert kids == [kid] assert kids == [kid]
end end
test "returns an empty list if the comment has no children " do test "returns an empty list if the comment has no children " do
parent = fixture(:parent_comment) parent = fixture(:parent_comment)
kids = Comments.children(parent.comment_id, Comments.list_comments) kids = Comments.children(parent.id, Comments.list_comments)
assert kids == [] assert kids == []
end end
end end
@ -45,8 +45,8 @@ defmodule Content.CommentsTest do
changeset = fixture(:parent_comment) |> Comments.change_comment changeset = fixture(:parent_comment) |> Comments.change_comment
changed_value = changed_value =
changeset changeset
|> Changeset.put_change(:comment_content, "woops") |> Changeset.put_change(:content, "woops")
|> Changeset.get_change(:comment_content) |> Changeset.get_change(:content)
assert changed_value == "woops" assert changed_value == "woops"
end end
end end

View file

@ -1,141 +0,0 @@
defmodule Content.MenuTest do
use Content.DataCase
alias Content.{Menu, Option, Post, Postmeta, Repo, Term, TermRelationship}
@theme_option %Option{
option_name: "test_theme",
option_value: "a:1:{s:18:\"nav_menu_locations\";a:1:{s:3:\"top\";i:13;}}"
}
@term_relationship %TermRelationship{
term_taxonomy_id: 13,
object_id: 123,
}
@top_nav_item %Post{
id: 123,
post_name: "home",
post_title: "Home",
post_content: "",
post_status: "publish",
post_type: "nav_item",
post_date: ~N"2018-01-01T00:00:00",
comment_status: "open",
}
@top_nav_metas [
%{
post_id: 123,
meta_key: "_menu_item_object_id",
meta_value: "456",
},
%{
post_id: 123,
meta_key: "_menu_item_object",
meta_value: "post",
},
%{
post_id: 123,
meta_key: "_menu_item_menu_item_parent",
meta_value: "0",
},
]
@category_nav_metas [
%{
post_id: 123,
meta_key: "_menu_item_object_id",
meta_value: "42",
},
%{
post_id: 123,
meta_key: "_menu_item_object",
meta_value: "category",
},
%{
post_id: 123,
meta_key: "_menu_item_menu_item_parent",
meta_value: "0",
},
]
@related_page %Post {
id: 456,
post_title: "Test Nav Home",
}
@related_category %Term{
term_id: 42,
name: "Test Category",
slug: "test-category",
}
def fixture(:option) do
@theme_option |> Repo.insert()
end
def fixture(:menu) do
{:ok, option} = fixture(:option)
{:ok, _term_relationship} = @term_relationship |> Repo.insert()
{:ok, _nav_item} = @top_nav_item |> Repo.insert()
{3, nil} = Repo.insert_all(Postmeta, @top_nav_metas)
{:ok, _post} = @related_page |> Repo.insert()
option
end
def fixture(:category_menu) do
{:ok, option} = fixture(:option)
{:ok, _term_relationship} = @term_relationship |> Repo.insert()
{:ok, _nav_item} = @top_nav_item |> Repo.insert()
{3, nil} = Repo.insert_all(Postmeta, @category_nav_metas)
{:ok, _category} = @related_category |> Repo.insert()
option
end
describe "get_menu_from_option_and_location" do
test "returns an empty if the menu is not present" do
fixture(:option)
assert Menu.get_menu_from_option_and_location("test_theme", "top") == []
end
test "returns items if the menu is present" do
fixture(:menu)
menu = Menu.get_menu_from_option_and_location("test_theme", "top")
refute menu == []
assert (menu |> Enum.at(0)) == %{
children: [],
parent_id: "0",
post_id: 123,
related_item: %{resource: "posts", slug: nil, title: "Test Nav Home"},
target_id: "456",
type: "post",
url: nil,
}
end
test "returns items if the menu has a category" do
fixture(:category_menu)
menu = Menu.get_menu_from_option_and_location("test_theme", "top")
refute menu == []
assert (menu |> Enum.at(0)) == %{
children: [],
parent_id: "0",
post_id: 123,
related_item: %{resource: "category", slug: "test-category", title: "Test Category"},
target_id: "42",
type: "category",
url: nil,
}
end
end
describe "put_menu_option" do
test "it can change the active menu in a position" do
fixture(:menu)
{:ok, _option} = Menu.put_menu_option("test_theme", "top", 7) |> Repo.update()
assert Menu.get_menu_from_option_and_location("test_theme", "top") == []
end
end
end

View file

@ -6,8 +6,8 @@ defmodule Content.OptionTest do
test "can save a new link" do test "can save a new link" do
%Option{} %Option{}
|> Option.changeset(%{ |> Option.changeset(%{
option_name: "test_up", name: "test_up",
option_value: "1", value: "1",
}) })
|> Repo.insert!() |> Repo.insert!()
end end

View file

@ -6,8 +6,8 @@ defmodule Content.OptionsTest do
def fixture(:option) do def fixture(:option) do
%Option{} %Option{}
|> Option.changeset(%{ |> Option.changeset(%{
option_name: "test_up", name: "test_up",
option_value: "1", value: "1",
}) })
|> Repo.insert!() |> Repo.insert!()
end end

View file

@ -7,8 +7,8 @@ defmodule Content.PostmetaTest do
%Postmeta{} %Postmeta{}
|> Postmeta.changeset(%{ |> Postmeta.changeset(%{
post_id: 123, post_id: 123,
meta_key: "testpostmeta", key: "testpostmeta",
meta_value: "some value", value: "some value",
}) })
|> Repo.insert!() |> Repo.insert!()
end end

View file

@ -6,34 +6,34 @@ defmodule Content.SlugsTest do
@create_attrs %{ @create_attrs %{
id: 123, id: 123,
post_name: "my-post", name: "my-post",
post_title: "My Post", title: "My Post",
post_content: "", content: "",
post_status: "publish", status: "publish",
post_type: "post", type: "post",
post_date: "2018-01-01T00:00:00Z" date: "2018-01-01T00:00:00Z"
} }
@dupe_title_attrs %{ @dupe_title_attrs %{
id: 456, id: 456,
post_title: "My Post", title: "My Post",
post_content: "", content: "",
post_status: "publish", status: "publish",
post_type: "post", type: "post",
post_date: "2018-01-01T00:00:00Z" date: "2018-01-01T00:00:00Z"
} }
describe "ensure_post_has_slug" do describe "ensure_post_has_slug" do
test "doesn't overwrite a set slug" do test "doesn't overwrite a set slug" do
new_post = new_post =
%Post{ %Post{
post_name: "a-set-slug" name: "a-set-slug"
} }
|> Post.changeset() |> Post.changeset()
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> Changeset.apply_changes() |> Changeset.apply_changes()
assert new_post.post_name == "a-set-slug" assert new_post.name == "a-set-slug"
end end
test "works even if the title is nil" do test "works even if the title is nil" do
@ -43,19 +43,19 @@ defmodule Content.SlugsTest do
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> Changeset.apply_changes() |> Changeset.apply_changes()
assert new_post.post_name |> String.length() > 0 assert new_post.name |> String.length() > 0
end end
test "sets a slug if the title is there" do test "sets a slug if the title is there" do
new_post = new_post =
%Post{ %Post{
post_title: "My NEW Post" title: "My NEW Post"
} }
|> Changeset.change(%{}) |> Changeset.change(%{})
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> Changeset.apply_changes() |> Changeset.apply_changes()
assert new_post.post_name == "my-new-post" assert new_post.name == "my-new-post"
end end
test "ensures uniqueness of the slug" do test "ensures uniqueness of the slug" do
@ -64,14 +64,14 @@ defmodule Content.SlugsTest do
new_post = new_post =
%Post{ %Post{
post_title: "MY POST" title: "MY POST"
} }
|> Changeset.change(%{}) |> Changeset.change(%{})
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> Changeset.apply_changes() |> Changeset.apply_changes()
assert new_post.post_name != og_post.post_name assert new_post.name != og_post.name
assert new_post.post_name == "my-post-1" assert new_post.name == "my-post-1"
end end
test "ensures uniqueness of the slug on update" do test "ensures uniqueness of the slug on update" do
@ -84,8 +84,8 @@ defmodule Content.SlugsTest do
|> Slugs.ensure_post_has_slug() |> Slugs.ensure_post_has_slug()
|> Changeset.apply_changes() |> Changeset.apply_changes()
assert new_post.post_name != og_post.post_name assert new_post.name != og_post.name
assert new_post.post_name == "my-post-1" assert new_post.name == "my-post-1"
end end
end end
end end

View file

@ -7,8 +7,8 @@ defmodule Content.TermmetaTest do
%Termmeta{} %Termmeta{}
|> Termmeta.changeset(%{ |> Termmeta.changeset(%{
term_id: 123, term_id: 123,
meta_key: "testtermmeta", key: "testtermmeta",
meta_value: "some value", value: "some value",
}) })
|> Repo.insert!() |> Repo.insert!()
end end

View file

@ -4,10 +4,10 @@ defmodule Content.CommentControllerTest do
alias Content.Comments alias Content.Comments
alias Content.Posts alias Content.Posts
@post_attrs %{id: 456, post_name: "blergh", post_status: "publish"} @post_attrs %{id: 456, name: "blergh", status: "publish"}
@create_attrs %{comment_id: 123, comment_content: "Hello world", comment_post_id: 456} @create_attrs %{id: 123, content: "Hello world", post_id: 456}
@update_attrs %{comment_id: 123, comment_content: "Goodbye", comment_post_id: 456} @update_attrs %{id: 123, content: "Goodbye", post_id: 456}
@invalid_attrs %{comment_id: 123, comment_content: "", comment_post_id: 456} @invalid_attrs %{id: 123, content: "", post_id: 456}
def fixture(:post) do def fixture(:post) do
{:ok, post} = Posts.create_posts(@post_attrs) {:ok, post} = Posts.create_posts(@post_attrs)

View file

@ -6,64 +6,52 @@ defmodule Content.PostsControllerTest do
@create_attrs %{ @create_attrs %{
id: 123, id: 123,
post_name: "my-post", name: "my-post",
post_title: "My Post", title: "My Post",
post_content: "Page One <!--nextpage--> Page Two", content: "Page One <!--nextpage--> Page Two",
post_status: "publish", status: "publish",
post_type: "post", type: "post",
post_date: "2018-01-01T00:00:00Z", date: "2018-01-01T00:00:00Z",
comment_status: "open",
}
@blog_post_attrs %{
id: 456,
post_name: "my-blog-post",
post_title: "My Blog Post",
post_content: "Page One <!--nextpage--> Page Two",
post_status: "publish",
post_type: "post",
post_date: "2018-01-01T00:00:00Z",
comment_status: "open", comment_status: "open",
} }
@preview_attrs %{ @preview_attrs %{
post_name: "my-post", name: "my-post",
post_title: "My Post", title: "My Post",
post_content: "", content: "",
post_status: "publish", status: "publish",
post_type: "post", type: "post",
post_date: "2018-01-01T00:00:00Z" date: "2018-01-01T00:00:00Z"
} }
@thumb_attrs %{ @thumb_attrs %{
id: 124, id: 124,
post_name: "my-thumb", name: "my-thumb",
post_title: "My Thumb", title: "My Thumb",
post_content: "", content: "",
post_status: "publish", status: "publish",
post_type: "attachment", type: "attachment",
post_date: "2018-01-01T00:00:00Z", date: "2018-01-01T00:00:00Z",
guid: "http://placekitten.com/200/300" guid: "http://placekitten.com/200/300"
} }
@attachment_attrs %{ @attachment_attrs %{
id: 123, id: 123,
post_name: "attachment.txt", name: "attachment.txt",
post_title: "", title: "",
post_content: "my text attachment" |> Base.encode64, content: "my text attachment" |> Base.encode64,
post_status: "publish", status: "publish",
post_type: "attachment", type: "attachment",
post_mime_type: "text/plain", mime_type: "text/plain",
post_date: "2018-01-01T00:00:00Z", date: "2018-01-01T00:00:00Z",
comment_status: "open", comment_status: "open",
} }
@update_attrs %{post_name: "my-post"}
@invalid_attrs %{post_status: "", post_type: "post"}
@post_category %Term{ @post_category %Term{
term_id: 42, id: 42,
name: "Test Category", name: "Test Category",
slug: "test-category", slug: "test-category",
} }
@post_category_taxonomy %TermTaxonomy{ @post_category_taxonomy %TermTaxonomy{
term_taxonomy_id: 64, id: 64,
term_id: 42, term_id: 42,
taxonomy: "category", taxonomy: "category",
description: "A test category", description: "A test category",
@ -78,15 +66,15 @@ defmodule Content.PostsControllerTest do
def fixture(:posts) do def fixture(:posts) do
{:ok, post} = Posts.create_posts(@create_attrs) {:ok, post} = Posts.create_posts(@create_attrs)
{:ok, thumb} = Posts.create_posts(@thumb_attrs) {:ok, thumb} = Posts.create_posts(@thumb_attrs)
{:ok, _meta} = %Content.Postmeta{post_id: post.id, meta_key: "_thumbnail_id", meta_value: Integer.to_string(thumb.id)} |> Repo.insert() {:ok, _meta} = %Content.Postmeta{post_id: post.id, key: "_thumbnail_id", value: Integer.to_string(thumb.id)} |> Repo.insert()
{:ok, _option} = %Content.Option{option_name: "sticky_posts", option_value: "a:1:{i:0;i:123;}"} |> Repo.insert() {:ok, _option} = %Content.Option{name: "sticky_posts", value: "a:1:{i:0;i:123;}"} |> Repo.insert()
post post
end end
def fixture(:single_post) do def fixture(:single_post) do
{:ok, post} = Posts.create_posts(@create_attrs) {:ok, post} = Posts.create_posts(@create_attrs)
{:ok, _comment} = %Comment{comment_post_id: post.id, comment_parent: 0, comment_date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)} |> Repo.insert() {:ok, _comment} = %Comment{post_id: post.id, parent: 0, date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)} |> Repo.insert()
post post
end end
@ -99,8 +87,8 @@ defmodule Content.PostsControllerTest do
def fixture(:front_post) do def fixture(:front_post) do
{:ok, post} = Posts.create_posts(@create_attrs) {:ok, post} = Posts.create_posts(@create_attrs)
{:ok, _option} = %Content.Option{option_name: "show_on_front", option_value: "page"} |> Repo.insert() {:ok, _option} = %Content.Option{name: "show_on_front", value: "page"} |> Repo.insert()
{:ok, _option} = %Content.Option{option_name: "page_on_front", option_value: post.id |> Integer.to_string(10)} |> Repo.insert() {:ok, _option} = %Content.Option{name: "page_on_front", value: post.id |> Integer.to_string(10)} |> Repo.insert()
post post
end end
@ -149,19 +137,19 @@ defmodule Content.PostsControllerTest do
test "shows the post", %{conn: conn, posts: posts} do test "shows the post", %{conn: conn, posts: posts} do
conn = get conn, Routes.posts_path(conn, :show, posts) conn = get conn, Routes.posts_path(conn, :show, posts)
assert html_response(conn, 200) =~ posts.post_title assert html_response(conn, 200) =~ posts.title
end end
test "shows the post by id", %{conn: conn, posts: posts} do test "shows the post by id", %{conn: conn, posts: posts} do
conn = get conn, Routes.posts_path(conn, :show, posts.id) conn = get conn, Routes.posts_path(conn, :show, posts.id)
assert html_response(conn, 200) =~ posts.post_title assert html_response(conn, 200) =~ posts.title
end end
test "shows the post with pagination", %{conn: conn, posts: posts} do test "shows the post with pagination", %{conn: conn, posts: posts} do
conn = get conn, Routes.paged_post_path(conn, :show, posts, "2") conn = get conn, Routes.paged_post_path(conn, :show, posts, "2")
assert html_response(conn, 200) =~ posts.post_title assert html_response(conn, 200) =~ posts.title
end end
end end
@ -179,7 +167,7 @@ defmodule Content.PostsControllerTest do
post = fixture(:single_post) post = fixture(:single_post)
conn = get conn, Routes.posts_path(conn, :show, "blog") conn = get conn, Routes.posts_path(conn, :show, "blog")
assert html_response(conn, 200) =~ post.post_title assert html_response(conn, 200) =~ post.title
end end
end end
@ -199,7 +187,7 @@ defmodule Content.PostsControllerTest do
post conn, Routes.posts_path(conn, :preview, %{"post" => @preview_attrs}) post conn, Routes.posts_path(conn, :preview, %{"post" => @preview_attrs})
end end
assert html_response(conn, 200) =~ @preview_attrs[:post_title] assert html_response(conn, 200) =~ @preview_attrs[:title]
end end
test "shows the post (put method)", %{conn: conn} do test "shows the post (put method)", %{conn: conn} do
@ -208,7 +196,7 @@ defmodule Content.PostsControllerTest do
put conn, Routes.posts_path(conn, :preview, %{"post" => @preview_attrs}) put conn, Routes.posts_path(conn, :preview, %{"post" => @preview_attrs})
end end
assert html_response(conn, 200) =~ @preview_attrs[:post_title] assert html_response(conn, 200) =~ @preview_attrs[:title]
end end
end end
@ -216,9 +204,4 @@ defmodule Content.PostsControllerTest do
post = fixture(:single_post) post = fixture(:single_post)
{:ok, posts: post} {:ok, posts: post}
end end
defp create_posts(_) do
posts = fixture(:posts)
{:ok, posts: posts}
end
end end

View file

@ -17,7 +17,8 @@ config :admin, Admin,
content: [ content: [
name: "Content", name: "Content",
resources: [ resources: [
post: [schema: Content.Post, admin: Content.PostAdmin, label: "Posts and Pages", id_column: :post_name] post: [schema: Content.Post, admin: Content.PostAdmin, label: "Posts and Pages", id_column: :name],
comment: [schema: Content.Comment, admin: Content.CommentAdmin],
] ]
] ]
] ]

View file

@ -10,4 +10,5 @@ $DIR_PATH/bootstrap
mix deps.get mix deps.get
mix npm.install mix npm.install
mix ecto.migrate mix ecto.create
mix ecto.migrate