amountable 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eaab8de5d539d970c93a407222cddfbdcb0b3cef
4
- data.tar.gz: ef1f96155420b3adddbfab6ce97fbaffd7bafe8f
3
+ metadata.gz: 1f8f2e9a3b13fdf8694c20e935180962c1731864
4
+ data.tar.gz: d11442900e2e45e11992198e15ccbc38d3a9a911
5
5
  SHA512:
6
- metadata.gz: 67aa3b1d6d456f2c3c3e2005ef6a009be518be79492cc2edbdfae92a2b011661e8ae51d7cd959d5794d5044e9ec80ad8132245acceb1832ce910f5607ff280d3
7
- data.tar.gz: 38ffecf5763a50f826ca60c4304cdf721e91814a6bdb08aacddf5fdb2da21ddffb965243c07cd396d16a2d47c862d218fd06efecc0789a5b6467c756f5de34a4
6
+ metadata.gz: 2b51ce8c0058625f4792b603986fdcf44b1bd359b5a438332c7a190c185c9f0dbb8fe5492f1daa787500636d42295b9b22c786dbe51a7be8e5a16693cac007dd
7
+ data.tar.gz: ff6bf8d43f992ecde40508a84bd61e3f3cdc755ab1b735a8fb868f3d8a27d855d35cf7d8b9b8c69a8d60b173a3233b15b0218d741e8e37979e1efdca7569976e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- amountable (0.0.1)
4
+ amountable (0.0.2)
5
5
  activerecord-import (= 0.10.0)
6
6
  monetize
7
7
  money-rails
@@ -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
- all_amounts << amount if amount.new_record?
109
- (@amounts_by_name ||= {})[name.to_sym] = amount
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|
@@ -1,5 +1,5 @@
1
1
  # Copyright 2015, Instacart
2
2
 
3
3
  module Amountable
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -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
- }.not_to change {
35
- Amount.count
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
- }.to make_database_queries(count: 1, manipulative: true)
56
+ end.to make_database_queries(count: 1, manipulative: true)
44
57
  end
45
58
  end
@@ -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.2
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: 2015-12-17 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails