Skip to content
Browse files

Added seperate validation to simplify code

  • Loading branch information...
1 parent d524a69 commit f14d409a65bc40305dcecbc32f45cba0d55ae543 @ErwinM committed Oct 2, 2011
Showing with 11 additions and 40 deletions.
  1. +1 −0 lib/acts_as_tenant.rb
  2. +8 −15 lib/acts_as_tenant/model_extensions.rb
  3. +2 −25 spec/model_extensions_spec.rb
View
1 lib/acts_as_tenant.rb
@@ -2,6 +2,7 @@
require "active_record"
require "action_controller"
+require "active_model"
#$LOAD_PATH.unshift(File.dirname(__FILE__))
View
23 lib/acts_as_tenant/model_extensions.rb
@@ -12,12 +12,6 @@ module ModelExtensions
extend ActiveSupport::Concern
# Alias the v_uniqueness_of method so we can scope it to the current tenant when relevant
- included do
- class << self
- alias original_validates_uniqueness_of :validates_uniqueness_of unless method_defined?(:original_validates_uniqueness_of)
- alias validates_uniqueness_of :scoped_validates_uniqueness_of
- end
- end
module ClassMethods
@@ -82,15 +76,14 @@ def self.is_scoped_by_tenant?
end
end
end
-
- 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
- end
- ret = original_validates_uniqueness_of(fields, args)
- end
-
+
+ def validates_uniqueness_to_tenant(fields, args ={})
+ raise "ActsAsTenant::validates_uniqueness_to_tenant: no current tenant" unless respond_to?(:is_scoped_by_tenant?)
+ tenant_id = lambda { "#{ActsAsTenant.tenant_class.to_s.downcase}_id"}.call
+ args[:scope].nil? ? args[:scope] = tenant_id : args[:scope] << tenant_id
+ validates_uniqueness_of(fields, args)
+ end
+
end
end
end
View
27 spec/model_extensions_spec.rb
@@ -37,7 +37,7 @@ class Project < ActiveRecord::Base
has_many :tasks
acts_as_tenant :account
- validates_uniqueness_of :name
+ validates_uniqueness_to_tenant :name
end
class Task < ActiveRecord::Base
@@ -48,14 +48,9 @@ class Task < ActiveRecord::Base
validates_uniqueness_of :name
end
-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
@@ -154,7 +149,7 @@ class City < ActiveRecord::Base
it { @task.update_attributes(:project_id => @project1.id).should == false }
end
- describe 'When using validates_uniqueness_of in a aat model' do
+ describe 'When using validates_uniqueness_to_tenant in a aat model' do
before do
@account = Account.create!(:name => 'foo')
ActsAsTenant.current_tenant = @account
@@ -172,24 +167,6 @@ 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')

0 comments on commit f14d409

Please sign in to comment.
Something went wrong with that request. Please try again.