ecto_taggable
allows to tag any row of an ecto
model.
For example you have model:
defmodule MyModel do
use Ecto.Model
schema "user" do
field :name, :string
field :old, :integer
has_many :tags, {"test_model_tags", Ecto.Taggable}, [foreign_key: :tag_id] # foreign_key: tag_id is necessarily
end
end
You can migrate this model and related tags
model to the database with ecto_migrate:
Ecto.Migration.Auto.migrate(EctoIt.Repo, MyModel)
Ecto.Migration.Auto.migrate(EctoIt.Repo, Ecto.Taggable, [for: MyModel])
Insert data in the database and set tag on this data:
my_model1 = repo.insert(%MyModel{name: "foo", old: 20})
my_model2 =repo.insert(%MyModel{name: "bar", old: 25})
Ecto.Taggable.set_tag(repo, my_model2 :tag_name)
ecto_taggable
provides ability to search tag with:
Ecto.Taggable.search_tag(repo, MyModel, :tag_name) % ==> %MyModel{name: "bar", ...}
and delete tag for all recrods with:
Ecto.Taggable.Api.drop_tag(EctoIt.Repo, MyModel, :tag_name)
or for the certain id
:
Ecto.Taggable.Api.drop_tag(EctoIt.Repo, %MyModel{id: 1}, :tag_name)