acts_as_priceable 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActsAsPriceable
2
2
 
3
- TODO: Write a gem description
3
+ This gem is extension for ActiveRecord which provides mechanism for handling prices.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,9 +16,97 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install acts_as_priceable
18
18
 
19
+ ## Migrations
20
+
21
+ You can add all columns which are needed by this gem using provided migrations
22
+ ```ruby
23
+
24
+ def change
25
+ create_table :products do |t|
26
+ t.price :price
27
+ # This creates 3 column
28
+ # t.integer :price_gross
29
+ # t.integer :price_net
30
+ # t.integer :price_tax
31
+ end
32
+ end
33
+
34
+ ```
35
+
36
+ ```ruby
37
+
38
+ def change
39
+ add_price :products, :price
40
+ end
41
+
42
+ ```
43
+
44
+ ```ruby
45
+
46
+ def change
47
+ remove_price :products, :price
48
+ end
49
+
50
+ ```
51
+
19
52
  ## Usage
20
53
 
21
- TODO: Write usage instructions here
54
+ Basics
55
+ ------
56
+
57
+ ```ruby
58
+ class Product < ActiveRecord::Base
59
+ has_price :price
60
+ end
61
+ ```
62
+
63
+ This provides accessors which wraps created database columns. `price_gross` and `price_net` converts integer columns into BigDecimal with some scale (default scale is 2).
64
+ Ex. if `price_gross` in database is `1000`, accessor will return `10.00` as BigDecimal.
65
+
66
+ `price_tax` is percentage VAT tax added to net price.
67
+ `price_tax_value` is value of tax.
68
+
69
+ Instead of setting `price_net` and `price_gross` separately you can use `price` and `price_mode` accessors.
70
+
71
+ ```ruby
72
+ p = Product.new
73
+ p.price_tax = 20
74
+ p.price = '10.00'
75
+ p.price_mode = 'net'
76
+ p.save
77
+ p.price_net # 10.00
78
+ p.price_gross # 12.00
79
+ ```
80
+
81
+ Recalculating price takes place in before_validation. You can recalculate it manually by calling `update_price` method.
82
+
83
+ NOTE: `price_net`, `price_gross`, etc. are made from `has_price` method first argument. You can have different name for price accessor or even two or more prices
84
+
85
+ ```ruby
86
+ class Product < ActiveRecord::Base
87
+ has_price :price
88
+ has_price :promotion
89
+ end
90
+ ```
91
+
92
+ This defines `price_net` etc. as well as `promotion_net` ...
93
+
94
+ Validations
95
+ -----------
96
+
97
+ On default `price_tax`, `price_net` and `price_gross` can't be lower than 0. You can turn off validation by setting `without_validation` parameter to true in options.
98
+
99
+ Configuration
100
+ -------------
101
+ ```ruby
102
+ class Product < ActiveRecord::Base
103
+ has_price :price, scale: 4,
104
+ without_validation: true
105
+ end
106
+ ```
107
+
108
+
109
+
22
110
 
23
111
  ## Contributing
24
112
 
@@ -24,6 +24,8 @@ module ActsAsPriceable
24
24
  net = "#{name}_net"
25
25
  tax_value = "#{name}_tax_value"
26
26
  tax = "#{name}_tax"
27
+ mode = "#{name}_mode"
28
+
27
29
  send :define_method, gross do
28
30
  BigDecimal.new(self[gross.to_sym].to_i) / BigDecimal.new(10**scale.to_i)
29
31
  end
@@ -44,11 +46,11 @@ module ActsAsPriceable
44
46
  send(gross) - send(net)
45
47
  end
46
48
 
47
- send :attr_accessor, :mode
49
+ send :attr_accessor, mode.to_sym
48
50
  send :attr_accessor, name
49
51
 
50
52
  send :define_method, "update_#{name}" do
51
- if self.mode == 'net'
53
+ if self.send(mode).to_s == 'net'
52
54
  self.send "#{net}=", self.send(name)
53
55
  self.send "#{gross}=", self.send(net) * ((BigDecimal.new(self.send(tax)) / 100) + 1)
54
56
  else
@@ -58,7 +60,7 @@ module ActsAsPriceable
58
60
  end
59
61
 
60
62
  send :before_validation do
61
- if self.send(name) and self.mode
63
+ if self.send(name) && self.send(mode)
62
64
  self.send "update_#{name}"
63
65
  end
64
66
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsPriceable
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -28,8 +28,8 @@ describe Dummy do
28
28
  end
29
29
 
30
30
  it 'should have mode attr_accessor' do
31
- @dummy.should respond_to(:mode)
32
- @dummy.should respond_to(:mode=)
31
+ @dummy.should respond_to(:price_mode)
32
+ @dummy.should respond_to(:price_mode=)
33
33
  end
34
34
 
35
35
  it 'should have price_tax_value method' do
@@ -63,7 +63,7 @@ describe Dummy do
63
63
  it 'calculates price gross from price_net' do
64
64
  @dummy.price = '10.00'
65
65
  @dummy.price_tax = 23
66
- @dummy.mode = 'net'
66
+ @dummy.price_mode = 'net'
67
67
  @dummy.update_price
68
68
  @dummy.price_net.should == BigDecimal.new('10.00')
69
69
  @dummy.price_gross.should == BigDecimal.new('12.30')
@@ -72,7 +72,7 @@ describe Dummy do
72
72
  it 'calculates price net from price gross' do
73
73
  @dummy.price = '12.30'
74
74
  @dummy.price_tax = 23
75
- @dummy.mode = 'gross'
75
+ @dummy.price_mode = 'gross'
76
76
  @dummy.update_price
77
77
  @dummy.price_net.should == BigDecimal.new('10.00')
78
78
  @dummy.price_gross.should == BigDecimal.new('12.30')
@@ -81,7 +81,7 @@ describe Dummy do
81
81
  it 'calculates tax value properly' do
82
82
  @dummy.price = '10.00'
83
83
  @dummy.price_tax = 23
84
- @dummy.mode = 'net'
84
+ @dummy.price_mode = 'net'
85
85
  @dummy.update_price
86
86
  @dummy.price_tax_value.should == BigDecimal.new('2.30')
87
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_priceable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
12
+ date: 2013-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: -2619945945868084859
114
+ hash: 415468810773993444
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
@@ -120,10 +120,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: -2619945945868084859
123
+ hash: 415468810773993444
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 1.8.24
126
+ rubygems_version: 1.8.25
127
127
  signing_key:
128
128
  specification_version: 3
129
129
  summary: Acts as priceable