Skip to content

Commit

Permalink
Allow tenant to be set if it is presently nil.
Browse files Browse the repository at this point in the history
This is helpful, for instance, during a multi-step user/account registration process.
  • Loading branch information
hypomodern committed Nov 15, 2013
1 parent eb4ae88 commit 8ffb618
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/acts_as_tenant/model_extensions.rb
Expand Up @@ -76,13 +76,13 @@ def acts_as_tenant(association = :account)
# - Add a helper method to verify if a model has been scoped by AaT
#
define_method "#{ActsAsTenant.fkey}=" do |integer|
raise ActsAsTenant::Errors::TenantIsImmutable unless new_record?
write_attribute("#{ActsAsTenant.fkey}", integer)
raise ActsAsTenant::Errors::TenantIsImmutable unless new_record? || send(ActsAsTenant.fkey).nil?
write_attribute("#{ActsAsTenant.fkey}", integer)
end

define_method "#{ActsAsTenant.tenant_klass.to_s}=" do |model|
raise ActsAsTenant::Errors::TenantIsImmutable unless new_record?
super(model)
define_method "#{ActsAsTenant.tenant_klass.to_s}=" do |model|
raise ActsAsTenant::Errors::TenantIsImmutable unless new_record? || send(ActsAsTenant.fkey).nil?
super(model)
end

def scoped_by_tenant?
Expand Down
14 changes: 12 additions & 2 deletions spec/acts_as_tenant/model_extensions_spec.rb
Expand Up @@ -165,7 +165,7 @@ class SubTask < ActiveRecord::Base
end
end

describe 'tenant_id should be immutable' do
describe 'tenant_id should be immutable, if already set' do
before do
@account = Account.create!(:name => 'foo')
@project = @account.projects.create!(:name => 'bar')
Expand All @@ -174,6 +174,16 @@ class SubTask < ActiveRecord::Base
it { lambda {@project.account_id = @account.id + 1}.should raise_error }
end

describe 'tenant_id should be mutable, if not already set' do
before do
@account = Account.create!(:name => 'foo')
@project = Project.create!(:name => 'bar')
end

it { @project.account_id.should be_nil }
it { lambda { @project.account = @account }.should_not raise_error }
end

describe 'Associations can only be made with in-scope objects' do
before do
@account = Account.create!(:name => 'foo')
Expand Down Expand Up @@ -275,7 +285,7 @@ class SubTask < ActiveRecord::Base
@project1 = @account1.projects.create!(:name => 'foobar')
ActsAsTenant.configuration.stub(require_tenant: true)
end

it "should raise an error when no tenant is provided" do
expect { Project.all.load }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
end
Expand Down

0 comments on commit 8ffb618

Please sign in to comment.