Skip to content

Commit

Permalink
Merge branch 'date_for_entries'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbulat committed Jul 24, 2015
2 parents a0b987c + d04f47d commit 68aca8a
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ Next we'll build the entry we want to record. Plutus uses ActiveRecord conventio
```ruby
entry = Plutus::Entry.new(
:description => "Order placed for widgets",
:date => Date.yesterday,
:debits => [
{:account_name => "Cash", :amount => 100.00}],
:credits => [
{:account_name => "Unearned Revenue", :amount => 100.00}])
```

Entries must specify a description, as well as at least one credit and debit amount. `Amount`s must specify an amount value as well as an account, either by providing a `Plutus::Account` to `account` or by passing in an `account_name` string.
Entries must specify a description, as well as at least one credit and debit amount. Specifying the date is optional; by default, the current date will be assigned to the entry before the record is saved. `Amount`s must specify an amount value as well as an account, either by providing a `Plutus::Account` to `account` or by passing in an `account_name` string.

Finally, save the entry.

Expand Down
5 changes: 5 additions & 0 deletions app/models/plutus/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Plutus
#
# @author Michael Bulat
class Entry < ActiveRecord::Base
before_save :default_date
belongs_to :commercial_document, :polymorphic => true
has_many :credit_amounts, :extend => AmountsExtension, :class_name => 'Plutus::CreditAmount', :inverse_of => :entry
has_many :debit_amounts, :extend => AmountsExtension, :class_name => 'Plutus::DebitAmount', :inverse_of => :entry
Expand Down Expand Up @@ -50,6 +51,10 @@ def initialize(*args)
end

private
def default_date
self.date ||= Date.today
end

def has_credit_amounts?
errors[:base] << "Entry must have at least one credit amount" if self.credit_amounts.blank?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.up

create_table :plutus_entries do |t|
t.string :description
t.date :date
t.integer :commercial_document_id
t.string :commercial_document_type

Expand All @@ -23,7 +24,7 @@ def self.up
t.references :account
t.references :entry
t.decimal :amount, :precision => 20, :scale => 10
end
end
add_index :plutus_amounts, :type
add_index :plutus_amounts, [:account_id, :entry_id]
add_index :plutus_amounts, [:entry_id, :account_id]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TenantPlutusTables < ActiveRecord::Migration
def change
# add a tenant column to plutus accounts table.
add_column :plutus_accounts, :tenant_id, :string, index: true
add_column :plutus_accounts, :tenant_id, :integer, index: true
end
end
3 changes: 2 additions & 1 deletion fixture_rails_root/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20141027103120) do
ActiveRecord::Schema.define(:version => 20150722204422) do

create_table "plutus_accounts", :force => true do |t|
t.string "name"
Expand All @@ -37,6 +37,7 @@

create_table "plutus_entries", :force => true do |t|
t.string "description"
t.date "date"
t.integer "commercial_document_id"
t.string "commercial_document_type"
t.datetime "created_at"
Expand Down
1 change: 1 addition & 0 deletions lib/generators/plutus/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.up

create_table :plutus_entries do |t|
t.string :description
t.date :date
t.integer :commercial_document_id
t.string :commercial_document_type

Expand Down
9 changes: 9 additions & 0 deletions spec/models/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ module Plutus
end
end

context "without a date" do
let(:entry) { FactoryGirl.build(:entry_with_credit_and_debit, date: nil) }

context "should assign a default date before being saved" do
before { entry.save! }
its(:date) { should == Date.today }
end
end

it "should require the debit and credit amounts to cancel" do
entry.credit_amounts << FactoryGirl.build(:credit_amount, :amount => 100, :entry => entry)
entry.debit_amounts << FactoryGirl.build(:debit_amount, :amount => 200, :entry => entry)
Expand Down
31 changes: 0 additions & 31 deletions spec/schema.rb

This file was deleted.

0 comments on commit 68aca8a

Please sign in to comment.