money-rails 1.9.0 → 1.10.0

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.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +2 -0
  4. data/Rakefile +4 -1
  5. data/lib/money-rails/active_record/monetizable.rb +3 -2
  6. data/lib/money-rails/version.rb +1 -1
  7. data/money-rails.gemspec +2 -2
  8. data/spec/active_record/monetizable_spec.rb +37 -1
  9. data/spec/dummy/db/migrate/20120331190108_create_products.rb +1 -1
  10. data/spec/dummy/db/migrate/20120402080348_add_bonus_cents_to_product.rb +1 -1
  11. data/spec/dummy/db/migrate/20120524052716_create_services.rb +1 -1
  12. data/spec/dummy/db/migrate/20120528181002_create_transactions.rb +1 -1
  13. data/spec/dummy/db/migrate/20120528210103_create_dummy_products.rb +1 -1
  14. data/spec/dummy/db/migrate/20120607210247_add_column_that_allows_nil.rb +1 -1
  15. data/spec/dummy/db/migrate/20120712202655_add_sale_price_cents_to_product.rb +1 -1
  16. data/spec/dummy/db/migrate/20130124023419_add_price_in_a_range_cents_to_products.rb +1 -1
  17. data/spec/dummy/db/migrate/20140110194016_add_validates_method_amount_cents_to_products.rb +1 -1
  18. data/spec/dummy/db/migrate/20141005075025_add_aliased_attr_to_products.rb +1 -1
  19. data/spec/dummy/db/migrate/20150107061030_add_delivery_fee_cents_and_restock_fee_cents_to_product.rb +1 -1
  20. data/spec/dummy/db/migrate/20150126231442_add_reduced_price_to_products.rb +1 -1
  21. data/spec/dummy/db/migrate/20150213234410_add_special_price_to_products.rb +1 -1
  22. data/spec/dummy/db/migrate/20150217222612_add_lambda_price_to_products.rb +1 -1
  23. data/spec/dummy/db/migrate/20150303222230_add_skip_validation_price_cents_to_products.rb +1 -1
  24. data/spec/dummy/db/migrate/20151026220420_add_optional_amount_to_transactions.rb +1 -1
  25. data/spec/mongoid/four_spec.rb +6 -5
  26. data/spec/mongoid/three_spec.rb +5 -5
  27. metadata +8 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efddf2fd0ce930972e24427f75fa976a18f915a6
4
- data.tar.gz: a3ee629c92a46b729b6beb2eaeae7f6beb8166da
3
+ metadata.gz: 43aea3acfffc9b52e9d127a1c40ff582b68fbe71
4
+ data.tar.gz: a7f43a08d717c6f786e94bbf33c6952001816b97
5
5
  SHA512:
6
- metadata.gz: 61d164c6d684028064e48d7a8b179c09eec0d31b968e2f8471dee8d013b84dae0cc8929cf14a2ba69d04daaaedbc31002c8345c75bb2de0981f3de16b7ba4ad5
7
- data.tar.gz: dbadb3bde17a75b27ceaac420fd6c41859150c34dc1730410c43fcf38789f4cacb3099d34b1d8fe573d67916b776da3aacfd08569039fbd8502d528502ce8ebd
6
+ metadata.gz: 2a104c9d3cb7ba768d8c627e4b34d77d5f31b35bb3a8f414b5a1f2d1548447fae817e2784f41e83acc0ff4a41cb56d7dc7c4b5b0b0f1b95a88ef936e4a735387
7
+ data.tar.gz: 5e6fe0700129072ff2b0f44276223b3540c90eb5d015f6b30b345f3397b7ae44e39cd9e8877075c3047ea63a9e24093589104d0fe874f56f4bf3849b14b0562c
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.10.0
4
+
5
+ - Bump money version to ~> 6.10.0
6
+ - Optimize reading of the attribute when `allow_nil` is set to true
7
+
3
8
  ## 1.9.0
4
9
 
5
10
  - Allow the use of money-rails with plan ActiveRecord (without Rails)
data/README.md CHANGED
@@ -261,6 +261,8 @@ MoneyRails.configure do |config|
261
261
 
262
262
  end
263
263
  ```
264
+ For complete list of available currency: [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217)
265
+
264
266
 
265
267
  If you need to set the default currency on a per-request basis, such as in a
266
268
  multi-tenant application, you may use a lambda to lazy-load the default currency
data/Rakefile CHANGED
@@ -60,6 +60,9 @@ namespace :spec do
60
60
  desc "Run Tests against mongoid (version 2)"
61
61
  task(:mongoid2) { run_with_gemfile 'gemfiles/mongoid2.gemfile' }
62
62
 
63
+ desc "Run Tests against rails 5.1"
64
+ task(:rails51) { run_with_gemfile 'gemfiles/rails51.gemfile' }
65
+
63
66
  desc "Run Tests against rails 5.0"
64
67
  task(:rails50) { run_with_gemfile 'gemfiles/rails50.gemfile' }
65
68
 
@@ -79,7 +82,7 @@ namespace :spec do
79
82
  task :mongoid => [:mongoid2, :mongoid3, :mongoid4, :mongoid5]
80
83
 
81
84
  desc "Run Tests against rails 3 & 4 & 4.1 & 4.2 & 5.0"
82
- task :rails => [:rails3, :rails4, :rails41, :rails42, :rails50]
85
+ task :rails => [:rails3, :rails4, :rails41, :rails42, :rails50, :rails51]
83
86
 
84
87
  desc "Run Tests against all ORMs"
85
88
  task :all => [:rails, :mongoid]
@@ -119,7 +119,7 @@ module MoneyRails
119
119
 
120
120
  # Getter for monetized attribute
121
121
  define_method name do |*args|
122
- read_monetized name, subunit_name, *args
122
+ read_monetized name, subunit_name, options, *args
123
123
  end
124
124
 
125
125
  # Setter for monetized attribute
@@ -178,10 +178,11 @@ module MoneyRails
178
178
  end
179
179
  end
180
180
 
181
- def read_monetized(name, subunit_name, *args)
181
+ def read_monetized(name, subunit_name, options = {}, *args)
182
182
  # Get the cents
183
183
  amount = public_send(subunit_name, *args)
184
184
 
185
+ return if amount.nil? && options[:allow_nil]
185
186
  # Get the currency object
186
187
  attr_currency = public_send("currency_for_#{name}")
187
188
 
@@ -1,3 +1,3 @@
1
1
  module MoneyRails
2
- VERSION = '1.9.0'
2
+ VERSION = '1.10.0'
3
3
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
 
27
27
  s.require_path = "lib"
28
28
 
29
- s.add_dependency "money", "~> 6.9.0"
29
+ s.add_dependency "money", "~> 6.10.0"
30
30
  s.add_dependency "monetize", "~> 1.7.0"
31
31
  s.add_dependency "activesupport", ">= 3.0"
32
32
  s.add_dependency "railties", ">= 3.0"
@@ -34,6 +34,6 @@ Gem::Specification.new do |s|
34
34
 
35
35
  s.add_development_dependency "rails", ">= 3.0"
36
36
  s.add_development_dependency "rspec-rails", "~> 3.0"
37
- s.add_development_dependency 'database_cleaner', ['>= 0.8.0', '< 1.4.0']
37
+ s.add_development_dependency 'database_cleaner', '~> 1.6.1'
38
38
  s.add_development_dependency 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
39
39
  end
@@ -78,13 +78,25 @@ if defined? ActiveRecord
78
78
  end
79
79
 
80
80
  it "assigns the correct value from params" do
81
- params_clp = { amount: '20000', tax: '1000', currency: 'CLP' }
81
+ params_clp = { amount: '20000', tax: '1000', currency: 'CLP' }
82
82
  product = Transaction.create(params_clp)
83
83
  expect(product.valid?).to be_truthy
84
84
  expect(product.amount.currency.subunit_to_unit).to eq(1)
85
85
  expect(product.amount_cents).to eq(20000)
86
86
  end
87
87
 
88
+ # TODO: This is a slightly controversial example, btu it reflects the current behaviour
89
+ it "re-assigns cents amount when subunit/unit ratio changes preserving amount in units" do
90
+ transaction = Transaction.create(amount: '20000', tax: '1000', currency: 'USD')
91
+
92
+ expect(transaction.amount).to eq(Money.new(20000_00, 'USD'))
93
+
94
+ transaction.currency = 'CLP'
95
+
96
+ expect(transaction.amount).to eq(Money.new(20000, 'CLP'))
97
+ expect(transaction.amount_cents).to eq(20000)
98
+ end
99
+
88
100
  it "raises an error if trying to create two attributes with the same name" do
89
101
  expect do
90
102
  class Product
@@ -842,6 +854,30 @@ if defined? ActiveRecord
842
854
  expect(product.read_monetized(:price, :price_cents).to_s).to eq('14,0')
843
855
  end
844
856
  end
857
+
858
+ context "with a monetized attribute that is nil" do
859
+ let(:service) do
860
+ Service.create(discount_cents: nil)
861
+ end
862
+ let(:default_currency_lambda) { double("Default Currency Fallback") }
863
+ subject { service.read_monetized(:discount, :discount_cents, options) }
864
+
865
+ around(:each) do |example|
866
+ service # Instantiate instance which relies on Money.default_currency
867
+ original_default_currency = Money.default_currency
868
+ Money.default_currency = -> { default_currency_lambda.read_currency }
869
+ example.run
870
+ Money.default_currency = original_default_currency
871
+ end
872
+
873
+ context "when allow_nil options is set" do
874
+ let(:options) { { allow_nil: true } }
875
+ it "does not attempt to read the fallback default currency" do
876
+ expect(default_currency_lambda).not_to receive(:read_currency)
877
+ subject
878
+ end
879
+ end
880
+ end
845
881
  end
846
882
 
847
883
  describe "#write_monetized" do
@@ -1,4 +1,4 @@
1
- class CreateProducts < ActiveRecord::Migration
1
+ class CreateProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  create_table :products do |t|
4
4
  t.integer :price_cents
@@ -1,4 +1,4 @@
1
- class AddBonusCentsToProduct < ActiveRecord::Migration
1
+ class AddBonusCentsToProduct < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :bonus_cents, :integer
4
4
 
@@ -1,4 +1,4 @@
1
- class CreateServices < ActiveRecord::Migration
1
+ class CreateServices < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  create_table :services do |t|
4
4
  t.integer :charge_cents
@@ -1,4 +1,4 @@
1
- class CreateTransactions < ActiveRecord::Migration
1
+ class CreateTransactions < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  create_table :transactions do |t|
4
4
  t.integer :amount_cents
@@ -1,4 +1,4 @@
1
- class CreateDummyProducts < ActiveRecord::Migration
1
+ class CreateDummyProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  create_table :dummy_products do |t|
4
4
  t.string :currency
@@ -1,4 +1,4 @@
1
- class AddColumnThatAllowsNil < ActiveRecord::Migration
1
+ class AddColumnThatAllowsNil < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :optional_price_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddSalePriceCentsToProduct < ActiveRecord::Migration
1
+ class AddSalePriceCentsToProduct < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :sale_price_amount, :integer,
4
4
  :default => 0, :null => false
@@ -1,4 +1,4 @@
1
- class AddPriceInARangeCentsToProducts < ActiveRecord::Migration
1
+ class AddPriceInARangeCentsToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :price_in_a_range_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddValidatesMethodAmountCentsToProducts < ActiveRecord::Migration
1
+ class AddValidatesMethodAmountCentsToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :validates_method_amount_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddAliasedAttrToProducts < ActiveRecord::Migration
1
+ class AddAliasedAttrToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :aliased_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddDeliveryFeeCentsAndRestockFeeCentsToProduct < ActiveRecord::Migration
1
+ class AddDeliveryFeeCentsAndRestockFeeCentsToProduct < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :delivery_fee_cents, :integer
4
4
  add_column :products, :restock_fee_cents, :integer
@@ -1,4 +1,4 @@
1
- class AddReducedPriceToProducts < ActiveRecord::Migration
1
+ class AddReducedPriceToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :reduced_price_cents, :integer
4
4
  add_column :products, :reduced_price_currency, :string
@@ -1,4 +1,4 @@
1
- class AddSpecialPriceToProducts < ActiveRecord::Migration
1
+ class AddSpecialPriceToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :special_price_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddLambdaPriceToProducts < ActiveRecord::Migration
1
+ class AddLambdaPriceToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :lambda_price_cents, :integer
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddSkipValidationPriceCentsToProducts < ActiveRecord::Migration
1
+ class AddSkipValidationPriceCentsToProducts < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :products, :skip_validation_price_cents, :string
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddOptionalAmountToTransactions < ActiveRecord::Migration
1
+ class AddOptionalAmountToTransactions < (Rails::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def change
3
3
  add_column :transactions, :optional_amount_cents, :integer
4
4
  end
@@ -4,11 +4,11 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
4
4
 
5
5
  describe Money do
6
6
  let!(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
7
- let!(:priceable_from_nil) { Priceable.create(:price => nil) }
8
- let!(:priceable_from_num) { Priceable.create(:price => 1) }
9
- let!(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
10
- let!(:priceable_from_hash) { Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"} )}
11
- let!(:priceable_from_hash_with_indifferent_access) {
7
+ let(:priceable_from_nil) { Priceable.create(:price => nil) }
8
+ let(:priceable_from_num) { Priceable.create(:price => 1) }
9
+ let(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
10
+ let(:priceable_from_hash) { Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"} )}
11
+ let(:priceable_from_hash_with_indifferent_access) {
12
12
  Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"}.with_indifferent_access)
13
13
  }
14
14
  let(:priceable_from_string_with_hyphen) { Priceable.create(:price => '1-2 EUR' )}
@@ -99,6 +99,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
99
99
 
100
100
  context "demongoize" do
101
101
  subject { Priceable.first.price }
102
+
102
103
  it { is_expected.to be_an_instance_of(Money) }
103
104
  it { is_expected.to eq(Money.new(100, 'EUR')) }
104
105
 
@@ -4,11 +4,11 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
4
4
 
5
5
  describe Money do
6
6
  let!(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
7
- let!(:priceable_from_nil) { Priceable.create(:price => nil) }
8
- let!(:priceable_from_num) { Priceable.create(:price => 1) }
9
- let!(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
10
- let!(:priceable_from_hash) { Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"} )}
11
- let!(:priceable_from_hash_with_indifferent_access) {
7
+ let(:priceable_from_nil) { Priceable.create(:price => nil) }
8
+ let(:priceable_from_num) { Priceable.create(:price => 1) }
9
+ let(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
10
+ let(:priceable_from_hash) { Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"} )}
11
+ let(:priceable_from_hash_with_indifferent_access) {
12
12
  Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"}.with_indifferent_access)
13
13
  }
14
14
  let(:priceable_from_string_with_hyphen) { Priceable.create(:price => '1-2 EUR' )}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Loupasakis
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-07-12 00:00:00.000000000 Z
13
+ date: 2018-01-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: money
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: 6.9.0
21
+ version: 6.10.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: 6.9.0
28
+ version: 6.10.0
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: monetize
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -100,22 +100,16 @@ dependencies:
100
100
  name: database_cleaner
101
101
  requirement: !ruby/object:Gem::Requirement
102
102
  requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- version: 0.8.0
106
- - - "<"
103
+ - - "~>"
107
104
  - !ruby/object:Gem::Version
108
- version: 1.4.0
105
+ version: 1.6.1
109
106
  type: :development
110
107
  prerelease: false
111
108
  version_requirements: !ruby/object:Gem::Requirement
112
109
  requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- version: 0.8.0
116
- - - "<"
110
+ - - "~>"
117
111
  - !ruby/object:Gem::Version
118
- version: 1.4.0
112
+ version: 1.6.1
119
113
  - !ruby/object:Gem::Dependency
120
114
  name: test-unit
121
115
  requirement: !ruby/object:Gem::Requirement