My space on the web https://inhji.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

44 lines
1018 B

defmodule Mirage.Notes.Note do
use Ecto.Schema
import Ecto.Changeset
alias Mirage.Notes.{NoteLink, Topic, Tags, Note}
schema "notes" do
field :title, :string
field :content, :string
field :content_html, :string
has_many :links, NoteLink
many_to_many :topics, Topic, join_through: "notes_topics"
many_to_many :backlinks, Note,
join_through: "notes_notes",
join_keys: [target_id: :id, source_id: :id]
field :topic_string, :string,
virtual: true,
default: ""
timestamps()
end
@doc false
def changeset(note, attrs) do
note
|> cast(attrs, [:content, :title])
|> validate_required([:content, :title])
|> Mirage.Markdown.maybe_render_markdown(:content, :content_html)
|> maybe_load_tag_string()
end
def maybe_load_tag_string(changeset) do
case get_field(changeset, :topic_string) do
"" ->
put_change(changeset, :topic_string, Tags.get_tags(changeset.data))
_ ->
changeset
end
end
end