Skip to content

Commit

Permalink
adding validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bulat committed Apr 20, 2010
1 parent d127a5d commit 46c45fa
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
# @author Michael Bulat
class Account < ActiveRecord::Base

validates_presence_of :type
validates_presence_of :type, :name

has_many :credit_transactions, :class_name => "Transaction", :foreign_key => "credit_account_id"
has_many :debit_transactions, :class_name => "Transaction", :foreign_key => "debit_account_id"
3 changes: 3 additions & 0 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
@@ -13,4 +13,7 @@
class Transaction < ActiveRecord::Base
belongs_to :credit_account, :class_name => "Account"
belongs_to :debit_account, :class_name => "Account"

validates_presence_of :credit_account, :debit_account
validates_associated :credit_account, :debit_account
end
5 changes: 5 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
@@ -15,5 +15,10 @@
Account.should respond_to(:trial_balance)
Account.trial_balance.should be_kind_of(BigDecimal)
end

it "should not be valid without a name" do
asset = Factory.build(:asset, :name => nil)
asset.should_not be_valid
end

end
24 changes: 23 additions & 1 deletion spec/models/transaction.rb
Original file line number Diff line number Diff line change
@@ -3,7 +3,29 @@
describe Transaction do

it "should create a transaction" do
account = Factory(:transaction)
transaction = Factory(:transaction)
end

it "should not be valid without a credit account" do
transaction = Factory.build(:transaction, :credit_account => nil)
transaction.should_not be_valid
end

it "should not be valid without a valid credit account" do
bad_account = Factory.build(:asset, :name => nil)
transaction = Factory.build(:transaction, :credit_account => bad_account)
transaction.should_not be_valid
end

it "should not be valid without a debit account" do
transaction = Factory.build(:transaction, :debit_account => nil)
transaction.should_not be_valid
end

it "should not be valid without a valid credit account" do
bad_account = Factory.build(:asset, :name => nil)
transaction = Factory.build(:transaction, :debit_account => bad_account)
transaction.should_not be_valid
end

end

0 comments on commit 46c45fa

Please sign in to comment.