Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor(macros): clean up #389

Merged
merged 2 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions lib/groupher_server/cms/helper/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule GroupherServer.CMS.Helper.Macros do
import Helper.Utils, only: [get_config: 2]

alias GroupherServer.{CMS, Accounts}

alias Accounts.Model.User

alias CMS.Model.{
Expand All @@ -32,7 +31,7 @@ defmodule GroupherServer.CMS.Helper.Macros do

add(:post_id, references(:cms_posts, on_delete: :delete_all))
add(:job_id, references(:cms_jobs, on_delete: :delete_all))
add(:repo_id, references(:cms_jobs, on_delete: :delete_all))
add(:repo_id, references(:cms_repos, on_delete: :delete_all))
...
"""
defmacro article_belongs_to_fields() do
Expand All @@ -41,7 +40,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
quote do
belongs_to(
unquote(thread),
Module.concat(CMS.Model, unquote(thread) |> to_string |> Recase.to_pascal()),
Module.concat(CMS.Model, Recase.to_pascal(to_string(unquote(thread)))),
foreign_key: unquote(:"#{thread}_id")
)
end
Expand Down Expand Up @@ -184,6 +183,7 @@ defmodule GroupherServer.CMS.Helper.Macros do
"""
defmacro general_article_fields do
quote do
field(:title, :string)
belongs_to(:author, Author)

field(:views, :integer, default: 0)
Expand All @@ -207,37 +207,14 @@ defmodule GroupherServer.CMS.Helper.Macros do
end
end

@doc """
for GroupherServer.CMS.Model.Community

# TABLE: "communities_[article]s"
add(:community_id, references(:communities, on_delete: :delete_all), null: false)
add(:[article]_id, references(:cms_[article]s, on_delete: :delete_all), null: false)

create(unique_index(:communities_[article]s, [:community_id, :[article]_id]))
"""
defmacro community_article_fields() do
@article_threads
|> Enum.map(fn thread ->
quote do
many_to_many(
unquote(:"#{thread}s"),
Module.concat(CMS.Model, unquote(thread) |> to_string |> Recase.to_pascal()),
join_through: unquote("communities_#{to_string(thread)}s"),
join_keys: [community_id: :id] ++ Keyword.new([{unquote(:"#{thread}_id"), :id}])
)
end
end)
end

@doc """
for GroupherServer.CMS.[Article]

# TABLE: "communities_join_[article]s"
add(:community_id, references(:communities, on_delete: :delete_all), null: false)
add(:[article]_id, references(:cms_[article]s, on_delete: :delete_all), null: false)

create(unique_index(:communities_job_[article]s, [:community_id, :[article]_id]))
create(unique_index(:communities_join_[article]s, [:community_id, :[article]_id]))
"""
defmacro article_communities_field(thread) do
quote do
Expand Down
2 changes: 0 additions & 2 deletions lib/groupher_server/cms/models/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ defmodule GroupherServer.CMS.Model.Community do
# on_replace: :delete
)

community_article_fields()
#
# posts_block_list ...
timestamps(type: :utc_datetime)
end
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/models/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule GroupherServer.CMS.Model.Job do

@type t :: %Job{}
schema "cms_jobs" do
field(:title, :string)
field(:company, :string)
field(:company_link, :string)
field(:desc, :string)
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/models/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule GroupherServer.CMS.Model.Post do
@type t :: %Post{}
schema "cms_posts" do
field(:body, :string)
field(:title, :string)
field(:digest, :string)
field(:link_addr, :string)
field(:copy_right, :string)
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/models/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule GroupherServer.CMS.Model.Repo do

@type t :: %Repo{}
schema "cms_repos" do
field(:title, :string)
field(:owner_name, :string)
field(:owner_url, :string)
field(:repo_url, :string)
Expand Down