Permalink
Browse files

Added record_timestamps class var to the timestamps plugin

  • Loading branch information...
kamilbednarz committed Jan 23, 2013
1 parent f21862e commit 7b723933b53f23c15894d3a0a8fdc130b0a88347
Showing with 80 additions and 38 deletions.
  1. +10 −3 lib/mongo_mapper/plugins/timestamps.rb
  2. +70 −35 test/functional/test_timestamps.rb
@@ -4,6 +4,11 @@ module Plugins
module Timestamps
extend ActiveSupport::Concern
+ included do
+ class_attribute :record_timestamps
+ self.record_timestamps = true
+ end
+
module ClassMethods
def timestamps!
key :created_at, Time
@@ -13,9 +18,11 @@ def timestamps!
end
def update_timestamps
- now = Time.now.utc
- self[:created_at] = now if !persisted? && !created_at?
- self[:updated_at] = now
+ if self.record_timestamps then
+ now = Time.now.utc
+ self[:created_at] = now if !persisted? && !created_at?
+ self[:updated_at] = now
+ end
end
end
end
@@ -1,6 +1,19 @@
require 'test_helper'
class TimestampsTest < Test::Unit::TestCase
+ context "included" do
+ setup do
+ @klass = Doc do
+ key :first_name, String
+ end
+ @klass.timestamps!
+ end
+
+ should "set record_timestamps to true" do
+ @klass.record_timestamps.should be(true)
+ end
+ end
+
context "timestamping" do
setup do
@klass = Doc do
@@ -12,51 +25,73 @@ class TimestampsTest < Test::Unit::TestCase
@klass.timestamps!
end
- should "set created_at and updated_at on create" do
- doc = @klass.new(:first_name => 'John', :age => 27)
- doc.created_at.should be(nil)
- doc.updated_at.should be(nil)
- doc.save
- doc.created_at.should_not be(nil)
- doc.updated_at.should_not be(nil)
- end
+ context "when #record_timestamps is set to true" do
+ should "set created_at and updated_at on create" do
+ doc = @klass.new(:first_name => 'John', :age => 27)
+ doc.created_at.should be(nil)
+ doc.updated_at.should be(nil)
+ doc.save
+ doc.created_at.should_not be(nil)
+ doc.updated_at.should_not be(nil)
+ end
- should "not overwrite created_at if it already exists" do
- original_created_at = 1.month.ago
- doc = @klass.new(:first_name => 'John', :age => 27, :created_at => original_created_at)
- doc.created_at.to_i.should == original_created_at.to_i
- doc.updated_at.should be_nil
- doc.save
- doc.created_at.to_i.should == original_created_at.to_i
- doc.updated_at.should_not be_nil
- end
+ should "not overwrite created_at if it already exists" do
+ original_created_at = 1.month.ago
+ doc = @klass.new(:first_name => 'John', :age => 27, :created_at => original_created_at)
+ doc.created_at.to_i.should == original_created_at.to_i
+ doc.updated_at.should be_nil
+ doc.save
+ doc.created_at.to_i.should == original_created_at.to_i
+ doc.updated_at.should_not be_nil
+ end
- should "set updated_at on field update but leave created_at alone" do
- doc = @klass.create(:first_name => 'John', :age => 27)
- old_created_at = doc.created_at
- old_updated_at = doc.updated_at
- doc.first_name = 'Johnny'
+ should "set updated_at on field update but leave created_at alone" do
+ doc = @klass.create(:first_name => 'John', :age => 27)
+ old_created_at = doc.created_at
+ old_updated_at = doc.updated_at
+ doc.first_name = 'Johnny'
- Timecop.freeze(Time.now + 5.seconds) do
- doc.save
+ Timecop.freeze(Time.now + 5.seconds) do
+ doc.save
+ end
+
+ doc.created_at.should == old_created_at
+ doc.updated_at.should_not == old_updated_at
end
- doc.created_at.should == old_created_at
- doc.updated_at.should_not == old_updated_at
+ should "set updated_at on document update but leave created_at alone" do
+ doc = @klass.create(:first_name => 'John', :age => 27)
+ old_created_at = doc.created_at.to_f
+
+ new_updated_at = Time.now + 5.seconds
+ Timecop.freeze(new_updated_at) do
+ @klass.update(doc._id, { :first_name => 'Johnny' })
+ end
+
+ doc = doc.reload
+ doc.created_at.to_f.should be_close(old_created_at, 0.001)
+ doc.updated_at.to_f.should be_close(new_updated_at.to_f, 0.001)
+ end
end
- should "set updated_at on document update but leave created_at alone" do
- doc = @klass.create(:first_name => 'John', :age => 27)
- old_created_at = doc.created_at.to_f
+ context "when #record_timestamps is set to false" do
+ setup do
+ @klass.record_timestamps = false
+ end
+
+ teardown do
+ @klass.record_timestamps = true
+ end
- new_updated_at = Time.now + 5.seconds
- Timecop.freeze(new_updated_at) do
- @klass.update(doc._id, { :first_name => 'Johnny' })
+ should "doesn't set updated_at on document create" do
+ doc = @klass.create(:first_name => "John")
+ doc.created_at.should be_nil
end
- doc = doc.reload
- doc.created_at.to_f.should be_close(old_created_at, 0.001)
- doc.updated_at.to_f.should be_close(new_updated_at.to_f, 0.001)
+ should "doesn't set updated_at on document create" do
+ doc = @klass.create(:first_name => "John")
+ doc.updated_at.should be_nil
+ end
end
end
end

0 comments on commit 7b72393

Please sign in to comment.