acts_as_price 0.2.0 → 0.2.1

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.
@@ -65,6 +65,9 @@ To test the plugin use the command 'rspec spec/**' inside the dir '~/vendor/plug
65
65
  =KNOWN BUGS
66
66
  The command 'rake test' is not running properly at the moment.
67
67
 
68
+ When creating a text_field, by default Rails gets the 'raw' value from the database, so you probably get the wrong value in your text_field. To prevent this please do the following:
69
+ f.textfield :price, :value => @object.price
70
+
68
71
  =NOTES
69
72
  This plugin comes with Rspec tests.
70
73
  However this plugin assumes that you have a database configuration, it actually don't use the database for the Rspec tests.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_price}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeroen van Ingen"]
12
- s.date = %q{2011-05-01}
12
+ s.date = %q{2011-05-02}
13
13
  s.description = %q{A specified database column acts as a price and creates on the fly methods like 'price' and 'price_in_cents'. For more information visit: http://github.com/jeroeningen/acts_as_price}
14
14
  s.email = %q{jeroeningen@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -18,7 +18,7 @@ module ActiveRecord
18
18
 
19
19
  #setters
20
20
  define_method("#{column_name}=") do |val|
21
- super((val.to_s.gsub(",", ".").to_f * 100).to_s)
21
+ super(((val.to_s.gsub(",", ".").to_f * 100) + 0.5).to_s)
22
22
  end
23
23
  alias_method("price=", "#{column_name}=")
24
24
 
@@ -29,7 +29,7 @@ module ActiveRecord
29
29
 
30
30
  #getters
31
31
  define_method(column_name) do
32
- if super == 0.0
32
+ if super.blank? or super == 0.0
33
33
  ""
34
34
  else
35
35
  if comma
@@ -28,8 +28,12 @@ module ActsAsPriceHelper
28
28
  #test for special cases
29
29
  #test if 1.5 is returned as 1.50
30
30
  test_setter_in_doubles "#{currency}. 1.5", seperator
31
+ test_setter_in_doubles "#{currency}. 1.505", seperator
32
+ test_setter_in_doubles "#{currency}. 1.504", seperator
31
33
  #test if 1,5 is returned as 1.50
32
34
  test_setter_in_doubles "#{currency}. 1,5", seperator
35
+ test_setter_in_doubles "#{currency}. 1,505", seperator
36
+ test_setter_in_doubles "#{currency}. 1,504", seperator
33
37
 
34
38
  #test if float returns 2.05 correctly
35
39
  #float can return 2.05 as 2.04 in some cases
@@ -65,7 +69,7 @@ module ActsAsPriceHelper
65
69
  columns_in_cents.each do |setter|
66
70
  @acts_as_price_model.send("#{setter}=", cents)
67
71
  columns_in_cents.each do |getter|
68
- @acts_as_price_model.send(getter).should == cents.to_i
72
+ @acts_as_price_model.send(getter).should == (cents.to_f + 0.5).to_i
69
73
  end
70
74
  columns_in_doubles.each do |getter|
71
75
  if cents == ""
@@ -89,16 +93,16 @@ module ActsAsPriceHelper
89
93
  columns_in_doubles.each do |setter|
90
94
  @acts_as_price_model.send("#{setter}=", double)
91
95
  columns_in_cents.each do |getter|
92
- @acts_as_price_model.send(getter).should == (double.gsub(",", ".").to_f * 100).to_s.to_i
96
+ @acts_as_price_model.send(getter).should == ((double.gsub(",", ".").to_f * 100) + 0.5).to_s.to_i
93
97
  end
94
98
  columns_in_doubles.each do |getter|
95
99
  if double == ""
96
100
  @acts_as_price_model.send(getter).should == ""
97
101
  else
98
102
  if currency
99
- @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
103
+ @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".").to_s).gsub(".", seperator)
100
104
  else
101
- @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
105
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".").to_s).gsub(".", seperator)
102
106
  end
103
107
  end
104
108
  end
@@ -3,7 +3,7 @@ require File.expand_path("../../spec_helper", __FILE__)
3
3
  describe Car do
4
4
  before(:each) do
5
5
  @column_name = :price
6
- @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995,99")
6
+ @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Stationwagon", :model => "Focus", :price => "23995,99")
7
7
  end
8
8
 
9
9
  context "given an empty car" do
@@ -11,6 +11,7 @@ describe Car do
11
11
  car = Car.new
12
12
  should validate_presence_of :brand
13
13
  should validate_presence_of :cartype
14
+ should validate_presence_of :model
14
15
  car.valid?.should be_false
15
16
  car.save.should be_false
16
17
  end
@@ -52,6 +53,8 @@ describe Car do
52
53
  it "should convert it to the right price in cents" do
53
54
  test_setter_in_doubles "EUR. 25500,5", ","
54
55
  test_setter_in_doubles "EUR. 21599,05", ","
56
+ test_setter_in_doubles "EUR. 21599,055", ","
57
+ test_setter_in_doubles "EUR. 21599,054", ","
55
58
  end
56
59
  end
57
60
 
@@ -53,6 +53,8 @@ describe Fueltype do
53
53
  it "should convert it to the right price in cents" do
54
54
  test_setter_in_doubles "1.5", @seperator
55
55
  test_setter_in_doubles "2.05", @seperator
56
+ test_setter_in_doubles "2.055", @seperator
57
+ test_setter_in_doubles "2.054", @seperator
56
58
  end
57
59
  end
58
60
 
@@ -11,6 +11,12 @@ describe Car do
11
11
  car = stub_model(Car, :price => "23000,59")
12
12
  car.price_in_cents.should == 2300059
13
13
  end
14
+ it "should return the right rounded price" do
15
+ car = stub_model(Car, :price => "23000,595")
16
+ car.price.should == "EUR. 23000,60"
17
+ car = stub_model(Car, :price => "23000,594")
18
+ car.price.should == "EUR. 23000,59"
19
+ end
14
20
  end
15
21
 
16
22
  context "adding the price as a dot seperated value" do
@@ -35,6 +41,13 @@ describe Car do
35
41
  car.currency.should == "EUR"
36
42
  end
37
43
  end
44
+
45
+ context "empty car" do
46
+ it "should return no price" do
47
+ car = Car.new
48
+ car.price.should == ""
49
+ end
50
+ end
38
51
  end
39
52
 
40
53
  #Fueltype returns the price as a dot seperated value
@@ -45,6 +58,12 @@ describe Fueltype do
45
58
  fueltype.price.should == "1.59"
46
59
  fueltype.price_per_liter.should == "1.59"
47
60
  end
61
+ it "should return the right rounded price" do
62
+ fueltype = stub_model(Fueltype, :price => "1,595")
63
+ fueltype.price.should == "1.60"
64
+ fueltype = stub_model(Fueltype, :price => "1,594")
65
+ fueltype.price.should == "1.59"
66
+ end
48
67
  it "should return the price in cents" do
49
68
  fueltype = stub_model(Fueltype, :price_per_liter => "1,59")
50
69
  fueltype.price_per_liter_in_cents.should == 159
@@ -80,4 +99,17 @@ describe Fueltype do
80
99
  fueltype.price_per_liter_in_cents.should == 205
81
100
  end
82
101
  end
102
+ context "using no currency" do
103
+ it "should return no currency" do
104
+ fueltype = stub_model(Fueltype)
105
+ fueltype.currency.should be_nil
106
+ end
107
+ end
108
+
109
+ context "empty fueltype" do
110
+ it "should return no price" do
111
+ fueltype = Fueltype.new
112
+ fueltype.price.should == ""
113
+ end
114
+ end
83
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_price
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeroen van Ingen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-01 00:00:00 -07:00
18
+ date: 2011-05-02 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency