Skip to content

xerions/ecto_taggable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EctoTaggable

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)

About

Ecto taggable plugin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages