Skip to content

Commit

Permalink
Merge pull request #910 from taciknowledge/rename_table
Browse files Browse the repository at this point in the history
customizable table names
  • Loading branch information
seuros committed Aug 13, 2018
2 parents 231a857 + 2427e66 commit 6932226
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Each change should fall into categories that would affect whether the release is
As such, _Breaking Changes_ are major. _Features_ would map to either major or minor. _Fixes_, _Performance_, and _Misc_ are either minor or patch, the difference being kind of fuzzy for the purposes of history. Adding _Documentation_ (including tests) would be patch level.

### [Master / Unreleased](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.0.1...master)
* Features
* [@mizukami234 @junmoka Make table names configurable](https://github.com/mbleigh/acts-as-taggable-on/pull/910)
* Fixes
* [@tonyta Avoid overriding user-defined columns cache methods](https://github.com/mbleigh/acts-as-taggable-on/pull/911)
* Misc
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ If you would like to have an exact match covering special characters with MySql:
ActsAsTaggableOn.force_binary_collation = true
```

If you would like to specify table names:

```ruby
ActsAsTaggableOn.tags_table = 'aato_tags'
ActsAsTaggableOn.taggings_table = 'aato_taggings'
```

If you want to change the default delimiter (it defaults to ','). You can also pass in an array of delimiters such as ([',', '|']):

```ruby
Expand Down
15 changes: 8 additions & 7 deletions db/migrate/1_acts_as_taggable_on_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
end
ActsAsTaggableOnMigration.class_eval do
def self.up
create_table :tags do |t|
create_table ActsAsTaggableOn.tags_table do |t|
t.string :name
t.timestamps
end

create_table :taggings do |t|
t.references :tag
create_table ActsAsTaggableOn.taggings_table do |t|
t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }

# You should make sure that the column created is
# long enough to store the required class names.
Expand All @@ -24,12 +25,12 @@ def self.up
t.datetime :created_at
end

add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
add_index ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
drop_table :taggings
drop_table :tags
drop_table ActsAsTaggableOn.taggings_table
drop_table ActsAsTaggableOn.tags_table
end
end
16 changes: 8 additions & 8 deletions db/migrate/2_add_missing_unique_indices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
def self.up
add_index :tags, :name, unique: true
add_index ActsAsTaggableOn.tags_table, :name, unique: true

remove_index :taggings, :tag_id if index_exists?(:taggings, :tag_id)
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings,
remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx'
end

def self.down
remove_index :tags, :name
remove_index ActsAsTaggableOn.tags_table, :name

remove_index :taggings, name: 'taggings_idx'
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'

add_index :taggings, :tag_id unless index_exists?(:taggings, :tag_id)
add_index :taggings, [:taggable_id, :taggable_type, :context]
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end
end
6 changes: 3 additions & 3 deletions db/migrate/3_add_taggings_counter_cache_to_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class AddTaggingsCounterCacheToTags < ActiveRecord::Migration; end
end
AddTaggingsCounterCacheToTags.class_eval do
def self.up
add_column :tags, :taggings_count, :integer, default: 0
add_column ActsAsTaggableOn.tags_table, :taggings_count, :integer, default: 0

ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
ActsAsTaggableOn::Tag.reset_counters(tag.id, ActsAsTaggableOn.taggings_table)
end
end

def self.down
remove_column :tags, :taggings_count
remove_column ActsAsTaggableOn.tags_table, :taggings_count
end
end
4 changes: 2 additions & 2 deletions db/migrate/4_add_missing_taggable_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
end
end
2 changes: 1 addition & 1 deletion db/migrate/5_change_collation_for_tag_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ChangeCollationForTagNames < ActiveRecord::Migration; 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;")
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
18 changes: 9 additions & 9 deletions db/migrate/6_add_missing_indexes_on_taggings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddMissingIndexesOnTaggings.class_eval do
def change
add_index :taggings, :tag_id unless index_exists? :taggings, :tag_id
add_index :taggings, :taggable_id unless index_exists? :taggings, :taggable_id
add_index :taggings, :taggable_type unless index_exists? :taggings, :taggable_type
add_index :taggings, :tagger_id unless index_exists? :taggings, :tagger_id
add_index :taggings, :context unless index_exists? :taggings, :context
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_id
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_type
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context

unless index_exists? :taggings, [:tagger_id, :tagger_type]
add_index :taggings, [:tagger_id, :tagger_type]
unless index_exists? ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
end

unless index_exists? :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
add_index :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
unless index_exists? ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
end
end
end
5 changes: 4 additions & 1 deletion lib/acts-as-taggable-on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def self.glue
class Configuration
attr_accessor :force_lowercase, :force_parameterize,
:remove_unused_tags, :default_parser,
:tags_counter
:tags_counter, :tags_table,
:taggings_table
attr_reader :delimiter, :strict_case_match

def initialize
Expand All @@ -75,6 +76,8 @@ def initialize
@tags_counter = true
@default_parser = DefaultParser
@force_binary_collation = false
@tags_table = :tags
@taggings_table = :taggings
end

def strict_case_match=(force_cs)
Expand Down
5 changes: 3 additions & 2 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8
module ActsAsTaggableOn
class Tag < ::ActiveRecord::Base
self.table_name = ActsAsTaggableOn.tags_table

### ASSOCIATIONS:

Expand Down Expand Up @@ -50,8 +51,8 @@ def self.named_like_any(list)

def self.for_context(context)
joins(:taggings).
where(["taggings.context = ?", context]).
select("DISTINCT tags.*")
where(["#{ActsAsTaggableOn.taggings_table}.context = ?", context]).
select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end

### CLASS METHODS:
Expand Down
2 changes: 2 additions & 0 deletions lib/acts_as_taggable_on/tagging.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ActsAsTaggableOn
class Tagging < ::ActiveRecord::Base #:nodoc:
self.table_name = ActsAsTaggableOn.taggings_table

DEFAULT_CONTEXT = 'tags'
belongs_to :tag, class_name: '::ActsAsTaggableOn::Tag', counter_cache: ActsAsTaggableOn.tags_counter
belongs_to :taggable, polymorphic: true
Expand Down
2 changes: 1 addition & 1 deletion spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
taggable1.save

column = TaggableModel.connection.quote_column_name("context")
offer_alias = TaggableModel.connection.quote_table_name("taggings")
offer_alias = TaggableModel.connection.quote_table_name(ActsAsTaggableOn.taggings_table)
need_alias = TaggableModel.connection.quote_table_name("need_taggings_taggable_models_join")

expect(TaggableModel.joins(:offerings, :needs).to_sql).to include "#{offer_alias}.#{column}"
Expand Down
12 changes: 6 additions & 6 deletions spec/acts_as_taggable_on/single_table_inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@
altered_inheriting.tag_list = 'fork, spoon'
altered_inheriting.save!

expect(InheritingTaggableModel.tag_counts_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(bob kelso))
expect(AlteredInheritingTaggableModel.tag_counts_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(fork spoon))
expect(TaggableModel.tag_counts_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(bob kelso fork spoon))
expect(InheritingTaggableModel.tag_counts_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(bob kelso))
expect(AlteredInheritingTaggableModel.tag_counts_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(fork spoon))
expect(TaggableModel.tag_counts_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(bob kelso fork spoon))
end

it 'should have different tags_on for inherited models' do
Expand All @@ -138,9 +138,9 @@
altered_inheriting.tag_list = 'fork, spoon'
altered_inheriting.save!

expect(InheritingTaggableModel.tags_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(bob kelso))
expect(AlteredInheritingTaggableModel.tags_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(fork spoon))
expect(TaggableModel.tags_on(:tags, order: 'tags.id').map(&:name)).to eq(%w(bob kelso fork spoon))
expect(InheritingTaggableModel.tags_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(bob kelso))
expect(AlteredInheritingTaggableModel.tags_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(fork spoon))
expect(TaggableModel.tags_on(:tags, order: "#{ActsAsTaggableOn.tags_table}.id").map(&:name)).to eq(%w(bob kelso fork spoon))
end

it 'should store same tag without validation conflict' do
Expand Down
16 changes: 8 additions & 8 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@
@taggables[1].skill_list = 'css'
@taggables.each { |taggable| taggable.save }

@found_taggables_by_tag = TaggableModel.joins(:tags).where(tags: {name: ['bob']})
@found_taggables_by_skill = TaggableModel.joins(:skills).where(tags: {name: ['ruby']})
@found_taggables_by_tag = TaggableModel.joins(:tags).where(ActsAsTaggableOn.tags_table => {name: ['bob']})
@found_taggables_by_skill = TaggableModel.joins(:skills).where(ActsAsTaggableOn.tags_table => {name: ['ruby']})

expect(@found_taggables_by_tag).to include @taggables[0]
expect(@found_taggables_by_tag).to_not include @taggables[1]
Expand Down Expand Up @@ -338,7 +338,7 @@
TaggableModel.create(name: 'Charlie', skill_list: 'ruby')

expect(TaggableModel.all_tag_counts).to_not be_empty
expect(TaggableModel.all_tag_counts(order: 'tags.id').first.count).to eq(3) # ruby
expect(TaggableModel.all_tag_counts(order: "#{ActsAsTaggableOn.tags_table}.id").first.count).to eq(3) # ruby
end

it 'should be able to get all tags on model as whole' do
Expand All @@ -347,7 +347,7 @@
TaggableModel.create(name: 'Charlie', skill_list: 'ruby')

expect(TaggableModel.all_tags).to_not be_empty
expect(TaggableModel.all_tags(order: 'tags.id').first.name).to eq('ruby')
expect(TaggableModel.all_tags(order: "#{ActsAsTaggableOn.tags_table}.id").first.name).to eq('ruby')
end

it 'should be able to use named scopes to chain tag finds by any tags by context' do
Expand All @@ -369,7 +369,7 @@
TaggableModel.create(name: 'Frank', tag_list: 'ruby, rails')
TaggableModel.create(name: 'Charlie', skill_list: 'ruby')

expect(TaggableModel.tagged_with('ruby').tag_counts(order: 'tags.id').first.count).to eq(2) # ruby
expect(TaggableModel.tagged_with('ruby').tag_counts(order: "#{ActsAsTaggableOn.tags_table}.id").first.count).to eq(2) # ruby
expect(TaggableModel.tagged_with('ruby').skill_counts.first.count).to eq(1) # ruby
end

Expand All @@ -378,15 +378,15 @@
TaggableModel.create(name: 'Frank', tag_list: 'ruby, rails')
TaggableModel.create(name: 'Charlie', skill_list: 'ruby')

expect(TaggableModel.tagged_with('ruby').all_tag_counts(order: 'tags.id').first.count).to eq(3) # ruby
expect(TaggableModel.tagged_with('ruby').all_tag_counts(order: "#{ActsAsTaggableOn.tags_table}.id").first.count).to eq(3) # ruby
end

it 'should be able to get all scoped tags' do
TaggableModel.create(name: 'Bob', tag_list: 'ruby, rails, css')
TaggableModel.create(name: 'Frank', tag_list: 'ruby, rails')
TaggableModel.create(name: 'Charlie', skill_list: 'ruby')

expect(TaggableModel.tagged_with('ruby').all_tags(order: 'tags.id').first.name).to eq('ruby')
expect(TaggableModel.tagged_with('ruby').all_tags(order: "#{ActsAsTaggableOn.tags_table}.id").first.name).to eq('ruby')
end

it 'should only return tag counts for the available scope' do
Expand Down Expand Up @@ -672,7 +672,7 @@
# NOTE: type column supports an STI Tag subclass in the test suite, though
# isn't included by default in the migration generator
expect(@taggable.grouped_column_names_for(ActsAsTaggableOn::Tag))
.to eq('tags.id, tags.name, tags.taggings_count, tags.type')
.to eq("#{ActsAsTaggableOn.tags_table}.id, #{ActsAsTaggableOn.tags_table}.name, #{ActsAsTaggableOn.tags_table}.taggings_count, #{ActsAsTaggableOn.tags_table}.type")
end

it 'should return all column names joined for TaggableModel GROUP clause' do
Expand Down
10 changes: 5 additions & 5 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ActiveRecord::Schema.define version: 0 do
create_table :tags, force: true do |t|
create_table ActsAsTaggableOn.tags_table, force: true do |t|
t.string :name
t.integer :taggings_count, default: 0
t.string :type
end
add_index 'tags', ['name'], name: 'index_tags_on_name', unique: true
add_index ActsAsTaggableOn.tags_table, ['name'], name: 'index_tags_on_name', unique: true

create_table :taggings, force: true do |t|
create_table ActsAsTaggableOn.taggings_table, force: true do |t|
t.integer :tag_id

# You should make sure that the column created is
Expand All @@ -23,10 +23,10 @@

t.datetime :created_at
end
add_index 'taggings',
add_index ActsAsTaggableOn.taggings_table,
['tag_id', 'taggable_id', 'taggable_type', 'context', 'tagger_id', 'tagger_type'],
unique: true, name: 'taggings_idx'
add_index 'taggings', :tag_id , name: 'index_taggings_on_tag_id'
add_index ActsAsTaggableOn.taggings_table, :tag_id , name: 'index_taggings_on_tag_id'

# above copied from
# generators/acts_as_taggable_on/migration/migration_generator
Expand Down

0 comments on commit 6932226

Please sign in to comment.