Skip to content

Commit

Permalink
Merge pull request #1 from hazelsparrow/fix-polymorphic-tenant
Browse files Browse the repository at this point in the history
fix polymorphic tenant
  • Loading branch information
hazelsparrow authored Nov 27, 2018
2 parents c9d51f4 + bd570c5 commit e7bddb3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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

0 comments on commit e7bddb3

Please sign in to comment.