-
Notifications
You must be signed in to change notification settings - Fork 62
[#67] Refactor ApiController to return JSON so thesis can redirect to the proper page. #111
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Example.Repo.Migrations.AddAUniqueIndexOnSlug do | ||
@moduledoc false | ||
use Ecto.Migration | ||
|
||
def up do | ||
# Rreate old non-unique index | ||
drop index(:thesis_pages, [:slug]) | ||
|
||
# Create a Unique Index | ||
create unique_index(:thesis_pages, [:slug]) | ||
end | ||
|
||
def down do | ||
# Remove unique index | ||
drop unique_index(:thesis_pages, :slug) | ||
|
||
# Create old non-unique index | ||
create index(:thesis_pages, [:slug]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,8 +84,11 @@ defmodule Thesis.EctoStore do | |
""" | ||
def update(page_params, contents_params) do | ||
page = save_page(page_params) | ||
save_page_contents(page, contents_params) | ||
:ok | ||
|
||
case save_page_contents(page, contents_params) do | ||
:ok -> {:ok, page} | ||
:error -> {:error, page} | ||
end | ||
end | ||
|
||
@doc """ | ||
|
@@ -94,7 +97,7 @@ defmodule Thesis.EctoStore do | |
def delete(%{"slug" => slug}) do | ||
page = page(slug) | ||
repo.delete!(page) | ||
:ok | ||
{:ok, page} | ||
end | ||
|
||
defp save_page(%{"slug" => slug} = page_params) do | ||
|
@@ -108,8 +111,8 @@ defmodule Thesis.EctoStore do | |
preloaded_contents = page_contents(page) | ||
|
||
contents_params | ||
|> Enum.map(fn(x) -> content_changeset(x, page, preloaded_contents) end) | ||
|> Enum.each(fn(x) -> repo.insert_or_update!(x) end) | ||
|> Enum.map(&(content_changeset(&1, page, preloaded_contents))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree...can you file an issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed in #113 |
||
|> Enum.each(&(repo.insert_or_update!(&1))) | ||
|
||
:ok | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,14 @@ defmodule Thesis.Store do | |
|
||
@doc """ | ||
Updates the given page (identified by its slug) with the given map of | ||
string keys and Thesis.PageContent structs. Returns `:ok`. | ||
string keys and Thesis.PageContent structs. Returns tuple `{:ok, page}`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are breaking changes. But I think we should do it now, for sure. It'll be harder to change later. |
||
|
||
update(%{"slug" => "/"}, %{"Heading" => "My Heading Content"}) | ||
""" | ||
@callback update(%{String.t => String.t}, map) :: atom | ||
@callback update(%{String.t => String.t}, map) :: {atom, Thesis.Page.t} | ||
|
||
@doc """ | ||
Deletes the given page (identified by its slug). Returns `:ok`. | ||
Deletes the given page (identified by its slug). Returns tuple `{:ok, page}`. | ||
|
||
delete(%{"slug" => "/asdf"}) | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,36 @@ defmodule Thesis.Utilities do | |
|
||
|
||
@doc """ | ||
Removes special characters, keeps dashes and underscores, and replaces spaces | ||
with dashes. Also downcases the entire string. | ||
Slugifies the url and then removes slashes | ||
|
||
iex> import Thesis.Utilities | ||
iex> parameterize("Jamon is so cool!") | ||
"jamon-is-so-cool" | ||
iex> parameterize("%#d50SDF dfsJ FDS lkdsf f dfka a") | ||
iex> parameterize("%#d50SDF dfsJ FDS / lkdsf f dfka a") | ||
"d50sdf-dfsj-fds--lkdsf-f-dfka---a" | ||
""" | ||
def parameterize(str) do | ||
str = Regex.replace(~r/[^a-z0-9\-\s\.]/i, str, "") | ||
Regex.split(~r/\%20|\s/, str) | ||
|> Enum.join("-") | ||
str | ||
|> slugify | ||
|> String.replace(~r/\//i, "") | ||
|> String.downcase | ||
end | ||
|
||
@doc """ | ||
Removes special characters, keeps dashes and underscores, and replaces spaces | ||
with dashes. | ||
|
||
iex> import Thesis.Utilities | ||
iex> slugify("Jamon is so cool!") | ||
"Jamon-is-so-cool" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
iex> slugify("%#d50SDF dfsJ FDS/lkdsf f dfka a") | ||
"d50SDF-dfsJ-FDS/lkdsf-f-dfka---a" | ||
""" | ||
def slugify(str) do | ||
str = Regex.replace(~r/[^A-z0-9\-\s\.\/]/i, str, "") | ||
Regex.split(~r/\%20|\s/, str) | ||
|> Enum.join("-") | ||
end | ||
@doc """ | ||
Generates a random string of letters of a given length. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Thesis.ApiView do | ||
use Thesis.View, :view | ||
|
||
|
||
def render("page.json", %{thesis_page: page} = assigns) do | ||
%{ | ||
id: page.id, | ||
slug: page.slug, | ||
title: page.title, | ||
description: page.description, | ||
redirect_url: page.redirect_url, | ||
page_contents: render("page_contents.json", assigns[:page_contents]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that |
||
inserted_at: page.inserted_at, | ||
updated_at: page.updated_at | ||
} | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we missing the description here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like it -- note that the javascript only makes use of the |
||
def render("page.json", nil), do: %{} | ||
|
||
def render("page_contents.json", nil), do: [] | ||
def render("page_contents.json", page_contents) when is_list(page_contents) do | ||
Enum.map(page_contents, fn(page_content) -> | ||
render("page_content.json", page_content) | ||
end) | ||
end | ||
|
||
def render("page_content.json", page_content) do | ||
%{ | ||
id: page_content.id, | ||
page_id: page_content.page_id, | ||
name: page_content.name, | ||
content: page_content.content, | ||
content_type: page_content.content_type, | ||
meta: page_content.meta, | ||
inserted_at: page_content.inserted_at, | ||
updated_at: page_content.updated_at | ||
} | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change this to a case statement? For some reason I like that better in this case, like you did in ecto_store.