The Rails tag plugin acts_as_taggable_on is a handy drop-in feature set for adding tags to your RoR applications. The documentation is slightly more than minimal, and upon peeking under the hood, you find almost no comments.
I am in the process of adding a caching table that will be updated asynchronous to the user request life-cycle. In doing so I documented EVERYTHING in the main file.
If this helps, please leave a comment or trackback.
I’ll get round to publishing the caching system once it’s done and dusted.
The original plugin is here:
http://www.intridea.com/2008/6/9/acts-as-taggable-on-grows-up http://github.com/mbleigh/acts-as-taggable-on/tree/master
Alternatives I passed over are here:
http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/ http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids
The plugin’s functions re-documented:
###################
# acts_as_taggable_on
###################
# EXAMPLE
####################
class Record "archive"
@rec.mytag_list = "boring, boxy, brown" # Sets tag list
@rec.tags # => [,,]
# AR RELATIONSHIPS:
####################
@rec.mytag_taggings # References related records on "taggables" table
@rec.base_tags # References related records on "tags" table
# INSTANCE METHODS:
####################
@rec.tag_types # [:associations, :tags]
@rec.is_taggable? # true
@rec.is_taggable? # confirms if tags can be added?
@rec.custom_contexts # ??
@rec.add_custom_context(value) # ??
@rec.set_tag_list_on(:mytags, "some, tags") # Context version of tag_list=
@rec.tag_list_on(:mytags) # Context version of tag_list()
@rec.tags_on(:mytags) # Context verison of base_tags()
# Dynamically created based on tag contexts
@rec.#{tag_type}_list # Context :mytags ... mytag_list()
@rec.#{tag_type}_list=(new_tags) # Context :mytags ... mytag_list=
@rec.#{tag_type}_counts(options = {}) # Context :mytags ... mytag_counts()
@rec.#{tag_type}_from(owner) # Context :mytags ... mytag_from()
@rec.find_related_#{tag_type}(options = {}) # Context :mytags ... find_related_mytag
@rec.find_related_#{tag_type}_for(klass, options = {}) # Context :mytags ... find_related_mytag_for()
@rec.find_related_on_#{tag_type} # Alias of find_related_mytag
@rec.tag_counts_on(:on => :mytag) # Returns tags of the provided context
@rec.related_search_options #
@rec.reload_with_tag_list #
@rec.save_cached_tag_list # called by :before_save filter
@rec.save_tags # called by :after_save filter
# SINGLETON METHODS:
####################
Record.tagged_with("awesome", :on => :skills) # Get all tags
Record.find_tagged_with("comp", :on => :tags) # same but does not uses "named_scope"
Record.caching_tag_list_on? # Is it in the cache
Record.tag_counts_on(:on => :skills) # Get counts for tag context
Record.find_options_for_find_tagged_with # Gets query options for retrieving
Record.#{tag_type}_counts(options={}) # Context :mytags ... mytag_counts()
Record.find_options_for_tag_counts(options = {}) # Gets query options for counts
# CACHING METHODS (incomplete!)
#######################
@rec.caching_#{tag_type.singularize}_list?
@rec.caching_tag_list_on
** Looks for column called "cached_#{context}_list" on the acts_as_taggable record table
** Not tested much by the original author, but should update before saving via filter
###################
## acts_as_tagger
###################
# EXAMPLE
####################
class User "cabinet, document", :on => :mytags) # adds the tags to @rec in the specified context
@rec.mytags_from(@user) # returns taggings applied by user to @rec
# AR RELATIONSHIPS:
####################
@user.owned_taggings # Returns all "taggings" belonging to user
@user.owned_tags # Returns all tags by join "taggings" belonging to user on "tags"
[...] sacar del plugin un comportamiento no descrito en la documentación, que encontraba por la web, obtener un listado con todas las aplicaciones que estuvieran tageadas con todos los [...]
Ancor Cruz » Funcionalidades “ocultas” de acts_as_taggable_on
March 9th, 2009
It’d be extremely useful to me to understand how to do a
rec.find_tagged_with( (’a', :on => ‘foo’) AND (’b', :on => ‘bar’))
To say that the documentation is sparse is charitable.
Thanks
GregM
March 15th, 2009
Hi Greg. This is not currently possible. The query options for the plugin allow only one “on” option.
The plugin could be extended to support multiple “contexts”, although this would raise the complexity in a number of places. Also the syntax would probably be more like this.
rec.find_tagged_with({ {’a', :on => ‘foo’}, {’b', :on => ‘bar’} })If you don’t mind running two queries, it is easy enough to each result set in your view.
Here is an example…
#In your controller@query1 = MyRec.find_tagged_with('a', :on => 'foo')
@query2 = MyRec.find_tagged_with('b', :on => 'bar')
#And in your view
<%for myquery in {@query1, @query2} do %>
<% for myrec in myquery do %>
<% if myrec.tag_type == "foo" %>
Foo tag: <% myrec.name %>
<% elsif myrec.tag_type == "bar" %>
Bar tag: <% myrec.name %>
<% end %>
<% end %>
<% end %>
BTW, the “tag_type” field is a “placebo” for the real variable,
which I can’t think of right now. I’ll fix this comment when I get
a chance.
admin
March 16th, 2009