has_money_fields 0.0.1 → 0.0.2

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.
data/History.md ADDED
@@ -0,0 +1,11 @@
1
+
2
+ 0.0.2 / 2011-07-14
3
+ ==================
4
+
5
+ * Added possibility to use some AR validations
6
+ * Added changelog
7
+
8
+ 0.0.1 / 2011-07-14
9
+ ==================
10
+
11
+ * Added TODO for future version
data/README.md CHANGED
@@ -68,4 +68,6 @@ class Product < ActiveRecord::Base
68
68
  end
69
69
  ```
70
70
 
71
+ # TODO
71
72
 
73
+ * Make dirty attributes and validations work
@@ -9,10 +9,6 @@ module HasMoneyFields
9
9
  options.assert_valid_keys(:only_cents)
10
10
 
11
11
  args.each do |attr|
12
- constructor = Proc.new do |cents, currency|
13
- Money.new(cents || 0, currency || Money.default_currency)
14
- end
15
-
16
12
  converter = Proc.new do |value|
17
13
  if value.respond_to? :to_money
18
14
  value.to_money
@@ -30,8 +26,10 @@ module HasMoneyFields
30
26
  self.composed_of attr,
31
27
  :class_name => "Money",
32
28
  :mapping => mapping,
33
- :constructor => constructor,
34
- :converter => converter
29
+ :constructor => :new,
30
+ :converter => converter,
31
+ :allow_nil => true
32
+
35
33
  end
36
34
  end
37
35
  end
@@ -1,3 +1,3 @@
1
1
  module HasMoneyFields
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -27,4 +27,29 @@ describe "Model with money fields" do
27
27
  its(:money_price_in_usd) { should == 1000 }
28
28
  its(:price_in_usd) { should == Money.new(1000, "USD") }
29
29
  end
30
+
31
+ context "with a nil values" do
32
+ before do
33
+ @product = Product.create! :price => nil, :price_in_usd => nil
34
+ end
35
+ subject {@product}
36
+
37
+ its(:money_price){ should be_nil }
38
+ its(:currency_price){ should be_nil }
39
+ its(:money_price_in_usd) { should be_nil }
40
+ end
41
+
42
+ context "with validations" do
43
+ before do
44
+ Product.class_eval do
45
+ validates :price, :presence => true
46
+ end
47
+
48
+ @product = Product.new :price => nil
49
+ end
50
+
51
+ it "is invalid" do
52
+ @product.save.should be_false
53
+ end
54
+ end
30
55
  end
data/spec/models.rb CHANGED
@@ -5,7 +5,7 @@ ActiveRecord::Schema.define(:version => 1) do
5
5
 
6
6
  t.integer :money_price_in_usd, :default => 0
7
7
 
8
- t.string :name, :null => false
8
+ t.string :name
9
9
  end
10
10
  end
11
11
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_money_fields
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francisco J. Casas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-11 00:00:00 +02:00
18
+ date: 2011-07-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -133,6 +133,7 @@ files:
133
133
  - .gitignore
134
134
  - Gemfile
135
135
  - Guardfile
136
+ - History.md
136
137
  - LICENSE
137
138
  - README.md
138
139
  - Rakefile