break compile time dependencies in schema helpers

This commit is contained in:
byte[] 2019-12-30 16:22:24 -05:00
parent f278362fda
commit 907e030b3a
9 changed files with 39 additions and 38 deletions

View file

@ -2,7 +2,7 @@ defmodule Philomena.Adverts.Advert do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.Schema.Time alias Philomena.Schema.Time
schema "adverts" do schema "adverts" do
field :image, :string field :image, :string
@ -34,15 +34,15 @@ defmodule Philomena.Adverts.Advert do
def changeset(advert, attrs) do def changeset(advert, attrs) do
advert advert
|> cast(attrs, []) |> cast(attrs, [])
|> propagate_time(:start_date, :start_time) |> Time.propagate_time(:start_date, :start_time)
|> propagate_time(:finish_date, :finish_time) |> Time.propagate_time(:finish_date, :finish_time)
end end
def save_changeset(advert, attrs) do def save_changeset(advert, attrs) do
advert advert
|> cast(attrs, [:title, :link, :start_time, :finish_time, :live, :restrictions, :notes]) |> cast(attrs, [:title, :link, :start_time, :finish_time, :live, :restrictions, :notes])
|> assign_time(:start_time, :start_date) |> Time.assign_time(:start_time, :start_date)
|> assign_time(:finish_time, :finish_date) |> Time.assign_time(:finish_time, :finish_date)
|> validate_required([:title, :link, :start_date, :finish_date]) |> validate_required([:title, :link, :start_date, :finish_date])
|> validate_inclusion(:restrictions, ["none", "nsfw", "sfw"]) |> validate_inclusion(:restrictions, ["none", "nsfw", "sfw"])
end end

View file

@ -3,8 +3,8 @@ defmodule Philomena.Bans.Fingerprint do
import Ecto.Changeset import Ecto.Changeset
alias Philomena.Users.User alias Philomena.Users.User
import Philomena.Schema.Time alias Philomena.Schema.Time
import Philomena.Schema.BanId alias Philomena.Schema.BanId
schema "fingerprint_bans" do schema "fingerprint_bans" do
belongs_to :banning_user, User belongs_to :banning_user, User
@ -25,14 +25,14 @@ defmodule Philomena.Bans.Fingerprint do
def changeset(fingerprint_ban, attrs) do def changeset(fingerprint_ban, attrs) do
fingerprint_ban fingerprint_ban
|> cast(attrs, []) |> cast(attrs, [])
|> propagate_time(:valid_until, :until) |> Time.propagate_time(:valid_until, :until)
end end
def save_changeset(fingerprint_ban, attrs) do def save_changeset(fingerprint_ban, attrs) do
fingerprint_ban fingerprint_ban
|> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until]) |> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until])
|> assign_time(:until, :valid_until) |> Time.assign_time(:until, :valid_until)
|> put_ban_id("F") |> BanId.put_ban_id("F")
|> validate_required([:reason, :enabled, :fingerprint, :valid_until]) |> validate_required([:reason, :enabled, :fingerprint, :valid_until])
end end
end end

View file

@ -3,8 +3,8 @@ defmodule Philomena.Bans.Subnet do
import Ecto.Changeset import Ecto.Changeset
alias Philomena.Users.User alias Philomena.Users.User
import Philomena.Schema.Time alias Philomena.Schema.Time
import Philomena.Schema.BanId alias Philomena.Schema.BanId
schema "subnet_bans" do schema "subnet_bans" do
belongs_to :banning_user, User belongs_to :banning_user, User
@ -25,14 +25,14 @@ defmodule Philomena.Bans.Subnet do
def changeset(subnet_ban, attrs) do def changeset(subnet_ban, attrs) do
subnet_ban subnet_ban
|> cast(attrs, []) |> cast(attrs, [])
|> propagate_time(:valid_until, :until) |> Time.propagate_time(:valid_until, :until)
end end
def save_changeset(subnet_ban, attrs) do def save_changeset(subnet_ban, attrs) do
subnet_ban subnet_ban
|> cast(attrs, [:reason, :note, :enabled, :specification, :until]) |> cast(attrs, [:reason, :note, :enabled, :specification, :until])
|> assign_time(:until, :valid_until) |> Time.assign_time(:until, :valid_until)
|> put_ban_id("S") |> BanId.put_ban_id("S")
|> validate_required([:reason, :enabled, :specification, :valid_until]) |> validate_required([:reason, :enabled, :specification, :valid_until])
|> mask_specification() |> mask_specification()
end end

View file

@ -4,8 +4,8 @@ defmodule Philomena.Bans.User do
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Repo alias Philomena.Repo
import Philomena.Schema.Time alias Philomena.Schema.Time
import Philomena.Schema.BanId alias Philomena.Schema.BanId
schema "user_bans" do schema "user_bans" do
belongs_to :user, User belongs_to :user, User
@ -28,16 +28,16 @@ defmodule Philomena.Bans.User do
def changeset(user_ban, attrs) do def changeset(user_ban, attrs) do
user_ban user_ban
|> cast(attrs, []) |> cast(attrs, [])
|> propagate_time(:valid_until, :until) |> Time.propagate_time(:valid_until, :until)
|> populate_username() |> populate_username()
end end
def save_changeset(user_ban, attrs) do def save_changeset(user_ban, attrs) do
user_ban user_ban
|> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until]) |> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until])
|> assign_time(:until, :valid_until) |> Time.assign_time(:until, :valid_until)
|> populate_user_id() |> populate_user_id()
|> put_ban_id("U") |> BanId.put_ban_id("U")
|> validate_required([:reason, :enabled, :user_id, :valid_until]) |> validate_required([:reason, :enabled, :user_id, :valid_until])
end end

View file

@ -1,9 +1,9 @@
defmodule Philomena.Filters.Filter do defmodule Philomena.Filters.Filter do
use Ecto.Schema use Ecto.Schema
import Philomena.Schema.TagList
import Philomena.Schema.Search
import Ecto.Changeset import Ecto.Changeset
alias Philomena.Schema.TagList
alias Philomena.Schema.Search
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Repo alias Philomena.Repo
@ -35,13 +35,13 @@ defmodule Philomena.Filters.Filter do
filter filter
|> cast(attrs, [:spoilered_tag_list, :hidden_tag_list, :description, :name, :spoilered_complex_str, :hidden_complex_str]) |> cast(attrs, [:spoilered_tag_list, :hidden_tag_list, :description, :name, :spoilered_complex_str, :hidden_complex_str])
|> propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids) |> TagList.propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids)
|> propagate_tag_list(:hidden_tag_list, :hidden_tag_ids) |> TagList.propagate_tag_list(:hidden_tag_list, :hidden_tag_ids)
|> validate_required([:name, :description]) |> validate_required([:name, :description])
|> validate_my_downvotes(:spoilered_complex_str) |> validate_my_downvotes(:spoilered_complex_str)
|> validate_my_downvotes(:hidden_complex_str) |> validate_my_downvotes(:hidden_complex_str)
|> validate_search(:spoilered_complex_str, user) |> Search.validate_search(:spoilered_complex_str, user)
|> validate_search(:hidden_complex_str, user) |> Search.validate_search(:hidden_complex_str, user)
|> unsafe_validate_unique([:user_id, :name], Repo) |> unsafe_validate_unique([:user_id, :name], Repo)
end end

View file

@ -3,7 +3,7 @@ defmodule Philomena.SiteNotices.SiteNotice do
import Ecto.Changeset import Ecto.Changeset
alias Philomena.Users.User alias Philomena.Users.User
import Philomena.Schema.Time alias Philomena.Schema.Time
schema "site_notices" do schema "site_notices" do
belongs_to :user, User belongs_to :user, User
@ -26,15 +26,15 @@ defmodule Philomena.SiteNotices.SiteNotice do
def changeset(site_notice, attrs) do def changeset(site_notice, attrs) do
site_notice site_notice
|> cast(attrs, []) |> cast(attrs, [])
|> propagate_time(:start_date, :start_time) |> Time.propagate_time(:start_date, :start_time)
|> propagate_time(:finish_date, :finish_time) |> Time.propagate_time(:finish_date, :finish_time)
|> validate_required([]) |> validate_required([])
end end
def save_changeset(site_notice, attrs) do def save_changeset(site_notice, attrs) do
site_notice site_notice
|> cast(attrs, [:title, :text, :link, :link_text, :live, :start_time, :finish_time]) |> cast(attrs, [:title, :text, :link, :link_text, :live, :start_time, :finish_time])
|> assign_time(:start_time, :start_date) |> Time.assign_time(:start_time, :start_date)
|> assign_time(:finish_time, :finish_date) |> Time.assign_time(:finish_time, :finish_date)
end end
end end

View file

@ -12,8 +12,9 @@ defmodule Philomena.Users.User do
extensions: [PowResetPassword, PowLockout] extensions: [PowResetPassword, PowLockout]
import Ecto.Changeset import Ecto.Changeset
import Philomena.Schema.TagList
import Philomena.Schema.Search alias Philomena.Schema.TagList
alias Philomena.Schema.Search
alias Philomena.Filters.Filter alias Philomena.Filters.Filter
alias Philomena.UserLinks.UserLink alias Philomena.UserLinks.UserLink
@ -206,12 +207,12 @@ defmodule Philomena.Users.User do
:anonymous_by_default, :scale_large_images, :comments_per_page, :theme, :anonymous_by_default, :scale_large_images, :comments_per_page, :theme,
:no_spoilered_in_watched, :use_centered_layout, :hide_vote_counts :no_spoilered_in_watched, :use_centered_layout, :hide_vote_counts
]) ])
|> propagate_tag_list(:watched_tag_list, :watched_tag_ids) |> TagList.propagate_tag_list(:watched_tag_list, :watched_tag_ids)
|> validate_inclusion(:theme, ~W(default dark red)) |> validate_inclusion(:theme, ~W(default dark red))
|> validate_inclusion(:images_per_page, 15..50) |> validate_inclusion(:images_per_page, 15..50)
|> validate_inclusion(:comments_per_page, 15..100) |> validate_inclusion(:comments_per_page, 15..100)
|> validate_search(:watched_images_query_str, user, true) |> Search.validate_search(:watched_images_query_str, user, true)
|> validate_search(:watched_images_exclude_str, user, true) |> Search.validate_search(:watched_images_exclude_str, user, true)
end end
def description_changeset(user, attrs) do def description_changeset(user, attrs) do