Skip to content

Commit

Permalink
ensure unique account names
Browse files Browse the repository at this point in the history
  • Loading branch information
mbulat committed May 15, 2012
1 parent 4fcc66a commit e94836f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/models/plutus/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Account < ActiveRecord::Base
has_many :debit_transactions, :through => :debit_amounts, :source => :transaction

validates_presence_of :type, :name
validates_uniqueness_of :name

# The trial balance of all accounts in the system. This should always equal zero,
# otherwise there is an error in the system.
Expand Down
16 changes: 10 additions & 6 deletions spec/factories/account_factory.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
FactoryGirl.define do
factory :account, :class => Plutus::Account do |account|
account.name 'factory name'
account.name
account.contra false
end

factory :asset, :class => Plutus::Asset do |account|
account.name 'factory name'
account.name
account.contra false
end

factory :equity, :class => Plutus::Equity do |account|
account.name 'factory name'
account.name
account.contra false
end

factory :expense, :class => Plutus::Expense do |account|
account.name 'factory name'
account.name
account.contra false
end

factory :liability, :class => Plutus::Liability do |account|
account.name 'factory name'
account.name
account.contra false
end

factory :revenue, :class => Plutus::Revenue do |account|
account.name 'factory name'
account.name
account.contra false
end

sequence :name do |n|
"Factory Name #{n}"
end
end
7 changes: 7 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module Plutus
account.should_not be_valid
end

it "should be unique per name" do
FactoryGirl.create(:account, :name => "Test1", :type => "Plutus::Asset")
account = FactoryGirl.build(:account, :name => "Test1", :type => "Plutus::Asset")
account.should_not be_valid
account.errors[:name].should == ["has already been taken"]
end

it "should not have a balance method" do
lambda{Account.balance}.should raise_error(NoMethodError)
end
Expand Down

0 comments on commit e94836f

Please sign in to comment.