acts-as-taggable-on

repository·master·Indexed 26 days ago

https://github.com/mbleigh/acts-as-taggable-on

A Ruby plugin for ActiveRecord models that enables multiple, arbitrary tag contexts (e.g., skills, interests) rather than a single tags keyword. It provides tools for managing tag lists, filtering objects via the tagged_with scope, finding related objects, implementing tag ownership and tenancy, and generating tag clouds. Supports various Ruby and Rails versions from v3.x up to v12.x (Rails 8.0).

Tokens
4.5K
Snippets
21
Records
39
Agent score
89%

What's inside acts-as-taggable-on

  1. Apply binary collation for MySql (Legacy versions <= 3.4.4)

    master

    If you are using a version of the gem older than 3.4.4 and need to approximate the force_binary_collation behavior in MySql, you must manually alter the tags table via the MySql console.

    USE my_wonderful_app_db;
    ALTER TABLE tags MODIFY name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin;
  2. Setup ActsAsTaggableOn in ActiveRecord models

    master

    To enable tagging, add acts_as_taggable_on to your model. You can define multiple tag contexts (e.g., :tags, :skills, :interests) within a single model. For Rails 4+ applications, ensure you permit :tag_list in your strong parameters in the controller.

    class User < ActiveRecord::Base
      acts_as_taggable_on :tags
      acts_as_taggable_on :skills, :interests # Multiple tag types
    end
    
    class UsersController < ApplicationController
      def user_params
        params.require(:user).permit(:name, :tag_list)
      end
    end
  3. Generate Tag Clouds

    master

    To generate tag clouds, use tag_counts_on to get frequency data and the ActsAsTaggableOn::TagsHelper to render them in your views.

    # Controller
    module PostsHelper
      include ActsAsTaggableOn::TagsHelper
    end
    
    class PostController < ApplicationController
      def tag_cloud
        @tags = Post.tag_counts_on(:tags)
      end
    end
    
    # View (ERB)
    <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
      <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
    <% end %>
  4. Use ordered tags with acts_as_ordered_taggable

    master

    To preserve the specific order in which tags were created/assigned, use acts_as_ordered_taggable (which is an alias for acts_as_ordered_taggable_on :tags) or acts_as_ordered_taggable_on with specific contexts.

    class User < ActiveRecord::Base
      acts_as_ordered_taggable
      acts_as_ordered_taggable_on :skills, :interests
    end
    
    @user.tag_list = "north, east, south, west"
    @user.save
    @user.reload
    @user.tag_list # => ["north", "east", "south", "west"]
  5. Implement Tag Tenancy

    master

    Use acts_as_taggable_tenant to scope tags to a specific tenant (e.g., an Account). This ensures that tags are only retrieved or created within the context of that tenant.

    class User < ActiveRecord::Base
      belongs_to :account
      acts_as_taggable_on :tags
      acts_as_taggable_tenant :account_id
    end
    
    # Tags will be scoped to the account
    ActsAsTaggableOn::Tag.for_tenant(@user.account_id)
  6. Filter objects using tagged_with

    master

    Use the tagged_with scope to filter models based on tags.

    Options:

    • :match_all => true: Returns objects that have the exact set of specified tags and no others.
    • :any => true: Returns objects that have any of the specified tags.
    • :exclude => true: Returns objects that do not have the specified tags.
    • :on => :context: Specifies which tag context to search (e.g., :skills).
    • start_at / end_at: Filters tags created within a specific time range.
    • wild: Supports wildcard searches:
      • wild: :suffix (e.g., awesome%)
      • wild: :prefix (e.g., %awesome)
      • wild: true (e.g., %awesome%)