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

fix polymorphic tenant #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/acts_as_tenant/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def acts_as_tenant(tenant = :account, options = {})
before_validation Proc.new {|m|
if ActsAsTenant.current_tenant
if options[:polymorphic]
m.send("#{fkey}=".to_sym, ActsAsTenant.current_tenant.class.to_s) if m.send("#{fkey}").nil?
m.send("#{polymorphic_type}=".to_sym, ActsAsTenant.current_tenant.class.to_s) if m.send("#{polymorphic_type}").nil?
m.send("#{fkey}=".to_sym, ActsAsTenant.current_tenant.id)
m.send("#{polymorphic_type}=".to_sym, ActsAsTenant.current_tenant.class.to_s)
else
m.send "#{fkey}=".to_sym, ActsAsTenant.current_tenant.id
end
Expand Down Expand Up @@ -214,13 +214,13 @@ def validates_uniqueness_to_tenant(fields, args ={})
if instance.new_record?
unless self.class.where(fkey.to_sym => [nil, instance[fkey]],
field.to_sym => instance[field]).empty?
errors.add(field, 'has already been taken')
errors.add(field, 'has already been taken')
end
else
unless self.class.where(fkey.to_sym => [nil, instance[fkey]],
field.to_sym => instance[field])
.where.not(:id => instance.id).empty?
errors.add(field, 'has already been taken')
errors.add(field, 'has already been taken')
end

end
Expand Down
6 changes: 4 additions & 2 deletions spec/acts_as_tenant/model_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,16 @@
before do
@comment.save!
@article = Article.create!(id: @project.id, title: 'article title')
@comment_on_article = @article.polymorphic_tenant_comments.create!
ActsAsTenant.with_tenant(@article) do
@comment_on_article = @article.polymorphic_tenant_comments.create!
end
end

it 'correctly scopes to the current tenant type' do
expect(@comment_on_article).to be_persisted
expect(@comment).to be_persisted
expect(PolymorphicTenantComment.count).to eql(1)
expect(PolymorphicTenantComment.all.first.attributes).to eql(@comment.attributes)
expect(PolymorphicTenantComment.first.attributes).to eql(@comment.attributes)
end
end

Expand Down