Run tests for ActsAsTaggableOn
masterThe gem uses RSpec for testing. To run the specs within the gem directory, use:
bundle
rake specrepository·master·Indexed 26 days ago
https://github.com/mbleigh/acts-as-taggable-onA 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).
The gem uses RSpec for testing. To run the specs within the gem directory, use:
bundle
rake specIf 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;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
endadd_tenant_to_taggings migration.ActsAsTaggableOn.force_binary_collation = true in an initializer file or by running the provided rake task.Add the gem to your Gemfile and run bundle to install it.
gem 'acts-as-taggable-on'bundleTo 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 %>ActsAsTaggableOn module. Common configurations include tag cleanup, casing, parameterization, and table naming.ActsAsTaggableOn.setup to configure the gem's behavior. You can pass a block to the setup method to set various configuration options on the Configuration object. Once configured, you can access these settings directly via the ActsAsTaggableOn module.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"]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)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%)