Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for horizontally sharded database projects #1079

Merged
merged 12 commits into from
Apr 29, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/acts-as-taggable-on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ','
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/tagging.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 4 additions & 0 deletions lib/tasks/example/acts_as_taggable_on.rb.example
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions lib/tasks/install_initializer.rake
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions spec/acts_as_taggable_on/tagging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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