Project

tag_bearer

0.0
No commit activity in last 3 years
No release in over 3 years
Key value tagging gem for ActiveRecord models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 4.2
~> 10.4
 Project Readme

Tag Bearer Travis CI Code Climate Test Coverage Gem Version

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, ...>]}