diff --git a/CHANGELOG.md b/CHANGELOG.md index c50a68f11..468c60f70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m ### [v9.0.2) / unreleased](https://github.com/mbleigh/acts-as-taggable-on/compare/v9.0.1...master) * Features * [@glampr Add support for prefix and suffix searches alongside previously supported containment (wildcard) searches](https://github.com/mbleigh/acts-as-taggable-on/pull/1082) + * [@donquxiote Add support for horizontally sharded databases](https://github.com/mbleigh/acts-as-taggable-on/pull/1079) ### [v9.0.1) / 2022-01-07](https://github.com/mbleigh/acts-as-taggable-on/compare/v9.0.0..v9.0.1) * Fixes diff --git a/lib/acts-as-taggable-on.rb b/lib/acts-as-taggable-on.rb index 4922a181c..e7d087420 100644 --- a/lib/acts-as-taggable-on.rb +++ b/lib/acts-as-taggable-on.rb @@ -66,7 +66,7 @@ class Configuration :remove_unused_tags, :default_parser, :tags_counter, :tags_table, :taggings_table - attr_reader :delimiter, :strict_case_match + attr_reader :delimiter, :strict_case_match, :base_class def initialize @delimiter = ',' @@ -79,6 +79,7 @@ def initialize @force_binary_collation = false @tags_table = :tags @taggings_table = :taggings + @base_class = ::ActiveRecord::Base end def strict_case_match=(force_cs) @@ -119,6 +120,11 @@ def self.apply_binary_collation(bincoll) end end + def base_class=(base_class) + raise "base_class must be a class constant" unless base_class.is_a?(Class) + @base_class = base_class + end + end setup diff --git a/lib/acts_as_taggable_on/tag.rb b/lib/acts_as_taggable_on/tag.rb index 6f6e9d023..390361195 100644 --- a/lib/acts_as_taggable_on/tag.rb +++ b/lib/acts_as_taggable_on/tag.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module ActsAsTaggableOn - class Tag < ::ActiveRecord::Base + class Tag < ActsAsTaggableOn.base_class self.table_name = ActsAsTaggableOn.tags_table ### ASSOCIATIONS: diff --git a/lib/acts_as_taggable_on/tagging.rb b/lib/acts_as_taggable_on/tagging.rb index 1bdb8a575..c8635ef0a 100644 --- a/lib/acts_as_taggable_on/tagging.rb +++ b/lib/acts_as_taggable_on/tagging.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module ActsAsTaggableOn - class Tagging < ::ActiveRecord::Base # :nodoc: + class Tagging < ActsAsTaggableOn.base_class # :nodoc: self.table_name = ActsAsTaggableOn.taggings_table DEFAULT_CONTEXT = 'tags' diff --git a/lib/tasks/example/acts_as_taggable_on.rb.example b/lib/tasks/example/acts_as_taggable_on.rb.example new file mode 100644 index 000000000..35e8fd01c --- /dev/null +++ b/lib/tasks/example/acts_as_taggable_on.rb.example @@ -0,0 +1,4 @@ +# This works because the classes where the base class is a concern, Tag and Tagging +# are autoloaded, and won't be started until after the initializers run. + +# ActsAsTaggableOn.base_class = ApplicationRecord diff --git a/lib/tasks/install_initializer.rake b/lib/tasks/install_initializer.rake new file mode 100644 index 000000000..e9f24aba9 --- /dev/null +++ b/lib/tasks/install_initializer.rake @@ -0,0 +1,23 @@ +namespace :acts_as_taggable_on do + + namespace :sharded_db do + + desc "Install initializer setting custom base class" + task :install_initializer => [:environment, "config/initializers/foo"] do + source = File.join( + Gem.loaded_specs["acts-as-taggable-on"].full_gem_path, + "lib", + "tasks", + "examples", + "acts_as_taggable_on.rb.example" + ) + + destination = "config/initializers/acts_as_taggable_on.rb" + + cp source, destination + end + + directory "config/initializers" + end + +end diff --git a/spec/acts_as_taggable_on/tag_spec.rb b/spec/acts_as_taggable_on/tag_spec.rb index b7ba88fdc..a916a4a43 100644 --- a/spec/acts_as_taggable_on/tag_spec.rb +++ b/spec/acts_as_taggable_on/tag_spec.rb @@ -352,4 +352,28 @@ end end + describe 'base_class' do + before do + class Foo < ActiveRecord::Base; end + end + + context "default" do + it "inherits from ActiveRecord::Base" do + + expect(ActsAsTaggableOn::Tag.ancestors).to include(ActiveRecord::Base) + expect(ActsAsTaggableOn::Tag.ancestors).to_not include(Foo) + end + end + + context "custom" do + it "inherits from custom class" do + + ActsAsTaggableOn.base_class = Foo + hide_const("ActsAsTaggableOn::Tag") + load("lib/acts_as_taggable_on/tag.rb") + + expect(ActsAsTaggableOn::Tag.ancestors).to include(Foo) + end + end + end end diff --git a/spec/acts_as_taggable_on/tagging_spec.rb b/spec/acts_as_taggable_on/tagging_spec.rb index 4efc72cdb..dde37f7f7 100644 --- a/spec/acts_as_taggable_on/tagging_spec.rb +++ b/spec/acts_as_taggable_on/tagging_spec.rb @@ -140,4 +140,30 @@ end end end + + describe 'base_class' do + before do + class Foo < ActiveRecord::Base; end + end + + context "default" do + it "inherits from ActiveRecord::Base" do + + expect(ActsAsTaggableOn::Tagging.ancestors).to include(ActiveRecord::Base) + expect(ActsAsTaggableOn::Tagging.ancestors).to_not include(Foo) + end + end + + context "custom" do + it "inherits from custom class" do + + ActsAsTaggableOn.base_class = Foo + hide_const("ActsAsTaggableOn::Tagging") + load("lib/acts_as_taggable_on/tagging.rb") + + expect(ActsAsTaggableOn::Tagging.ancestors).to include(Foo) + end + end + end + end