Reputation: 629
I'm using two gems in an Rails 4 application: acts_as_tenant and simple_hashtags.
If a hashtag exist on one tenant, it won't be resaved for the other tenant. So I want to override the find_or_create_by_name and find_by_name methods.
For that, I also need to override the parsed_hashtags method, but to make it used by my app, I need also to include the callback
before_save :update_hashtags
I have an initializer that I first used to act the multitenancy system to hashtags (so the tenant_id is automatically saved). I added the methods, but when trying to override the callback, I hit a wall.
If I use the extend ActiveSupport::Concern like in the gist, I get this error and can't start my app.
lib/active_support/concern.rb:126:in `included': Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:16:in `<module:Hashtaggable>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:14:in `<module:SimpleHashtag>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:1:in `<top (required)>'
And if I use the version with
def self.included(base)
base.class_eval do
before_save :do_something
end
end
I get this error, and can start my app, but get the error on any page.
undefined method `before_save' for HashtagConcern:Module
I'm at a loss, those are the only two solutions I can find, and I can't seem to make them works. Is there any other way to use the callback in a module ? Or maybe another way to solve the problem of finding by name and tenant ?
Upvotes: 1
Views: 358
Reputation: 384
In order to get the simple_hashtag be tenant-aware, just override the validation of the Hashtag model, for example like this:
SimpleHashtag::Hashtag.clear_validators!
module SimpleHashtag
class Hashtag < ActiveRecord::Base
acts_as_tenant :tenant
validates :name, :uniqueness => { :scope => :tenant }
end
end
Upvotes: 1