Skip to content

Instantly share code, notes, and snippets.

@rokumatsumoto
Last active June 19, 2020 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokumatsumoto/e7225334767c68c1859e097442f647a1 to your computer and use it in GitHub Desktop.
Save rokumatsumoto/e7225334767c68c1859e097442f647a1 to your computer and use it in GitHub Desktop.
Configure ActsAsTaggableOn gem (acts-as-taggable-on, acts as taggable on, tagging, tags)

After configured(28/06/2019) acts-as-taggable-on gem, I found gutentag gem and switched to this.

acts-as-taggable-on gem still has problems with generating migrations. See issue #845

My shared database migration files compatible with strong migrations and has no problems.

Here is the list of configured files.

  • config/initializers/acts_as_taggable.rb
  • *_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb
  • *_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb
  • *_add_missing_taggable_index.acts_as_taggable_on_engine.rb
  • *_add_missing_unique_indices.acts_as_taggable_on_engine.rb
  • *_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb
  • *_change_collation_for_tag_names.acts_as_taggable_on_engine.rb
  • Gemfile
  • schema.rb
# config/initializers/acts_as_taggable.rb
ActsAsTaggableOn.force_lowercase = true
# tags_counter enables caching count of tags which results in an update whenever a tag is added or removed
# since the count is not used anywhere its better performance wise to disable this cache
# see https://github.com/mbleigh/acts-as-taggable-on/wiki#counter-cache
ActsAsTaggableOn.tags_counter = false
# validate that counter cache is disabled
raise "Counter cache is not disabled" if
ActsAsTaggableOn::Tagging.reflections["tag"].options[:counter_cache]
# This migration comes from acts_as_taggable_on_engine (originally 1)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]; end
else
class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
end
ActsAsTaggableOnMigration.class_eval do
def self.up
create_table :tags do |t|
t.string :name
end
create_table :taggings do |t|
t.references :tag
# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true
# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128
t.datetime :created_at
end
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
drop_table :taggings
drop_table :tags
end
end
# This migration comes from acts_as_taggable_on_engine (originally 6)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[4.2]; end
else
class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddMissingIndexesOnTaggings.class_eval do
disable_ddl_transaction!
def change
add_index :taggings, :tag_id, algorithm: :concurrently unless index_exists? :taggings, :tag_id
add_index :taggings, :taggable_id, algorithm: :concurrently unless index_exists? :taggings, :taggable_id
add_index :taggings, :taggable_type, algorithm: :concurrently unless index_exists? :taggings, :taggable_type
add_index :taggings, :tagger_id, algorithm: :concurrently unless index_exists? :taggings, :tagger_id
add_index :taggings, :context, algorithm: :concurrently unless index_exists? :taggings, :context
unless index_exists? :taggings, [:tagger_id, :tagger_type]
add_index :taggings, [:tagger_id, :tagger_type], algorithm: :concurrently
end
unless index_exists? :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
safety_assured { add_index :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy', algorithm: :concurrently }
end
end
end
# This migration comes from acts_as_taggable_on_engine (originally 4)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingTaggableIndex < ActiveRecord::Migration[4.2]; end
else
class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
disable_ddl_transaction!
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context], algorithm: :concurrently
end
def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
# This migration comes from acts_as_taggable_on_engine (originally 2)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingUniqueIndices < ActiveRecord::Migration[4.2]; end
else
class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
disable_ddl_transaction!
def self.up
add_index :tags, :name, unique: true, algorithm: :concurrently unless index_exists?(:tags, :name)
remove_index :taggings, :tag_id if index_exists?(:taggings, :tag_id)
if index_exists? :taggings, [:taggable_id, :taggable_type, :context]
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
add_index :taggings,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx', algorithm: :concurrently
end
def self.down
remove_index :tags, :name
remove_index :taggings, name: 'taggings_idx'
add_index :taggings, :tag_id unless index_exists?(:taggings, :tag_id)
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
# This migration comes from acts_as_taggable_on_engine (originally 3)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[4.2]; end
else
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration; end
end
AddTaggingsCounterCacheToTags.class_eval do
def self.up
add_column :tags, :taggings_count, :integer, default: 0
ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
end
def self.down
remove_column :tags, :taggings_count
end
end
# This migration comes from acts_as_taggable_on_engine (originally 5)
# This migration is added to circumvent issue #623 and have special characters
# work properly
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ChangeCollationForTagNames < ActiveRecord::Migration[4.2]; end
else
class ChangeCollationForTagNames < ActiveRecord::Migration; end
end
ChangeCollationForTagNames.class_eval do
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
gem 'acts-as-taggable-on', '~> 6.0'
create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.string "tagger_type"
t.integer "tagger_id"
t.string "context", limit: 128
t.datetime "created_at"
t.index ["context"], name: "index_taggings_on_context"
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
end
create_table "tags", id: :serial, force: :cascade do |t|
t.string "name"
t.integer "taggings_count", default: 0
t.index ["name"], name: "index_tags_on_name", unique: true
end
@milkfarm
Copy link

Thanks for sharing! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment