Permalink
Browse files

Merge branch 'fix_time_in_edit_error'

  • Loading branch information...
2 parents 506afdf + a804df6 commit 85299a8e21512281d4bc9be90a0220083325e96f @rstacruz rstacruz committed May 28, 2012
Showing with 21 additions and 6 deletions.
  1. +10 −5 app/models/time_log.rb
  2. +11 −1 spec/models/time_log_spec.rb
@@ -4,6 +4,8 @@ class TimeLog < ActiveRecord::Base
has_many :comments
belongs_to :user
+ attr_writer :comment_text
+
validates :date, uniqueness: {
scope: :user_id,
message: "user should have only one time log per date" },
@@ -15,10 +17,6 @@ def hours
end
end
- def comment_text=(value)
- self.comments.create(comments: value)
- end
-
def user_name
user.name
end
@@ -28,10 +26,17 @@ def user_email
end
def comment_text
- if comments.any?
+ @comment_text ||= if comments.any?
comments.collect(&:comments).join("\n")
else
nil
end
end
+
+private
+
+ after_save :create_comment_text
+ def create_comment_text
+ self.comments.create(comments: @comment_text) if @comment_text
+ end
end
@@ -13,14 +13,24 @@
it "should set the comment properly" do
@log.comment_text = "Did something"
+ @log.save
- @log.comments.should_not == []
+ @log.comments.should_not be_empty
@log.comments.first.comments.should == "Did something"
end
it "should return the right comment" do
@log.comment_text = "Hello"
@log.comment_text.should == "Hello"
end
+
+ it "should create comments properly" do
+ @log = TimeLog.new user: @user
+ @log.update_attributes in: Time.now, date: Date.today, comment_text: "Hello"
+
+ @log.errors.should be_empty
+ TimeLog.find(@log.id).comments.size.should == 1
+ TimeLog.find(@log.id).comments.first.comments == "Hello"
+ end
end
end

0 comments on commit 85299a8

Please sign in to comment.