mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
context: source_changes, tag_changes
This commit is contained in:
parent
98582aeb5f
commit
b6cdb85d75
4 changed files with 357 additions and 0 deletions
|
@ -391,4 +391,196 @@ defmodule Philomena.Images do
|
||||||
def change_subscription(%Subscription{} = subscription) do
|
def change_subscription(%Subscription{} = subscription) do
|
||||||
Subscription.changeset(subscription, %{})
|
Subscription.changeset(subscription, %{})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias Philomena.Images.SourceChange
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the list of source_changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> list_source_changes()
|
||||||
|
[%SourceChange{}, ...]
|
||||||
|
|
||||||
|
"""
|
||||||
|
def list_source_changes do
|
||||||
|
Repo.all(SourceChange)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Gets a single source_change.
|
||||||
|
|
||||||
|
Raises `Ecto.NoResultsError` if the Source change does not exist.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> get_source_change!(123)
|
||||||
|
%SourceChange{}
|
||||||
|
|
||||||
|
iex> get_source_change!(456)
|
||||||
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
|
"""
|
||||||
|
def get_source_change!(id), do: Repo.get!(SourceChange, id)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a source_change.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> create_source_change(%{field: value})
|
||||||
|
{:ok, %SourceChange{}}
|
||||||
|
|
||||||
|
iex> create_source_change(%{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def create_source_change(attrs \\ %{}) do
|
||||||
|
%SourceChange{}
|
||||||
|
|> SourceChange.changeset(attrs)
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates a source_change.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> update_source_change(source_change, %{field: new_value})
|
||||||
|
{:ok, %SourceChange{}}
|
||||||
|
|
||||||
|
iex> update_source_change(source_change, %{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def update_source_change(%SourceChange{} = source_change, attrs) do
|
||||||
|
source_change
|
||||||
|
|> SourceChange.changeset(attrs)
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Deletes a SourceChange.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> delete_source_change(source_change)
|
||||||
|
{:ok, %SourceChange{}}
|
||||||
|
|
||||||
|
iex> delete_source_change(source_change)
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def delete_source_change(%SourceChange{} = source_change) do
|
||||||
|
Repo.delete(source_change)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns an `%Ecto.Changeset{}` for tracking source_change changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> change_source_change(source_change)
|
||||||
|
%Ecto.Changeset{source: %SourceChange{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def change_source_change(%SourceChange{} = source_change) do
|
||||||
|
SourceChange.changeset(source_change, %{})
|
||||||
|
end
|
||||||
|
|
||||||
|
alias Philomena.Images.TagChange
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the list of tag_changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> list_tag_changes()
|
||||||
|
[%TagChange{}, ...]
|
||||||
|
|
||||||
|
"""
|
||||||
|
def list_tag_changes do
|
||||||
|
Repo.all(TagChange)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Gets a single tag_change.
|
||||||
|
|
||||||
|
Raises `Ecto.NoResultsError` if the Tag change does not exist.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> get_tag_change!(123)
|
||||||
|
%TagChange{}
|
||||||
|
|
||||||
|
iex> get_tag_change!(456)
|
||||||
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
|
"""
|
||||||
|
def get_tag_change!(id), do: Repo.get!(TagChange, id)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a tag_change.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> create_tag_change(%{field: value})
|
||||||
|
{:ok, %TagChange{}}
|
||||||
|
|
||||||
|
iex> create_tag_change(%{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def create_tag_change(attrs \\ %{}) do
|
||||||
|
%TagChange{}
|
||||||
|
|> TagChange.changeset(attrs)
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates a tag_change.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> update_tag_change(tag_change, %{field: new_value})
|
||||||
|
{:ok, %TagChange{}}
|
||||||
|
|
||||||
|
iex> update_tag_change(tag_change, %{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def update_tag_change(%TagChange{} = tag_change, attrs) do
|
||||||
|
tag_change
|
||||||
|
|> TagChange.changeset(attrs)
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Deletes a TagChange.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> delete_tag_change(tag_change)
|
||||||
|
{:ok, %TagChange{}}
|
||||||
|
|
||||||
|
iex> delete_tag_change(tag_change)
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def delete_tag_change(%TagChange{} = tag_change) do
|
||||||
|
Repo.delete(tag_change)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns an `%Ecto.Changeset{}` for tracking tag_change changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> change_tag_change(tag_change)
|
||||||
|
%Ecto.Changeset{source: %TagChange{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def change_tag_change(%TagChange{} = tag_change) do
|
||||||
|
TagChange.changeset(tag_change, %{})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
25
lib/philomena/images/source_change.ex
Normal file
25
lib/philomena/images/source_change.ex
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
defmodule Philomena.Images.SourceChange do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "source_changes" do
|
||||||
|
belongs_to :user, Philomena.Users.User
|
||||||
|
belongs_to :image, Philomena.Images.Image
|
||||||
|
|
||||||
|
field :ip, EctoNetwork.INET
|
||||||
|
field :fingerprint, :string
|
||||||
|
field :user_agent, :string, default: ""
|
||||||
|
field :referrer, :string, default: ""
|
||||||
|
field :new_value, :string
|
||||||
|
field :initial, :boolean, default: false
|
||||||
|
|
||||||
|
timestamps(inserted_at: :created_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def changeset(source_change, attrs) do
|
||||||
|
source_change
|
||||||
|
|> cast(attrs, [])
|
||||||
|
|> validate_required([])
|
||||||
|
end
|
||||||
|
end
|
26
lib/philomena/images/tag_change.ex
Normal file
26
lib/philomena/images/tag_change.ex
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
defmodule Philomena.Images.TagChange do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "tag_changes" do
|
||||||
|
belongs_to :user, Philomena.Users.User
|
||||||
|
belongs_to :tag, Philomena.Tags.Tag
|
||||||
|
belongs_to :image, Philomena.Images.Image
|
||||||
|
|
||||||
|
field :ip, EctoNetwork.INET
|
||||||
|
field :fingerprint, :string
|
||||||
|
field :user_agent, :string, default: ""
|
||||||
|
field :referrer, :string, default: ""
|
||||||
|
field :added, :boolean
|
||||||
|
field :tag_name_cache, :string, default: ""
|
||||||
|
|
||||||
|
timestamps(inserted_at: :created_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def changeset(tag_change, attrs) do
|
||||||
|
tag_change
|
||||||
|
|> cast(attrs, [])
|
||||||
|
|> validate_required([])
|
||||||
|
end
|
||||||
|
end
|
|
@ -230,4 +230,118 @@ defmodule Philomena.ImagesTest do
|
||||||
assert %Ecto.Changeset{} = Images.change_subscription(subscription)
|
assert %Ecto.Changeset{} = Images.change_subscription(subscription)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "source_changes" do
|
||||||
|
alias Philomena.Images.SourceChange
|
||||||
|
|
||||||
|
@valid_attrs %{}
|
||||||
|
@update_attrs %{}
|
||||||
|
@invalid_attrs %{}
|
||||||
|
|
||||||
|
def source_change_fixture(attrs \\ %{}) do
|
||||||
|
{:ok, source_change} =
|
||||||
|
attrs
|
||||||
|
|> Enum.into(@valid_attrs)
|
||||||
|
|> Images.create_source_change()
|
||||||
|
|
||||||
|
source_change
|
||||||
|
end
|
||||||
|
|
||||||
|
test "list_source_changes/0 returns all source_changes" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert Images.list_source_changes() == [source_change]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get_source_change!/1 returns the source_change with given id" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert Images.get_source_change!(source_change.id) == source_change
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_source_change/1 with valid data creates a source_change" do
|
||||||
|
assert {:ok, %SourceChange{} = source_change} = Images.create_source_change(@valid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_source_change/1 with invalid data returns error changeset" do
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Images.create_source_change(@invalid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_source_change/2 with valid data updates the source_change" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert {:ok, %SourceChange{} = source_change} = Images.update_source_change(source_change, @update_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_source_change/2 with invalid data returns error changeset" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Images.update_source_change(source_change, @invalid_attrs)
|
||||||
|
assert source_change == Images.get_source_change!(source_change.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "delete_source_change/1 deletes the source_change" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert {:ok, %SourceChange{}} = Images.delete_source_change(source_change)
|
||||||
|
assert_raise Ecto.NoResultsError, fn -> Images.get_source_change!(source_change.id) end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change_source_change/1 returns a source_change changeset" do
|
||||||
|
source_change = source_change_fixture()
|
||||||
|
assert %Ecto.Changeset{} = Images.change_source_change(source_change)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "tag_changes" do
|
||||||
|
alias Philomena.Images.TagChange
|
||||||
|
|
||||||
|
@valid_attrs %{}
|
||||||
|
@update_attrs %{}
|
||||||
|
@invalid_attrs %{}
|
||||||
|
|
||||||
|
def tag_change_fixture(attrs \\ %{}) do
|
||||||
|
{:ok, tag_change} =
|
||||||
|
attrs
|
||||||
|
|> Enum.into(@valid_attrs)
|
||||||
|
|> Images.create_tag_change()
|
||||||
|
|
||||||
|
tag_change
|
||||||
|
end
|
||||||
|
|
||||||
|
test "list_tag_changes/0 returns all tag_changes" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert Images.list_tag_changes() == [tag_change]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get_tag_change!/1 returns the tag_change with given id" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert Images.get_tag_change!(tag_change.id) == tag_change
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_tag_change/1 with valid data creates a tag_change" do
|
||||||
|
assert {:ok, %TagChange{} = tag_change} = Images.create_tag_change(@valid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_tag_change/1 with invalid data returns error changeset" do
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Images.create_tag_change(@invalid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_tag_change/2 with valid data updates the tag_change" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert {:ok, %TagChange{} = tag_change} = Images.update_tag_change(tag_change, @update_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_tag_change/2 with invalid data returns error changeset" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Images.update_tag_change(tag_change, @invalid_attrs)
|
||||||
|
assert tag_change == Images.get_tag_change!(tag_change.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "delete_tag_change/1 deletes the tag_change" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert {:ok, %TagChange{}} = Images.delete_tag_change(tag_change)
|
||||||
|
assert_raise Ecto.NoResultsError, fn -> Images.get_tag_change!(tag_change.id) end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change_tag_change/1 returns a tag_change changeset" do
|
||||||
|
tag_change = tag_change_fixture()
|
||||||
|
assert %Ecto.Changeset{} = Images.change_tag_change(tag_change)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue