Skip to content

Commit

Permalink
fixed spec; :uniqueness validator not working with tenant scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwinM committed Oct 1, 2011
1 parent e5aea96 commit d524a69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
7 changes: 3 additions & 4 deletions README.md
@@ -1,6 +1,6 @@
Acts As Tenant
==============
This gem was born out of our own need for a fail-safe and out-of-the-way manner to add multi-tenancy to a Rails app, that integrates (near) seamless with Rails.
This gem was born out of our own need for a fail-safe and out-of-the-way manner to add multi-tenancy to a Rails app with a shared database scheme, that integrates (near) seamless with Rails.

Acts_As_Tenant adds the ability to scope models to a tenant model, such as an account. Acts_As_Tenant will set the current tenant for you and ensures all 'tenant models' are always properly scoped to the current tenant: when viewing, searching and creating.

Expand Down Expand Up @@ -54,7 +54,7 @@ Scoping your models
acts_as_tenant(:account)
end

Acts_As_Tenant requires each scoped model to have a column in its db schema identifying its tenant. Adding acts_as_tenant to your model declaration will scope that model to the current tenant **if a current tenant has been set**.
Acts_As_Tenant requires each scoped model to have a column in its schema linking it to a tenant. Adding acts_as_tenant to your model declaration will scope that model to the current tenant **if a current tenant has been set**.
Acts_As_Tenant.current_tenant = Account.find(3)

# New objects are scoped to the current tenant
Expand All @@ -77,15 +77,14 @@ Acts_As_Tenant requires each scoped model to have a column in its db schema iden
* validates uniqueness of limitation

=== To Do
* validates :uniqueness =>

=== Bug reports & suggested improvements


=== Maintained by

=== Credits
This gem is used the Multitenant gem by Ryan Sonnek as a starting point and some of his code to set the default_scope is reused.
This gem used the Multitenant gem by Ryan Sonnek as a starting point and some of his code to set the default_scope is reused.

== License
Copyright (c) 2011 Erwin Matthijssen, released under the MIT license
1 change: 0 additions & 1 deletion lib/acts_as_tenant/model_extensions.rb
Expand Up @@ -87,7 +87,6 @@ def scoped_validates_uniqueness_of(fields, args = {})
if respond_to?(:is_scoped_by_tenant?)
raise "ActsAsTenant: :scope argument of uniqueness validator is not available for classes that are scoped by acts_as_tenant" if args.has_key?(:scope)
args[:scope] = lambda { "#{ActsAsTenant.tenant_class.to_s.downcase}_id"}.call
puts "#{ActsAsTenant.tenant_class.to_s.downcase}_id"
end
ret = original_validates_uniqueness_of(fields, args)
end
Expand Down
31 changes: 29 additions & 2 deletions spec/model_extensions_spec.rb
Expand Up @@ -18,6 +18,10 @@
t.column :completed, :boolean
end

create_table :countries, :force => true do |t|
t.column :name, :string
end

create_table :cities, :force => true do |t|
t.column :name, :string
end
Expand All @@ -44,11 +48,16 @@ class Task < ActiveRecord::Base
validates_uniqueness_of :name
end

class City < ActiveRecord::Base
#validates_uniqueness_of :name
class Country < ActiveRecord::Base
acts_as_tenant :account
validates :name, :uniqueness => true
end

class City < ActiveRecord::Base
validates_uniqueness_of :name
#validates :name, :uniqueness => true
end


# Start testing!
describe ActsAsTenant do
Expand Down Expand Up @@ -163,6 +172,24 @@ class City < ActiveRecord::Base
end
end

describe 'When using validates :uniqueness => true in a aat model' do
before do
@account = Account.create!(:name => 'foo')
ActsAsTenant.current_tenant = @account
@country1 = Country.create!(:name => 'bar')
end

it 'should not be possible to create a duplicate within the same tenant' do
@project2 = Country.create(:name => 'bar').valid?.should == false
end

it 'should be possible to create a duplicate outside the tenant scope' do
@other_account = Account.create!(:name => 'baz')
ActsAsTenant.current_tenant = @other_account
@country2 = Country.create(:name => 'bar').valid?.should == true
end
end

describe 'When using validates_uniqueness_of in a NON-aat model' do
before do
@city1 = City.create!(:name => 'foo')
Expand Down

0 comments on commit d524a69

Please sign in to comment.