A gem that handles tags in a "Key"/"Value" setup.
We've been using acts_as_taggable_on for a lot of projects, and if all you need are single tags, it's a great solution. But similar to AWS, we needed a solution that allowed us to use Keys and Values, and Tag Bearer was born.
This gem has been tested against mysql and postgresql
Setup is simple
gem install tag_bearer
Generate and run rails migration
rails generate tag_bearer:install
rake db:migrate
If you change the default table name you must let TagBearer know in an initializer
# config/initializers/tag_bearer.rb
TagBearer.tag_table = 'new_table_name'
Add to model
class YourModel < ActiveRecord::Base
acts_as_tag_bearer
end
Tag a model
# single tag
instance.tag(key: 'environment', value: 'development')
# multiple tags
instance.assign_tag(environment: 'development', owner: 'appowner')
Get a tag value on a model
instance.get_tag :environment
# development
Get a list of tag names a model is tagged with
instance.tag_list
# ['environment', 'app_name', 'owner']
Do a complete sync of all tags Note: This is destructive and will remove all tags no longer included
params = [
{key: 'environment', value: 'production'},
{key: 'owner', value: 'you'}
]
instance.full_sync!(params)
Get the model associated with a specific tagging
TagBearer::Tag.first.owner
##<YourModel id: 1, created_at: "2015-05-25 02:25:38", updated_at: "2015-05-25 02:25:38">
Find models of a specific resource that match tag conditions
Model.with_tags(environment: 'production', owner: 'johndoe')
##[<YourModel id: 1, created_at: "2015-05-25 02:25:38", updated_at: "2015-05-25 02:25:38">]
Find resources of all types that match a tag key/value set
TagBearer.resources_with_tags(owner: 'Jim Lahey')
##{:TaggableModel=>[#<TaggableModel id: 1, ...>, #<TaggableModel id: 2, ....>], :AnotherTaggableModel=>[#<AnotherTaggableModel id: 1, ...>]}