deb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +0 -0
- data/Rakefile +29 -0
- data/app/models/deb/account.rb +28 -0
- data/app/models/deb/item.rb +36 -0
- data/app/models/deb/transaction.rb +32 -0
- data/lib/deb/builder.rb +41 -0
- data/lib/deb/engine.rb +8 -0
- data/lib/deb/version.rb +3 -0
- data/lib/deb.rb +5 -0
- data/lib/generators/deb/deb_generator.rb +16 -0
- data/lib/generators/deb/templates/migration.rb +40 -0
- data/lib/tasks/deb_tasks.rake +4 -0
- metadata +161 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Deb'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Deb
|
2
|
+
class Account < ActiveRecord::Base
|
3
|
+
belongs_to :reference, polymorphic: true
|
4
|
+
has_many :items
|
5
|
+
has_many :transactions, through: :items
|
6
|
+
|
7
|
+
validates :kind, inclusion: {in: %w(asset liability equity revenue expense)}
|
8
|
+
|
9
|
+
attr_accessible :name, :kind, :short_name, :contra
|
10
|
+
|
11
|
+
def self.[](some_shot_name)
|
12
|
+
where(short_name: some_shot_name).first
|
13
|
+
end
|
14
|
+
|
15
|
+
ACCOUNTS_POSITIONS = {
|
16
|
+
"debit" => ["asset", "expense"],
|
17
|
+
"credit" => ["liability", "equity", "revenue"]
|
18
|
+
}
|
19
|
+
|
20
|
+
def can_be_in?(position)
|
21
|
+
ACCOUNTS_POSITIONS[contra? ? self.class.swap_position(position) : position].member?(kind)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.swap_position(value)
|
25
|
+
"debit" == value ? "credit" : "debit"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Deb
|
2
|
+
class Item < ActiveRecord::Base
|
3
|
+
belongs_to :account
|
4
|
+
belongs_to :transaction
|
5
|
+
validates :kind, inclusion: {in: %w(debit credit)}
|
6
|
+
validates :account_id, presence: true
|
7
|
+
validate :account_kind
|
8
|
+
validate :positive_amount
|
9
|
+
|
10
|
+
after_create :update_balances
|
11
|
+
|
12
|
+
attr_accessible :account, :amount
|
13
|
+
|
14
|
+
def account_kind
|
15
|
+
errors.add(:account_id, "account cannot be in #{kind}") if !account || !account.can_be_in?(kind)
|
16
|
+
end
|
17
|
+
|
18
|
+
def positive_amount
|
19
|
+
errors.add(:amount, "should be positive") unless amount > 0
|
20
|
+
end
|
21
|
+
|
22
|
+
def update_balances
|
23
|
+
return if @balances_updated
|
24
|
+
@balances_updated = true
|
25
|
+
self.balance_before = account.current_balance
|
26
|
+
self.balance_after = account.current_balance + op_sign * amount
|
27
|
+
save!
|
28
|
+
account.current_balance = balance_after
|
29
|
+
account.save!
|
30
|
+
end
|
31
|
+
|
32
|
+
def op_sign
|
33
|
+
"debit" == kind ? -1 : 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Deb
|
2
|
+
class Transaction < ActiveRecord::Base
|
3
|
+
belongs_to :reference, polymorphic: true
|
4
|
+
has_many :items
|
5
|
+
has_many :accounts, through: :items
|
6
|
+
has_many :debit_items, class_name: "Deb::Item", conditions: {kind: "debit"}
|
7
|
+
has_many :debit_accounts, through: :debit_items
|
8
|
+
has_many :credit_items, class_name: "Deb::Item", conditions: {kind: "credit"}
|
9
|
+
has_many :credit_accounts, through: :credit_items
|
10
|
+
|
11
|
+
validate :proper_amounts
|
12
|
+
|
13
|
+
attr_accessible :reference, :description
|
14
|
+
|
15
|
+
def self.start(&block)
|
16
|
+
Docile.dsl_eval(Deb::Builder.new, &block).build
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.start!(&block)
|
20
|
+
transaction do
|
21
|
+
start(&block).save!
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def proper_amounts
|
26
|
+
errors.add(:base, "no debit items") if debit_items.blank?
|
27
|
+
errors.add(:base, "no credit items") if credit_items.blank?
|
28
|
+
errors.add(:base, "wrong credit total is not equal debit total") unless credit_items.collect(&:amount).reduce(:+) == debit_items.collect(&:amount).reduce(:+)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/deb/builder.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Deb
|
2
|
+
class Builder
|
3
|
+
attr_reader :debits, :credits, :description, :reference
|
4
|
+
|
5
|
+
def debit(account, amount)
|
6
|
+
@debits ||= {}
|
7
|
+
@debits[account] = amount
|
8
|
+
end
|
9
|
+
|
10
|
+
def credit(account, amount)
|
11
|
+
@credits ||= {}
|
12
|
+
@credits[account] = amount
|
13
|
+
end
|
14
|
+
|
15
|
+
def description(desc)
|
16
|
+
@description = desc
|
17
|
+
end
|
18
|
+
|
19
|
+
def reference(ref)
|
20
|
+
@reference = ref
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
@debits ||= {}
|
25
|
+
@credits ||= {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def build
|
29
|
+
Deb::Transaction.new(description: @description, reference: @reference) do |t|
|
30
|
+
t.reference = @reference
|
31
|
+
credits.each do |account, amount|
|
32
|
+
t.credit_items.build(account: account, amount: amount)
|
33
|
+
end
|
34
|
+
debits.each do |account, amount|
|
35
|
+
t.debit_items.build(account: account, amount: amount)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
data/lib/deb/engine.rb
ADDED
data/lib/deb/version.rb
ADDED
data/lib/deb.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class DebGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def self.next_migration_number(path)
|
9
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_migration_file
|
13
|
+
migration_template 'migration.rb', 'db/migrate/create_deb_tables.rb'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class CreateDebTables < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :deb_accounts do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :kind
|
6
|
+
t.string :short_name, limit: 16
|
7
|
+
t.references :reference, polymorphic: true
|
8
|
+
t.boolean :contra
|
9
|
+
t.decimal :current_balance, precision: 20, scale: 2, default: 0
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
add_index :deb_accounts, :short_name
|
13
|
+
add_index :deb_accounts, [:reference_type, :reference_id, :kind], name: "deb_accounts_default"
|
14
|
+
|
15
|
+
create_table :deb_transactions do |t|
|
16
|
+
t.string :description
|
17
|
+
t.references :reference, polymorphic: true
|
18
|
+
t.timestamps
|
19
|
+
end
|
20
|
+
add_index :deb_transactions, [:reference_type, :reference_id], name: "deb_transactions_default"
|
21
|
+
|
22
|
+
create_table :deb_items do |t|
|
23
|
+
t.string :kind
|
24
|
+
t.integer :account_id
|
25
|
+
t.integer :transaction_id
|
26
|
+
t.decimal :amount, precision: 20, scale: 2, default: 0
|
27
|
+
t.decimal :balance_before, precision: 20, scale: 2, default: 0
|
28
|
+
t.decimal :balance_after, precision: 20, scale: 2, default: 0
|
29
|
+
end
|
30
|
+
add_index :deb_items, :account_id
|
31
|
+
add_index :deb_items, [:transaction_id, :kind]
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.down
|
35
|
+
drop_table :deb_accounts
|
36
|
+
drop_table :deb_transactions
|
37
|
+
drop_table :deb_items
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Boris Nadion
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: docile
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.13'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.13'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec-rails
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2.13'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '2.13'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec-rails
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '2.13'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '2.13'
|
110
|
+
description:
|
111
|
+
email:
|
112
|
+
- boris@astrails.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- app/models/deb/account.rb
|
118
|
+
- app/models/deb/item.rb
|
119
|
+
- app/models/deb/transaction.rb
|
120
|
+
- lib/deb/builder.rb
|
121
|
+
- lib/deb/engine.rb
|
122
|
+
- lib/deb/version.rb
|
123
|
+
- lib/deb.rb
|
124
|
+
- lib/generators/deb/deb_generator.rb
|
125
|
+
- lib/generators/deb/templates/migration.rb
|
126
|
+
- lib/tasks/deb_tasks.rake
|
127
|
+
- MIT-LICENSE
|
128
|
+
- Rakefile
|
129
|
+
- README.markdown
|
130
|
+
homepage: https://github.com/astrails/deb
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
hash: -3835187591091313650
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ! '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
segments:
|
153
|
+
- 0
|
154
|
+
hash: -3835187591091313650
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 1.8.25
|
158
|
+
signing_key:
|
159
|
+
specification_version: 3
|
160
|
+
summary: Double Entry Bookkeeping for Rails
|
161
|
+
test_files: []
|