Skip to content

Commit

Permalink
[#issue-27] add concerns for tenancy and non-tenancy, and make sure a…
Browse files Browse the repository at this point in the history
…ccount includes it
  • Loading branch information
iffyuva committed Oct 27, 2014
1 parent 12b3137 commit 6200c54
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/models/plutus/account.rb
Expand Up @@ -35,8 +35,13 @@ class Account < ActiveRecord::Base
has_many :credit_entries, :through => :credit_amounts, :source => :entry, :class_name => 'Plutus::Entry'
has_many :debit_entries, :through => :debit_amounts, :source => :entry, :class_name => 'Plutus::Entry'

validates_presence_of :type, :name
validates_uniqueness_of :name
validates_presence_of :type

if Plutus.enable_tenancy
include Tenancy
else
include NoTenancy
end

# The credit balance for the account.
#
Expand Down
9 changes: 9 additions & 0 deletions app/models/plutus/no_tenancy.rb
@@ -0,0 +1,9 @@
module Plutus
module NoTenancy
extend ActiveSupport::Concern

included do
validates :name, presence: true, uniqueness: true
end
end
end
11 changes: 11 additions & 0 deletions app/models/plutus/tenancy.rb
@@ -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
45 changes: 45 additions & 0 deletions spec/models/tenancy_spec.rb
@@ -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
13 changes: 13 additions & 0 deletions spec/support/active_support_helpers.rb
@@ -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

0 comments on commit 6200c54

Please sign in to comment.