https://github.com/mongoid/mongoid-tag-collectible
Easily maintain a collection of Tag instances with aggregate counts from your model's tags.
https://github.com/mongoid/mongoid-tag-collectible
Last synced: about 1 year ago
JSON representation
Easily maintain a collection of Tag instances with aggregate counts from your model's tags.
- Host: GitHub
- URL: https://github.com/mongoid/mongoid-tag-collectible
- Owner: mongoid
- License: mit
- Created: 2013-06-26T03:14:46.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2016-08-04T20:09:16.000Z (almost 10 years ago)
- Last Synced: 2025-04-15T03:55:36.096Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 35.2 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
Mongoid::TagCollectible
=======================
[](http://badge.fury.io/rb/mongoid-tag-collectible)
[](https://travis-ci.org/mongoid/mongoid-tag-collectible)
[](https://gemnasium.com/mongoid/mongoid-tag-collectible)
[](https://codeclimate.com/github/mongoid/mongoid-tag-collectible)
Easily maintain a collection of `Tag` instances with aggregate counts from your model's `tags`.
### Compatibility
This gem supports Mongoid 3, Mongoid 4 and Mongoid 5.
### Install
Add `mongoid-tag-collectible` to your Gemfile.
```
gem 'mongoid-tag-collectible'
```
### Use
``` ruby
class Thing
include Mongoid::Document
include Mongoid::TagCollectible::Tagged
end
thing1 = Thing.create!(tags: [ 'funny', 'red' ])
thing2 = Thing.create!(tags: [ 'funny', 'yellow' ])
funny_tag = ThingTag.find('funny') # find by tag
funny_tag.name # "funny"
funny_tag.count # 2, not a database query
funny_tag.tagged # thing1 and thing2
```
#### Renaming Tags
You can rename a tag, which causes all the tags in your model's `tags` collection to be renamed.
``` ruby
ThingTag.find('funny').update_attributes!(name: 'sad')
Thing.first.tags # [ 'sad', 'red' ]
```
#### Destroying Tags
You can destroy a tag, which also removes it from your model's `tags` collection.
``` ruby
ThingTag.find('red').destroy
Thing.first.tags # [ 'sad' ]
```
#### Case-Sensitive
Tags are case-sensitive. Transform your tags in `before_validation` if you don't want this behavior.
``` ruby
class Thing
before_validation :downcase_tags
private
def downcase_tags
tags = tags.map(&:downcase) if tags
end
end
```
### Contribute
See [CONTRIBUTING](CONTRIBUTING.md).
### Copyright and License
Copyright Daniel Doubrovkine and Contributors, Artsy Inc., 2013-2015
[MIT License](LICENSE.md)