prevent changing slugs of published posts
This commit is contained in:
parent
64aed0e5db
commit
2e7a81c121
2 changed files with 18 additions and 0 deletions
|
@ -13,6 +13,13 @@ defmodule Core.Posts.Post do
|
|||
changeset =
|
||||
case get_field(changeset, :kind) do
|
||||
:blog ->
|
||||
changeset =
|
||||
if not is_nil(get_field(changeset, :published_at)) and changed?(changeset, :slug) do
|
||||
add_error(changeset, :slug, "cannot change slug of published post")
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
validate_required(changeset, [:title, :slug])
|
||||
|
||||
:status ->
|
||||
|
|
|
@ -114,6 +114,17 @@ defmodule Test.Core.PostsTest do
|
|||
assert post.title == "A new title"
|
||||
end
|
||||
|
||||
@tag blog: :to_update
|
||||
test "prevents updating the slug of a published post", %{blogs: %{to_update: post}} do
|
||||
{:ok, post} = Core.Posts.publish_post(post)
|
||||
|
||||
assert {:error, %Ecto.Changeset{} = changeset} =
|
||||
Core.Posts.update_post(post, %{slug: "a-new-slug"})
|
||||
|
||||
refute changeset.valid?
|
||||
assert "cannot change slug of published post" in errors_on(changeset).slug
|
||||
end
|
||||
|
||||
@tag status: :to_update
|
||||
test "updates a status post", %{statuses: %{to_update: post}} do
|
||||
assert {:ok, %Schema.Post{} = post} = Core.Posts.update_post(post, %{body: "Hello, World!"})
|
||||
|
|
Loading…
Reference in a new issue