Skip to content

Commit

Permalink
Added seperate validation to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwinM committed Oct 2, 2011
1 parent d524a69 commit f14d409
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 40 deletions.
1 change: 1 addition & 0 deletions lib/acts_as_tenant.rb
Expand Up @@ -2,6 +2,7 @@

require "active_record"
require "action_controller"
require "active_model"

#$LOAD_PATH.unshift(File.dirname(__FILE__))

Expand Down
23 changes: 8 additions & 15 deletions lib/acts_as_tenant/model_extensions.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
27 changes: 2 additions & 25 deletions spec/model_extensions_spec.rb
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down

0 comments on commit f14d409

Please sign in to comment.