diff --git a/README.markdown b/README.markdown index 62916d4a..92dc99a0 100644 --- a/README.markdown +++ b/README.markdown @@ -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. diff --git a/app/models/plutus/entry.rb b/app/models/plutus/entry.rb index f362d3a6..17bab2e6 100644 --- a/app/models/plutus/entry.rb +++ b/app/models/plutus/entry.rb @@ -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 @@ -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 diff --git a/fixture_rails_root/db/migrate/20140224192409_create_plutus_tables.rb b/fixture_rails_root/db/migrate/20150722204256_create_plutus_tables.rb similarity index 97% rename from fixture_rails_root/db/migrate/20140224192409_create_plutus_tables.rb rename to fixture_rails_root/db/migrate/20150722204256_create_plutus_tables.rb index 00a5cd45..e79048e5 100644 --- a/fixture_rails_root/db/migrate/20140224192409_create_plutus_tables.rb +++ b/fixture_rails_root/db/migrate/20150722204256_create_plutus_tables.rb @@ -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 @@ -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] diff --git a/fixture_rails_root/db/migrate/20141027103120_tenant_plutus_tables.rb b/fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb similarity index 65% rename from fixture_rails_root/db/migrate/20141027103120_tenant_plutus_tables.rb rename to fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb index 78e8e325..9790d59e 100644 --- a/fixture_rails_root/db/migrate/20141027103120_tenant_plutus_tables.rb +++ b/fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb @@ -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 diff --git a/fixture_rails_root/db/schema.rb b/fixture_rails_root/db/schema.rb index 36f1724e..9c96f1a8 100644 --- a/fixture_rails_root/db/schema.rb +++ b/fixture_rails_root/db/schema.rb @@ -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" @@ -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" diff --git a/lib/generators/plutus/templates/migration.rb b/lib/generators/plutus/templates/migration.rb index a5a6fca9..e79048e5 100644 --- a/lib/generators/plutus/templates/migration.rb +++ b/lib/generators/plutus/templates/migration.rb @@ -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 diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index 38f9ae4d..f6a4a934 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -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) diff --git a/spec/schema.rb b/spec/schema.rb deleted file mode 100644 index c11b72b0..00000000 --- a/spec/schema.rb +++ /dev/null @@ -1,31 +0,0 @@ -# This file is auto-generated from the current state of the database. Instead of editing this file, -# please use the migrations feature of Active Record to incrementally modify your database, and -# then regenerate this schema definition. -# -# Note that this schema.rb definition is the authoritative source for your database schema. If you need -# to create the application database on another system, you should be using db:schema:load, not running -# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). -# -# It's strongly recommended to check this file into your version control system. - -ActiveRecord::Schema.define(:version => 20100419190249) do - - create_table "accounts", :force => true do |t| - t.string "name" - t.string "type" - t.datetime "created_at" - t.datetime "updated_at" - end - - create_table "transactions", :force => true do |t| - t.string "description" - t.integer "credit_account_id" - t.decimal "credit_amount" - t.integer "debit_account_id" - t.decimal "debit_amount" - t.datetime "created_at" - t.datetime "updated_at" - end - -end