Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

[#67] Refactor ApiController to return JSON so thesis can redirect to the proper page. #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

silasjmatson
Copy link
Contributor

  • Return JSON on create/update/delete
  • Parameterize slug on save
  • Handle redirect when the slug may change
  • Change index on thesis_pages.slug to be unique

Fixes #67

Note that this changes the API a bit, but I do think it'll make some of the other issues a bit easier to tackle, namely #68

to the proper page.

* Change index on thesis_pages.slug to be unique
@@ -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)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Ecto.Multi here (future suggestion).

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree...can you file an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed in #113

inserted_at: page.inserted_at,
updated_at: page.updated_at
}
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we missing the description here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 slug in the response currently, at any rate.

def render("page.json", nil), do: %{}

def render("page_contents.json", page_contents)
when is_nil(page_contents), do: []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not put this on 18?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yulolimum I like keeping line-length under 80 characters -- I have a guide in my editor that shows when a line is over that length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha - i have mine set to 9999, just like most of my z-indexes
¯_(ツ)_/¯

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing

title: page.title,
description: page.description,
redirect_url: page.redirect_url,
page_contents: render("page_contents.json", assigns[:page_contents]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that page_contents will always be an empty list here, as we aren't assigning :page_contents in the controller -- this is only here for possible future need.

@silasjmatson
Copy link
Contributor Author

@jamonholmgren Note that this includes a new migration -- does hex have a way of doing a post-install/update message so we can alert the user to re-run mix thesis.install?

Copy link
Member

@jamonholmgren jamonholmgren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to chat about these changes sometime. Kind of busy with Chain React prep, but hit me up on Slack.

end

defp parameterize_slug(changeset) do
if {:data, slug} = fetch_field(changeset, :slug) do
Copy link
Member

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.

@@ -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}`.
Copy link
Member

Choose a reason for hiding this comment

The 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.


iex> import Thesis.Utilities
iex> slugify("Jamon is so cool!")
"Jamon-is-so-cool"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah

use Ecto.Migration

def up do
# Rreate old non-unique index
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean Remove not Rreate. :D

drop index(:thesis_pages, [:slug])

# Create a Unique Index
create unique_index(:thesis_pages, [:slug])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail in code bases that already include duplicate slugs. We should probably fail with a friendly error here. You can detect duplicates or just catch the error and fail gracefully (recommended).

@jamonholmgren
Copy link
Member

@silasjmatson I'll update this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants