Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#issue-27] add concerns for tenancy and non-tenancy, and make sure a…
…ccount includes it
- Loading branch information
Showing
5 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Plutus | ||
module NoTenancy | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
validates :name, presence: true, uniqueness: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Plutus | ||
module Tenancy | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
validates :name, presence: true, uniqueness: { scope: :tenant_id } | ||
|
||
belongs_to :tenant, class_name: Plutus.tenant_class | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'spec_helper' | ||
|
||
module Plutus | ||
describe Account do | ||
describe 'tenancy support' do | ||
before(:each) do | ||
ActiveSupportHelpers.clear_model('Account') | ||
ActiveSupportHelpers.clear_model('Asset') | ||
|
||
Plutus.enable_tenancy = true | ||
Plutus.tenant_class = 'Plutus::Entry' | ||
|
||
FactoryGirlHelpers.reload() | ||
Plutus::Asset.new | ||
end | ||
|
||
after(:each) do | ||
if Plutus.const_defined?(:Asset) | ||
ActiveSupportHelpers.clear_model('Account') | ||
ActiveSupportHelpers.clear_model('Asset') | ||
end | ||
|
||
Plutus.enable_tenancy = false | ||
Plutus.tenant_class = nil | ||
|
||
FactoryGirlHelpers.reload() | ||
end | ||
|
||
it 'validate uniqueness of name scoped to tenant' do | ||
account = FactoryGirl.create(:asset, tenant_id: 10) | ||
|
||
record = FactoryGirl.build(:asset, name: account.name, tenant_id: 10) | ||
record.should_not be_valid | ||
record.errors[:name].should == ['has already been taken'] | ||
end | ||
|
||
it 'allows same name scoped under a different tenant' do | ||
account = FactoryGirl.create(:asset, tenant_id: 10) | ||
|
||
record = FactoryGirl.build(:asset, name: account.name, tenant_id: 11) | ||
record.should be_valid | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ActiveSupportHelpers | ||
# Helps in removing model, and force-reloading it next time This helper does 2 | ||
# things: | ||
# * remove from $LOADED_FEATURES so that ruby 'require' reloads file again | ||
# * remove the constant from active support dependencies | ||
def self.clear_model(model_name) | ||
ActiveSupport::Dependencies.remove_constant('Plutus::' + model_name) | ||
|
||
models_dir = File.dirname(__FILE__) + '/../../app/models/plutus/' | ||
path = File.expand_path(models_dir + model_name.downcase + '.rb') | ||
$LOADED_FEATURES.delete(path) | ||
end | ||
end |