Mongoid Tag¶ ↑
This gem is heaviliy influenced by Aaron Qians mongoid_taggable_with_context gem. I needed a way to tag a model and let a contextual scope of that model hold meta-data about the tags.
In example: when I tag a product with “nice”, I wanted the products category to know how many products was tagged with “nice”. I also wanted to store additional data about this tag such as color (i.e colored labels on gmail.)
NOTE¶ ↑
This is my first gem ever, and it is experimental at the moment. Use it at your own risk…
Usage¶ ↑
class Product
include Mongoid::Document
include Mongoid::Tag
belongs_to :category
tag :tags, :meta_in => :category
end
class Category
include Mongoid::Document
include Mongoid::Tag::Meta
tag_meta_for :tags
end
@c = Category.create
@product = Product.create(:tags => "new, expensive", :category => @c)
@product.tags => ["new", "expensive"]
@category.tags_with_weight => [["new", 1], ["expensive", 1]]
@category.add_tag("on sale", {:color => "#a2a2a2"}) #add any meta you want
@category.tags_with_meta => [["new", {:count => 1}], ["expensive", {:count => 1}], ["on sale", {:count => 0, :color => "#a2a2a2"}]]
Scopes¶ ↑
@category.products.with_all_tags(['tag1', 'tag2']) => products tagged with both @category.products.with_any_tags(['tag1', 'tag2']) => products tagged with one or both @category.products.without_tags(['tag1', 'tag2']) => products not with any of tags