Skip to content

Commit

Permalink
Rename "datetime" to "published_at"
Browse files Browse the repository at this point in the history
  • Loading branch information
qcam committed May 16, 2020
1 parent 6527ecd commit f0a2117
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ All posts in Nabo should follow this format:
{
"title": "Welcome to Nabo",
"slug": "welcome",
"datetime": "2017-01-01T00:00:00Z"
"published_at": "2017-01-01T00:00:00Z"
}
---
This is the excerpt of the post in markdown
Expand Down
22 changes: 11 additions & 11 deletions lib/mix/tasks/nabo.gen.post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ defmodule Mix.Tasks.Nabo.Gen.Post do
case OptionParser.parse(args, switches: switches, aliases: aliases) do
{options, [slug], _invalid} ->
root_path = Keyword.get(options, :path, "priv/posts/")
datetime = DateTime.utc_now()
published_at = DateTime.utc_now()
path = Path.relative_to(root_path, Mix.Project.app_path)
file = Path.join(path, "#{format_datetime(datetime)}_#{slug}.md")
create_file(file, post_template(slug: slug, datetime: datetime))
file = Path.join(path, "#{format_datetime(published_at)}_#{slug}.md")
create_file(file, post_template(slug: slug, published_at: published_at))

_ ->
Mix.raise "expected nabo.gen.post to receive post slug, " <>
"got: #{inspect(Enum.join(args, " "))}"
end
end

defp format_datetime(datetime) do
defp format_datetime(published_at) do
[
datetime.year,
pad_string(datetime.month),
pad_string(datetime.day),
pad_string(datetime.hour),
pad_string(datetime.minute),
pad_string(datetime.second),
published_at.year,
pad_string(published_at.month),
pad_string(published_at.day),
pad_string(published_at.hour),
pad_string(published_at.minute),
pad_string(published_at.second),
] |> Enum.join("")
end

Expand All @@ -58,7 +58,7 @@ defmodule Mix.Tasks.Nabo.Gen.Post do
{
"title": "",
"slug": "<%= @slug %>",
"datetime": "<%= DateTime.to_iso8601(@datetime) %>"
"published_at": "<%= DateTime.to_iso8601(@published_at) %>"
}
---
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/nabo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Nabo do
{
"title": "First Nabo post",
"slug": "first-post",
"datetime": "2017-01-01T00:00:00Z"
"published_at": "2017-01-01T00:00:00Z"
}
---
This is my first blog post created with Nabo.
Expand Down
2 changes: 1 addition & 1 deletion lib/nabo/metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Nabo.Metadata do
defstruct [
:slug,
:title,
:datetime,
:published_at,
:draft?,
:extras
]
Expand Down
4 changes: 2 additions & 2 deletions lib/nabo/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Nabo.Parser do
Given a raw post like this.
```
title,slug,datetime,draft
title,slug,published_at,draft
Nabo,nabo,"Monday, 15-Aug-2005 15:52:01 UTC",false
---
This is a post body
Expand All @@ -37,7 +37,7 @@ defmodule Nabo.Parser do
metadata = %Nabo.Metadata{
title: data["title"],
slug: data["slug"],
datetime: data["datetime"],
published_at: data["published_at"],
draft?: data["draft"]
}
Expand Down
20 changes: 10 additions & 10 deletions lib/nabo/parser/front.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ defmodule Nabo.Parser.Front do
{:ok, json} ->
with {:ok, title} <- build_title(json),
{:ok, slug} <- build_slug(json),
{:ok, datetime} <- build_datetime(json),
{:ok, published_at} <- build_published_at(json),
{:ok, draft?} <- build_draft(json) do
metadata = %Metadata{
title: title,
slug: slug,
datetime: datetime,
published_at: published_at,
draft?: draft?,
extras: json
}
Expand Down Expand Up @@ -43,18 +43,18 @@ defmodule Nabo.Parser.Front do
end
end

defp build_datetime(json) do
case Map.fetch(json, "datetime") do
defp build_published_at(json) do
case Map.fetch(json, "published_at") do
:error ->
{:error, "\"datetime\" has to be set"}
{:error, "\"published_at\" has to be set"}

{:ok, datetime} ->
case DateTime.from_iso8601(datetime) do
{:ok, datetime, _} ->
{:ok, datetime}
{:ok, published_at} ->
case DateTime.from_iso8601(published_at) do
{:ok, published_at, _} ->
{:ok, published_at}

{:error, _reason} ->
{:error, "\"datetime\" has to be in ISO-8601 format, got: #{inspect(datetime)}"}
{:error, "\"published_at\" has to be in ISO-8601 format, got: #{inspect(published_at)}"}
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/nabo/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Nabo.Post do
{
"title": "Hello World",
"slug": "hello-world",
"datetime": "2017-01-01T00:00:00Z"
"published_at": "2017-01-01T00:00:00Z"
}
---
Welcome to my blog!
Expand All @@ -35,7 +35,7 @@ defmodule Nabo.Post do
{
"title": "Hello World",
"slug": "hello-world",
"datetime": "2017-01-01T00:00:00Z"
"published_at": "2017-01-01T00:00:00Z"
}
---
### Hello there!
Expand All @@ -49,7 +49,7 @@ defmodule Nabo.Post do
defstruct [
:title,
:slug,
:datetime,
:published_at,
:draft?,
:excerpt,
:excerpt_html,
Expand All @@ -64,7 +64,7 @@ defmodule Nabo.Post do
@type t() :: %__MODULE__{
body: String.t,
body_html: String.t,
datetime: DateTime.t,
published_at: DateTime.t,
draft?: boolean,
reading_time: Float.t,
excerpt: String.t,
Expand All @@ -88,7 +88,7 @@ defmodule Nabo.Post do
post
|> Map.put(:title, metadata.title)
|> Map.put(:slug, metadata.slug)
|> Map.put(:datetime, metadata.datetime)
|> Map.put(:published_at, metadata.published_at)
|> Map.put(:draft?, metadata.draft?)
|> Map.put(:metadata, metadata.extras)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/nabo/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ defmodule Nabo.Repo do
end

def order_by_datetime(posts) do
Enum.sort(posts, &(DateTime.compare(&1.datetime, &2.datetime) == :gt))
Enum.sort(posts, &(DateTime.compare(&1.published_at, &2.published_at) == :gt))
end

def exclude_draft(posts) do
Enum.reject(posts, & &1.draft?)
end

def filter_published(posts, datetime \\ DateTime.utc_now()) do
Enum.filter(posts, &(DateTime.compare(&1.datetime, datetime) == :lt))
def filter_published(posts, published_at \\ DateTime.utc_now()) do
Enum.filter(posts, &(DateTime.compare(&1.published_at, published_at) == :lt))
end
end
end
Expand Down Expand Up @@ -195,7 +195,7 @@ defmodule Nabo.Repo do
posts = MyRepo.all() |> MyRepo.filter_published()
"""
@callback filter_published(posts :: [Nabo.Post.t()], datetime :: DateTime.t()) :: [
@callback filter_published(posts :: [Nabo.Post.t()], published_at :: DateTime.t()) :: [
Nabo.Post.t()
]

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/draft-post.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Draft Post",
"slug": "draft-post",
"datetime": "2017-01-01T00:00:00Z",
"published_at": "2017-01-01T00:00:00Z",
"draft": true
}
---
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/last-year.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Last Year",
"slug": "last-year",
"datetime": "2016-01-01T00:00:00Z"
"published_at": "2016-01-01T00:00:00Z"
}
---
This is the post for last year
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/next-year.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Next Year",
"slug": "next-year",
"datetime": "2018-01-01T00:00:00Z"
"published_at": "2018-01-01T00:00:00Z"
}
---
This is the post for next year
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/this-year-0101.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "This year 01-01",
"slug": "this-year-0101",
"datetime": "2017-01-01T00:00:00Z"
"published_at": "2017-01-01T00:00:00Z"
}
---
This is the post for 01-01
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/this-year-0202.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "This year 02-02",
"slug": "this-year-0202",
"datetime": "2017-02-02T00:00:00Z"
"published_at": "2017-02-02T00:00:00Z"
}
---
This is the post for 02-02
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/posts/this-year-0303.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "This year 03-03",
"slug": "this-year-0303",
"datetime": "2017-03-03T00:00:00Z"
"published_at": "2017-03-03T00:00:00Z"
}
---
### Welcome!
Expand Down
4 changes: 2 additions & 2 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Nabo.IntegrationTest do

assert post.title == "This year 02-02"
assert post.slug == "this-year-0202"
assert post.datetime == DateTime.from_naive!(~N[2017-02-02 00:00:00], "Etc/UTC")
assert post.published_at == DateTime.from_naive!(~N[2017-02-02 00:00:00], "Etc/UTC")
refute post.draft?
assert post.excerpt == "This is the post for 02-02"
assert post.excerpt_html == "<p>This is the post for 02-02</p>\n"
Expand All @@ -23,7 +23,7 @@ defmodule Nabo.IntegrationTest do

assert post.title == "This year 01-01"
assert post.slug == "this-year-0101"
assert post.datetime == DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC")
assert post.published_at == DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC")
refute post.draft?
assert post.excerpt == "This is the post for 01-01"
assert post.excerpt_html == "<p>This is the post for 01-01</p>\n"
Expand Down
14 changes: 7 additions & 7 deletions test/nabo/compiler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Nabo.CompilerTest do
{:ok, %Nabo.Metadata{
title: "Incompetent Title",
slug: "incompetent-slug",
datetime: DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC"),
published_at: DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC"),
draft?: false
}}
end
Expand All @@ -31,7 +31,7 @@ defmodule Nabo.CompilerTest do

test "compile/2 with default parsers" do
raw_post = """
{"title":"Title","slug":"slug","datetime":"2017-01-01T01:02:03Z","draft":true}
{"title":"Title","slug":"slug","published_at":"2017-01-01T01:02:03Z","draft":true}
---
This is the _excerpt_.
---
Expand All @@ -41,7 +41,7 @@ defmodule Nabo.CompilerTest do
assert {:ok, post} = compile(raw_post, [])
assert post.title == "Title"
assert post.slug == "slug"
assert post.datetime == DateTime.from_naive!(~N[2017-01-01 01:02:03], "Etc/UTC")
assert post.published_at == DateTime.from_naive!(~N[2017-01-01 01:02:03], "Etc/UTC")
assert post.excerpt == "This is the _excerpt_."
assert post.excerpt_html == "<p>This is the <em>excerpt</em>.</p>\n"
assert post.body == "This is the **BODY**.\n"
Expand All @@ -50,7 +50,7 @@ defmodule Nabo.CompilerTest do

test "compile/2 with custom split pattern" do
raw_post = """
{"title":"Title","slug":"slug","datetime":"2017-01-01T01:02:03Z","draft":true}
{"title":"Title","slug":"slug","published_at":"2017-01-01T01:02:03Z","draft":true}
<<----->>
This is the _excerpt_.
<<----->>
Expand All @@ -60,7 +60,7 @@ defmodule Nabo.CompilerTest do
assert {:ok, post} = compile(raw_post, [split_pattern: "<<----->>"])
assert post.title == "Title"
assert post.slug == "slug"
assert post.datetime == DateTime.from_naive!(~N[2017-01-01 01:02:03], "Etc/UTC")
assert post.published_at == DateTime.from_naive!(~N[2017-01-01 01:02:03], "Etc/UTC")
assert post.excerpt == "\nThis is the _excerpt_.\n"
assert post.excerpt_html == "<p>This is the <em>excerpt</em>.</p>\n"
assert post.body == "\nThis is the **BODY**.\n"
Expand All @@ -69,7 +69,7 @@ defmodule Nabo.CompilerTest do

test "compile/2 with custom parsers" do
raw_post = """
{"title":"Title","slug":"slug","datetime":"2017-01-01T01:02:03Z","draft":true}
{"title":"Title","slug":"slug","published_at":"2017-01-01T01:02:03Z","draft":true}
---
This is the _excerpt_.
---
Expand All @@ -84,7 +84,7 @@ defmodule Nabo.CompilerTest do
assert {:ok, post} = compile(raw_post, options)
assert post.title == "Incompetent Title"
assert post.slug == "incompetent-slug"
assert post.datetime == DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC")
assert post.published_at == DateTime.from_naive!(~N[2017-01-01 00:00:00], "Etc/UTC")
assert post.excerpt == "This is the _excerpt_."
assert post.excerpt_html == "This IS NOT the _excerpt_."
assert post.body == "This is the **BODY**.\n"
Expand Down
Loading

0 comments on commit f0a2117

Please sign in to comment.