Permalink
Browse files

Fixes #134, Overriding the setter for :cost here is causing a stack o…

…verflow when cost is attempted to be set on a project. Simply set cost to 0.0 if something's set it to null/"".
  • Loading branch information...
1 parent 7e8a398 commit 34423a60d583fc5ffff26d43b68b4e64d98e1017 @fermion fermion committed Nov 7, 2013
Showing with 40 additions and 3 deletions.
  1. +4 −3 app/models/project.rb
  2. +36 −0 spec/models/project_spec.rb
View
7 app/models/project.rb
@@ -10,7 +10,8 @@ class Project < ActiveRecord::Base
has_many :assignments, :dependent => :destroy
has_many :work_weeks, through: :assignments
has_many :users, through: :assignments
-
+
+ before_save :check_cost
after_update :update_originator_timestamp
validates_presence_of :name, :client
@@ -25,7 +26,7 @@ def proposed_for_user?(user)
# DB column can't be null but incoming params may pass it. If the key's present
# AR uses the value whatever it is, even if it's "". This doesn't trigger
# the default value of 0.0 to be set. Ignore blank values.
- def cost=(new_cost)
- assign_attributes(cost: new_cost) unless new_cost.blank?
+ def check_cost
+ assign_attributes(cost: 0.0) if self.cost.blank?
end
end
View
36 spec/models/project_spec.rb
@@ -12,6 +12,7 @@
project.valid?.should be_true
end
end
+
describe "after_update callback" do
it "should update the updated_at timestamp for a user that modifies a project" do
with_versioning do
@@ -36,4 +37,39 @@
end
end
end
+
+ describe "#check_cost" do
+ it "should do nothing if the cost is not nil" do
+ project = FactoryGirl.build(:project)
+ project.cost = cost = 100.0
+
+ lambda {
+ project.save!
+ }.should_not raise_error
+
+ project.cost.should eq(cost)
+ end
+
+ it "should set cost to 0.0 if cost is nil" do
+ project = FactoryGirl.build(:project)
+ project.cost = nil
+
+ lambda {
+ project.save!
+ }.should_not raise_error
+
+ project.cost.should eq(0.0)
+ end
+
+ it "should set cost to 0.0 if cost is an empty string" do
+ project = FactoryGirl.build(:project)
+ project.cost = ""
+
+ lambda {
+ project.save!
+ }.should_not raise_error
+
+ project.cost.should eq(0.0)
+ end
+ end
end

0 comments on commit 34423a6

Please sign in to comment.