death_and_taxes 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,7 @@ require 'active_support/core_ext'
7
7
  require File.join(File.dirname(__FILE__), 'death_and_taxes/railtie')
8
8
 
9
9
  module DeathAndTaxes
10
- autoload :Hook, File.join(File.dirname(__FILE__), "death_and_taxes/hook")
11
- autoload :InstanceMethods, File.join(File.dirname(__FILE__), "death_and_taxes/instance_methods")
12
-
13
- @taxes = Dir[File.join( File.dirname(__FILE__), '..', 'config', '*.yml' )].map do |filename|
14
- DeathAndTaxes::Taxes::Country.new(YAML.load_file(file_name))
10
+ %w( Tax Taxable Taxation Taxer ).each do |class_name|
11
+ autoload class_name.to_sym, File.join(File.dirname(__FILE__), "death_and_taxes/#{class_name.downcase}")
15
12
  end
16
13
  end
@@ -4,7 +4,8 @@ require 'death_and_taxes'
4
4
  module DeathAndTaxes
5
5
  class Railtie < Rails::Railtie
6
6
  config.to_prepare do
7
- ActiveRecord::Base.send(:extend, DeathAndTaxes::Hook)
7
+ ActiveRecord::Base.send(:include, DeathAndTaxes::Taxable)
8
+ ActiveRecord::Base.send(:include, DeathAndTaxes::Taxer)
8
9
  end
9
10
  end
10
11
  end
@@ -0,0 +1,14 @@
1
+ module DeathAndTaxes
2
+ class Tax < ::ActiveRecord::Base
3
+ belongs_to :taxer, :polymorphic => true
4
+ belongs_to :tax, :foreign_key => :apply_on_tax
5
+
6
+ def apply amount
7
+ amount * multiplier
8
+ end
9
+
10
+ def multiplier
11
+ (percentage / 100) * (1 + tax.try(:multiplier).to_f)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ module DeathAndTaxes
2
+ module Taxable
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+
8
+ module ClassMethods
9
+
10
+ ##
11
+ # Makes this model taxable
12
+ # It will then be able to have taxations applied
13
+ # Example:
14
+ # class Invoicing < ActiveRecord::Base
15
+ # acts_as_taxable
16
+ # end
17
+ def acts_as_taxable(*args)
18
+
19
+ class_eval do
20
+ has_many :taxations, :as => :taxable, :class_name => 'DeathAndTaxes::Taxation'
21
+ end
22
+
23
+ include DeathAndTaxes::Taxable::InstanceMethods
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ def apply_taxes(taxes)
29
+ taxes = [taxes] unless taxes.is_a? Array
30
+
31
+ self.taxations = taxes.collect do |tax|
32
+ Taxation.new :amount => tax.apply(amount), :percentage => tax.multiplier, :name => tax.name, :account_number => tax.account_number
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module DeathAndTaxes
2
+ class Taxation < ::ActiveRecord::Base
3
+ belongs_to :taxable, :polymorphic => true
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module DeathAndTaxes
2
+ module Taxer
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ ##
9
+ # Makes this model a taxer
10
+ # It will then be able to create and possess taxes
11
+ # Example:
12
+ # class State < ActiveRecord::Base
13
+ # acts_as_taxer
14
+ # end
15
+ def acts_as_taxer(*args)
16
+ class_eval do
17
+ has_many :taxes, :as => :taxer, :class_name => "DeathAndTaxes::Tax"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module DeathAndTaxes
2
- VERSION = '0.0.0'
3
- end
2
+ VERSION = '0.0.1'
3
+ end
@@ -4,14 +4,29 @@ class DeathAndTaxesMigration < ActiveRecord::Migration
4
4
  t.references :taxable, :polymorphic => true
5
5
  t.integer :amount
6
6
  t.float :percentage
7
- t.string :level
8
7
  t.string :name
8
+ t.string :account_number
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ create_table :taxes do |t|
14
+ t.references :taxer, :polymorphic => true
15
+ t.float :percentage
16
+ t.string :name
17
+ t.string :account_number
18
+ t.integer :apply_on_tax
19
+
20
+ t.timestamps
9
21
  end
10
22
 
11
23
  add_index :taxations, [:taxable_id, :taxable_type]
24
+ add_index :taxes, [:taxer_id, :taxer_type]
25
+ add_index :taxes, :apply_on_tax
12
26
  end
13
27
 
14
28
  def self.down
15
29
  drop_table :taxations
30
+ drop_table :taxes
16
31
  end
17
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: death_and_taxes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-09 00:00:00.000000000Z
12
+ date: 2011-08-09 00:00:00.000000000 -04:00
13
+ default_executable:
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: rails
16
- requirement: &2157922740 !ruby/object:Gem::Requirement
17
+ requirement: &2152141700 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,10 +22,10 @@ dependencies:
21
22
  version: '0'
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *2157922740
25
+ version_requirements: *2152141700
25
26
  - !ruby/object:Gem::Dependency
26
27
  name: sqlite3
27
- requirement: &2157922200 !ruby/object:Gem::Requirement
28
+ requirement: &2152141280 !ruby/object:Gem::Requirement
28
29
  none: false
29
30
  requirements:
30
31
  - - ! '>='
@@ -32,7 +33,7 @@ dependencies:
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *2157922200
36
+ version_requirements: *2152141280
36
37
  description: Death and Taxes allows to 'easily' calculate taxes on a model
37
38
  email: gmalette@gmail.com
38
39
  executables: []
@@ -46,21 +47,19 @@ files:
46
47
  - LICENSE.txt
47
48
  - README.rdoc
48
49
  - Rakefile
49
- - config/ca.yml
50
- - death_and_taxes-0.0.1.gem
51
50
  - death_and_taxes.gemspec
52
51
  - lib/death_and_taxes.rb
53
- - lib/death_and_taxes/hook.rb
54
- - lib/death_and_taxes/instance_methods.rb
55
52
  - lib/death_and_taxes/railtie.rb
56
- - lib/death_and_taxes/taxes/country.rb
57
- - lib/death_and_taxes/taxes/state.rb
58
- - lib/death_and_taxes/taxes/tax.rb
53
+ - lib/death_and_taxes/tax.rb
54
+ - lib/death_and_taxes/taxable.rb
55
+ - lib/death_and_taxes/taxation.rb
56
+ - lib/death_and_taxes/taxer.rb
59
57
  - lib/death_and_taxes/version.rb
60
58
  - lib/generators/death_and_taxes/migration/migration_generator.rb
61
59
  - lib/generators/death_and_taxes/migration/templates/active_record/migration.rb
62
60
  - test/helper.rb
63
61
  - test/test_death_and_taxes.rb
62
+ has_rdoc: true
64
63
  homepage: ''
65
64
  licenses: []
66
65
  post_install_message:
@@ -81,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
80
  version: '0'
82
81
  requirements: []
83
82
  rubyforge_project:
84
- rubygems_version: 1.8.6
83
+ rubygems_version: 1.6.2
85
84
  signing_key:
86
85
  specification_version: 3
87
86
  summary: Tax calculations
data/config/ca.yml DELETED
@@ -1,17 +0,0 @@
1
- taxes:
2
- -
3
- start_date: 2011-01-01
4
- end_date: 2012-01-01
5
- percentage: 5.0
6
- name: gst
7
- states:
8
- qc:
9
- -
10
- start_date: 2011-01-01
11
- end_date: 2012-01-01
12
- percentage: 8.5
13
- name: pst
14
- rule:
15
- country: ['gst']
16
- state:
17
- compound: ['gst', 'pst']
Binary file
@@ -1,7 +0,0 @@
1
- module DeathAndTaxes::Hook
2
- def acts_as_taxable(*args)
3
- options = args.extract_options!
4
-
5
- include DeathAndTaxes::InstanceMethods
6
- end
7
- end
@@ -1,5 +0,0 @@
1
- module DeathAndTaxes::InstanceMethods
2
- def apply_taxes
3
- raise "TAXES"
4
- end
5
- end
@@ -1,38 +0,0 @@
1
- class DeathAndTaxes::Taxes::Country
2
-
3
- def initialize tax_infos
4
- @taxes = tax_infos['taxes'].collect do |tax|
5
- Tax.new tax, :country => self
6
- end
7
-
8
- @states = {}
9
- tax_infos['states'].each do |name, tax|
10
- @states[name] = Tax.new(tax)
11
- end
12
- end
13
-
14
- def apply_taxes product, from, to, options = {}
15
- if from[:country] == to[:country]
16
- if (state = @states[from[:state]] && state.matches?(from, to))
17
- state.apply_taxes product, from, to, options
18
- else
19
- apply_tax product, from, to, options
20
- end
21
- end
22
- []
23
- end
24
-
25
- private
26
- def apply_tax product, from, to, options
27
- options.reverse_merge!({:time => Time.now})
28
-
29
- find_tax product, options[:date]
30
- end
31
-
32
- def find_tax product, date
33
- @taxes.select do |tax|
34
- tax.period.cover?(date)
35
- end
36
- end
37
-
38
- end
@@ -1,5 +0,0 @@
1
- class DeathAndTaxes::Taxes::Country
2
- def initialize tax_infos, options={}
3
-
4
- end
5
- end
@@ -1,10 +0,0 @@
1
- class DeathAndTaxes::Taxes::Tax
2
- def initialize infos, options={}
3
- @start_date = Date.parse infos['start_date']
4
- @end_date = Date.parse infos['end_date']
5
- end
6
-
7
- def period
8
- @start_date..@end_date
9
- end
10
- end