amountable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/amountable.rb +10 -3
- data/lib/amountable/version.rb +1 -1
- data/spec/amountable/amountable_spec.rb +29 -16
- data/spec/support/database.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f8f2e9a3b13fdf8694c20e935180962c1731864
|
4
|
+
data.tar.gz: d11442900e2e45e11992198e15ccbc38d3a9a911
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b51ce8c0058625f4792b603986fdcf44b1bd359b5a438332c7a190c185c9f0dbb8fe5492f1daa787500636d42295b9b22c786dbe51a7be8e5a16693cac007dd
|
7
|
+
data.tar.gz: ff6bf8d43f992ecde40508a84bd61e3f3cdc755ab1b735a8fb868f3d8a27d855d35cf7d8b9b8c69a8d60b173a3233b15b0218d741e8e37979e1efdca7569976e
|
data/Gemfile.lock
CHANGED
data/lib/amountable.rb
CHANGED
@@ -102,11 +102,18 @@ module Amountable
|
|
102
102
|
end
|
103
103
|
|
104
104
|
define_method "#{name}=" do |value|
|
105
|
-
return Money.zero if value.zero?
|
106
105
|
amount = find_amount(name) || amounts.build(name: name)
|
107
106
|
amount.value = value.to_money
|
108
|
-
|
109
|
-
|
107
|
+
if value.zero?
|
108
|
+
amounts.delete(amount)
|
109
|
+
all_amounts.delete(amount)
|
110
|
+
@amounts_by_name.delete(name)
|
111
|
+
amount.destroy
|
112
|
+
else
|
113
|
+
all_amounts << amount if amount.new_record?
|
114
|
+
(@amounts_by_name ||= {})[name.to_sym] = amount
|
115
|
+
end
|
116
|
+
value.to_money
|
110
117
|
end
|
111
118
|
|
112
119
|
Array(options[:summable] || options[:summables] || options[:set] || options[:sets] || options[:amount_set] || options[:amount_sets]).each do |set|
|
data/lib/amountable/version.rb
CHANGED
@@ -6,11 +6,7 @@ describe Amountable do
|
|
6
6
|
|
7
7
|
it 'should' do
|
8
8
|
order = Order.new
|
9
|
-
expect {
|
10
|
-
order.save
|
11
|
-
}.not_to change {
|
12
|
-
Amount.count
|
13
|
-
}
|
9
|
+
expect { order.save }.not_to change { Amount.count }
|
14
10
|
%i(sub_total taxes total).each do |name|
|
15
11
|
expect(order.send(name)).to eq(Money.zero)
|
16
12
|
end
|
@@ -22,24 +18,41 @@ describe Amountable do
|
|
22
18
|
expect(amount.name).to eq('sub_total')
|
23
19
|
expect(amount.value).to eq(Money.new(100))
|
24
20
|
expect(amount.new_record?).to be true
|
25
|
-
expect {
|
26
|
-
order.save
|
27
|
-
}.to change {
|
28
|
-
Amount.count
|
29
|
-
}.by(1)
|
21
|
+
expect { order.save }.to change { Amount.count }.by(1)
|
30
22
|
expect(amount.persisted?).to be true
|
31
23
|
end
|
32
|
-
expect
|
24
|
+
expect do
|
33
25
|
expect(order.update_attributes(sub_total: Money.new(200)))
|
34
|
-
|
35
|
-
|
36
|
-
|
26
|
+
end.not_to change { Amount.count }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'name=' do
|
30
|
+
let (:order) { Order.create }
|
31
|
+
|
32
|
+
it 'should not persist Money.zero' do
|
33
|
+
expect(order.sub_total = Money.zero).to eq(Money.zero)
|
34
|
+
expect { order.save }.not_to change { Amount.count }
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should not persist Money.zero if using ActiveRecord persistence' do
|
38
|
+
expect { order.update(sub_total: Money.zero) }.not_to change { Amount.count }
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should work with ActiveRecord#update' do
|
42
|
+
expect { order.update(sub_total: Money.new(1)) }.to change { Amount.count }.by(1)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should destroy Amount if exist and assigning Money.zero' do
|
46
|
+
order.update(sub_total: Money.new(1))
|
47
|
+
expect { order.sub_total = Money.zero }.to change { Amount.count }.by(-1)
|
48
|
+
expect(order.amounts.empty?).to be true
|
49
|
+
end
|
37
50
|
end
|
38
51
|
|
39
52
|
it 'should insert amounts in bulk' do
|
40
53
|
order = Order.create
|
41
|
-
expect
|
54
|
+
expect do
|
42
55
|
order.update(sub_total: Money.new(100), taxes: Money.new(200))
|
43
|
-
|
56
|
+
end.to make_database_queries(count: 1, manipulative: true)
|
44
57
|
end
|
45
58
|
end
|
data/spec/support/database.rb
CHANGED
@@ -28,7 +28,7 @@ if File.exist?(database_yml)
|
|
28
28
|
ActiveRecord::Base.establish_connection(config)
|
29
29
|
end
|
30
30
|
|
31
|
-
load(File.dirname(__FILE__) + '/../internal/db/schema.rb')
|
31
|
+
#load(File.dirname(__FILE__) + '/../internal/db/schema.rb')
|
32
32
|
load(File.dirname(__FILE__) + '/../internal/app/models/order.rb')
|
33
33
|
|
34
34
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amountable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emmanuel Turlay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|