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

Commit c7cf29e

Browse files
authored
refactor(cms-macros): extract article_belongs_to macro (#360)
* refactor(cms-macros): extract article_belongs_to macro * refactor: wip * refactor: general articles_foreign_key_constraint helper * refactor(cms-macros): clean up warnings * refactor(cms-macros): unused emotion config
1 parent 37badea commit c7cf29e

File tree

22 files changed

+259
-109
lines changed

22 files changed

+259
-109
lines changed

config/config.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ config :groupher_server, :customization,
6161
sidebar_communities_index: %{}
6262

6363
config :groupher_server, :article,
64-
emotionable_threads: [:post, :job],
6564
# NOTE: if you want to add/remove emotion, just edit the list below
6665
# and migrate the field to table "articles_users_emotions"
6766
supported_emotions: [

lib/groupher_server/accounts/delegates/collect_folder.ex

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
152152
CMS.set_collect_folder(article_collect, folder)
153153
end)
154154
|> Repo.transaction()
155-
|> upsert_collect_folder_result()
155+
|> result()
156156
end
157157
end
158158

@@ -183,7 +183,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
183183
CMS.undo_set_collect_folder(article_collect, folder)
184184
end)
185185
|> Repo.transaction()
186-
|> upsert_collect_folder_result()
186+
|> result()
187187
end
188188
end
189189

@@ -249,13 +249,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
249249

250250
defp filter_thread_ifneed(query, _), do: query
251251

252-
defp upsert_collect_folder_result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result}
253-
254-
defp upsert_collect_folder_result({:ok, %{rm_from_collect_folder: result}}) do
255-
{:ok, result}
256-
end
257-
258-
defp upsert_collect_folder_result({:error, _, result, _steps}) do
259-
{:error, result}
260-
end
252+
defp result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result}
253+
defp result({:ok, %{rm_from_collect_folder: result}}), do: {:ok, result}
254+
defp result({:error, _, result, _steps}), do: {:error, result}
261255
end

lib/groupher_server/cms/abuse_report.ex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ defmodule GroupherServer.CMS.AbuseReport do
55
use Ecto.Schema
66
use Accessible
77
import Ecto.Changeset
8+
import GroupherServer.CMS.Helper.Macros
9+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
810

911
alias GroupherServer.{Accounts, CMS}
10-
alias CMS.{ArticleComment, Embeds, Post, Job, Repo}
12+
alias CMS.{ArticleComment, Embeds}
13+
14+
@article_threads CMS.Community.article_threads()
1115

1216
# @required_fields ~w(article_comment_id user_id recived_user_id)a
13-
@optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with report_cases_count)a
17+
@optional_fields ~w(article_comment_id account_id operate_user_id deal_with report_cases_count)a
1418
@update_fields ~w(operate_user_id deal_with report_cases_count)a
1519

20+
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")
21+
1622
@type t :: %AbuseReport{}
1723
schema "abuse_reports" do
1824
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
19-
belongs_to(:post, Post, foreign_key: :post_id)
20-
belongs_to(:job, Job, foreign_key: :job_id)
21-
belongs_to(:repo, Repo, foreign_key: :repo_id)
2225
belongs_to(:account, Accounts.User, foreign_key: :account_id)
2326

2427
embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)
@@ -28,19 +31,22 @@ defmodule GroupherServer.CMS.AbuseReport do
2831

2932
field(:deal_with, :string)
3033

34+
article_belongs_to()
3135
timestamps(type: :utc_datetime)
3236
end
3337

3438
@doc false
3539
def changeset(%AbuseReport{} = struct, attrs) do
3640
struct
37-
|> cast(attrs, @optional_fields)
41+
|> cast(attrs, @optional_fields ++ @article_fields)
3842
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
43+
|> articles_foreign_key_constraint
3944
end
4045

4146
def update_changeset(%AbuseReport{} = struct, attrs) do
4247
struct
43-
|> cast(attrs, @update_fields)
48+
|> cast(attrs, @update_fields ++ @article_fields)
4449
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
50+
|> articles_foreign_key_constraint
4551
end
4652
end

lib/groupher_server/cms/article_collect.ex

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,36 @@ defmodule GroupherServer.CMS.ArticleCollect do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7+
import GroupherServer.CMS.Helper.Macros
8+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
79

810
alias GroupherServer.{Accounts, CMS}
9-
1011
alias Accounts.{User, CollectFolder}
11-
alias CMS.{Post, Job, Repo}
12+
13+
@article_threads CMS.Community.article_threads()
1214

1315
@required_fields ~w(user_id)a
14-
@optional_fields ~w(thread post_id job_id repo_id)a
16+
@optional_fields ~w(thread)a
17+
18+
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")
1519

1620
@type t :: %ArticleCollect{}
1721
schema "article_collects" do
1822
field(:thread, :string)
19-
2023
belongs_to(:user, User, foreign_key: :user_id)
21-
belongs_to(:post, Post, foreign_key: :post_id)
22-
belongs_to(:job, Job, foreign_key: :job_id)
23-
belongs_to(:repo, Repo, foreign_key: :repo_id)
24-
2524
embeds_many(:collect_folders, CollectFolder, on_replace: :delete)
2625

26+
article_belongs_to()
2727
timestamps(type: :utc_datetime)
2828
end
2929

3030
@doc false
3131
def changeset(%ArticleCollect{} = article_collect, attrs) do
3232
article_collect
33-
|> cast(attrs, @optional_fields ++ @required_fields)
33+
|> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields)
3434
|> validate_required(@required_fields)
3535
|> cast_embed(:collect_folders, with: &CollectFolder.changeset/2)
3636
|> foreign_key_constraint(:user_id)
37-
|> foreign_key_constraint(:post_id)
38-
|> foreign_key_constraint(:job_id)
39-
|> foreign_key_constraint(:repo_id)
37+
|> articles_foreign_key_constraint
4038
end
4139
end

lib/groupher_server/cms/article_comment.ex

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ defmodule GroupherServer.CMS.ArticleComment do
66
use Accessible
77

88
import Ecto.Changeset
9+
import GroupherServer.CMS.Helper.Macros
10+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
911

1012
alias GroupherServer.{Accounts, CMS}
11-
12-
alias CMS.{
13-
Post,
14-
Job,
15-
Repo,
16-
Embeds,
17-
ArticleCommentUpvote
18-
}
13+
alias CMS.{Embeds, ArticleCommentUpvote}
1914

2015
# alias Helper.HTML
16+
@article_threads CMS.Community.article_threads()
2117

2218
@required_fields ~w(body_html author_id)a
23-
@optional_fields ~w(post_id job_id repo_id reply_to_id replies_count is_folded is_deleted floor is_article_author)a
19+
@optional_fields ~w(reply_to_id replies_count is_folded is_deleted floor is_article_author)a
2420
@updatable_fields ~w(is_folded is_deleted floor upvotes_count is_pinned)a
2521

22+
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")
23+
2624
@max_participator_count 5
2725
@max_parent_replies_count 3
2826

@@ -69,10 +67,6 @@ defmodule GroupherServer.CMS.ArticleComment do
6967
field(:is_pinned, :boolean, default: false)
7068
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)
7169

72-
belongs_to(:post, Post, foreign_key: :post_id)
73-
belongs_to(:job, Job, foreign_key: :job_id)
74-
belongs_to(:repo, Repo, foreign_key: :repo_id)
75-
7670
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)
7771

7872
embeds_many(:replies, ArticleComment, on_replace: :delete)
@@ -83,13 +77,14 @@ defmodule GroupherServer.CMS.ArticleComment do
8377

8478
has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote})
8579

80+
article_belongs_to()
8681
timestamps(type: :utc_datetime)
8782
end
8883

8984
@doc false
9085
def changeset(%ArticleComment{} = article_comment, attrs) do
9186
article_comment
92-
|> cast(attrs, @required_fields ++ @optional_fields)
87+
|> cast(attrs, @required_fields ++ @optional_fields ++ @article_fields)
9388
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
9489
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
9590
|> validate_required(@required_fields)
@@ -99,14 +94,15 @@ defmodule GroupherServer.CMS.ArticleComment do
9994
# @doc false
10095
def update_changeset(%ArticleComment{} = article_comment, attrs) do
10196
article_comment
102-
|> cast(attrs, @required_fields ++ @updatable_fields)
97+
|> cast(attrs, @required_fields ++ @updatable_fields ++ @article_fields)
10398
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
10499
|> generl_changeset
105100
end
106101

107102
defp generl_changeset(content) do
108103
content
109104
|> foreign_key_constraint(:author_id)
105+
|> articles_foreign_key_constraint
110106

111107
# |> validate_length(:body_html, min: 3, max: 2000)
112108
# |> HTML.safe_string(:body_html)

lib/groupher_server/cms/article_pinned_comment.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,40 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do
66
use Accessible
77

88
import Ecto.Changeset
9+
import GroupherServer.CMS.Helper.Macros
10+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
911

1012
alias GroupherServer.CMS
1113
alias CMS.ArticleComment
1214

1315
# alias Helper.HTML
16+
@article_threads CMS.Community.article_threads()
1417

1518
@required_fields ~w(article_comment_id)a
16-
@optional_fields ~w(post_id job_id repo_id)a
19+
# @optional_fields ~w(post_id job_id repo_id)a
20+
21+
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")
1722

1823
@type t :: %ArticlePinnedComment{}
1924
schema "articles_pinned_comments" do
2025
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
21-
belongs_to(:post, CMS.Post, foreign_key: :post_id)
22-
belongs_to(:job, CMS.Job, foreign_key: :job_id)
23-
belongs_to(:repo, CMS.Repo, foreign_key: :repo_id)
2426

27+
article_belongs_to()
2528
timestamps(type: :utc_datetime)
2629
end
2730

2831
@doc false
2932
def changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
3033
article_pined_comment
31-
|> cast(attrs, @required_fields ++ @optional_fields)
34+
|> cast(attrs, @required_fields ++ @article_fields)
3235
|> validate_required(@required_fields)
36+
|> articles_foreign_key_constraint
3337
end
3438

3539
# @doc false
3640
def update_changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do
3741
article_pined_comment
38-
|> cast(attrs, @required_fields ++ @optional_fields)
42+
|> cast(attrs, @required_fields ++ @article_fields)
43+
|> articles_foreign_key_constraint
3944
end
4045
end

lib/groupher_server/cms/article_upvote.ex

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,34 @@ defmodule GroupherServer.CMS.ArticleUpvote do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7+
import GroupherServer.CMS.Helper.Macros
8+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
79

810
alias GroupherServer.{Accounts, CMS}
9-
1011
alias Accounts.User
11-
alias CMS.{Post, Job, Repo}
12+
13+
@article_threads CMS.Community.article_threads()
1214

1315
@required_fields ~w(user_id)a
14-
@optional_fields ~w(thread post_id job_id repo_id)a
16+
@optional_fields ~w(thread)a
17+
@article_fields @article_threads |> Enum.map(&:"#{&1}_id")
1518

1619
@type t :: %ArticleUpvote{}
1720
schema "article_upvotes" do
1821
# for user-center to filter
1922
field(:thread, :string)
20-
2123
belongs_to(:user, User, foreign_key: :user_id)
22-
belongs_to(:post, Post, foreign_key: :post_id)
23-
belongs_to(:job, Job, foreign_key: :job_id)
24-
belongs_to(:repo, Repo, foreign_key: :repo_id)
2524

25+
article_belongs_to()
2626
timestamps(type: :utc_datetime)
2727
end
2828

2929
@doc false
3030
def changeset(%ArticleUpvote{} = article_upvote, attrs) do
3131
article_upvote
32-
|> cast(attrs, @optional_fields ++ @required_fields)
32+
|> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields)
3333
|> validate_required(@required_fields)
3434
|> foreign_key_constraint(:user_id)
35-
|> foreign_key_constraint(:post_id)
36-
|> foreign_key_constraint(:job_id)
37-
|> foreign_key_constraint(:repo_id)
35+
|> articles_foreign_key_constraint
3836
end
3937
end

lib/groupher_server/cms/article_user_emotion.ex

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
2323
import Ecto.Changeset
2424
import GroupherServer.CMS.ArticleUserEmotion.Macros
2525
import Helper.Utils, only: [get_config: 2]
26+
import GroupherServer.CMS.Helper.Macros
27+
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
2628

2729
alias GroupherServer.{Accounts, CMS}
28-
alias CMS.{Post, Job}
2930

3031
@supported_emotions get_config(:article, :supported_emotions)
31-
@supported_threads get_config(:article, :emotionable_threads)
32+
@article_threads CMS.Community.article_threads()
3233

3334
@required_fields ~w(user_id recived_user_id)a
34-
@optional_fields Enum.map(@supported_threads, &:"#{&1}_id") ++
35+
@optional_fields Enum.map(@article_threads, &:"#{&1}_id") ++
3536
Enum.map(@supported_emotions, &:"#{&1}")
3637

3738
@type t :: %ArticleUserEmotion{}
3839
schema "articles_users_emotions" do
39-
belongs_to(:post, Post, foreign_key: :post_id)
40-
belongs_to(:job, Job, foreign_key: :job_id)
4140
belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id)
4241
belongs_to(:user, Accounts.User, foreign_key: :user_id)
4342

4443
emotion_fields()
44+
article_belongs_to()
4545
timestamps(type: :utc_datetime)
4646
end
4747

@@ -50,19 +50,17 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
5050
struct
5151
|> cast(attrs, @required_fields ++ @optional_fields)
5252
|> validate_required(@required_fields)
53-
|> foreign_key_constraint(:post_id)
54-
|> foreign_key_constraint(:job_id)
5553
|> foreign_key_constraint(:user_id)
5654
|> foreign_key_constraint(:recived_user_id)
55+
|> articles_foreign_key_constraint
5756
end
5857

5958
def update_changeset(%ArticleUserEmotion{} = struct, attrs) do
6059
struct
6160
|> cast(attrs, @required_fields ++ @optional_fields)
6261
|> validate_required(@required_fields)
63-
|> foreign_key_constraint(:post_id)
64-
|> foreign_key_constraint(:job_id)
6562
|> foreign_key_constraint(:user_id)
6663
|> foreign_key_constraint(:recived_user_id)
64+
|> articles_foreign_key_constraint
6765
end
6866
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule GroupherServer.CMS.Helper.Macros do
2+
@moduledoc """
3+
macros for define article related fields in CMS models
4+
"""
5+
6+
alias GroupherServer.CMS
7+
8+
@article_threads CMS.Community.article_threads()
9+
10+
@doc """
11+
generate belongs to fields for given thread
12+
13+
e.g:
14+
belongs_to(:post, Post, foreign_key: :post_id)
15+
16+
NOTE: should do migration to DB manually
17+
"""
18+
defmacro article_belongs_to() do
19+
@article_threads
20+
|> Enum.map(fn thread ->
21+
quote do
22+
belongs_to(
23+
unquote(thread),
24+
Module.concat(CMS, unquote(thread) |> to_string |> Recase.to_pascal()),
25+
foreign_key: unquote(:"#{thread}_id")
26+
)
27+
end
28+
end)
29+
end
30+
end

0 commit comments

Comments
 (0)