dm-timestamps 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dm-timestamps.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
 
3
- gem 'dm-core', '=0.9.3'
3
+ gem 'dm-core', '=0.9.4'
4
4
  require 'dm-core'
5
5
 
6
6
  module DataMapper
@@ -8,8 +8,8 @@ module DataMapper
8
8
  TIMESTAMP_PROPERTIES = {
9
9
  :updated_at => lambda { |r| r.updated_at = DateTime.now },
10
10
  :updated_on => lambda { |r| r.updated_on = Date.today },
11
- :created_at => lambda { |r| r.created_at = DateTime.now if r.new_record? },
12
- :created_on => lambda { |r| r.created_on = Date.today if r.new_record? },
11
+ :created_at => lambda { |r| r.created_at = DateTime.now if r.new_record? && r.created_at.nil? },
12
+ :created_on => lambda { |r| r.created_on = Date.today if r.new_record? && r.created_on.nil?},
13
13
  }
14
14
 
15
15
  def self.included(model)
@@ -19,8 +19,10 @@ module DataMapper
19
19
  private
20
20
 
21
21
  def set_timestamp_properties
22
- self.class.properties.slice(*TIMESTAMP_PROPERTIES.keys).compact.each do |property|
23
- TIMESTAMP_PROPERTIES[property.name][self]
22
+ if dirty?
23
+ self.class.properties.slice(*TIMESTAMP_PROPERTIES.keys).compact.each do |property|
24
+ TIMESTAMP_PROPERTIES[property.name][self]
25
+ end
24
26
  end
25
27
  end
26
28
  end # module Timestamp
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Timestamps
3
- VERSION = "0.9.3"
3
+ VERSION = "0.9.4"
4
4
  end
5
- end
5
+ end
@@ -22,6 +22,20 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
22
22
  repository(:default).adapter.execute('DELETE from green_smoothies');
23
23
  end
24
24
 
25
+ it "should not set the created_at/on fields if they're already set" do
26
+ repository(:default) do
27
+ green_smoothie = GreenSmoothie.new(:name => 'Banana')
28
+ time = (DateTime.now - 100)
29
+ green_smoothie.created_at = time
30
+ green_smoothie.created_on = time
31
+ green_smoothie.save
32
+ green_smoothie.created_at.should == time
33
+ green_smoothie.created_on.should == time
34
+ green_smoothie.created_at.should be_a_kind_of(DateTime)
35
+ green_smoothie.created_on.should be_a_kind_of(Date)
36
+ end
37
+ end
38
+
25
39
  it "should set the created_at/on fields on creation" do
26
40
  repository(:default) do
27
41
  green_smoothie = GreenSmoothie.new(:name => 'Banana')
@@ -58,14 +72,40 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
58
72
  green_smoothie.updated_on.should be_a_kind_of(Date)
59
73
  original_updated_at = green_smoothie.updated_at
60
74
  original_updated_on = green_smoothie.updated_on
61
- sleep 1
62
- tomorrow = Date.today + 1
63
- Date.should_receive(:today).and_return(tomorrow)
75
+ time_tomorrow = DateTime.now + 1
76
+ date_tomorrow = Date.today + 1
77
+ DateTime.stub!(:now).and_return { time_tomorrow }
78
+ Date.stub!(:today).and_return { date_tomorrow }
64
79
  green_smoothie.name = 'Cranberry Mango'
65
80
  green_smoothie.save
66
81
  green_smoothie.updated_at.should_not eql(original_updated_at)
67
82
  green_smoothie.updated_on.should_not eql(original_updated_on)
83
+ green_smoothie.updated_at.should eql(time_tomorrow)
84
+ green_smoothie.updated_on.should eql(date_tomorrow)
85
+ end
86
+ end
87
+
88
+ it "should only set the updated_at/on fields on dirty objects" do
89
+ repository(:default) do
90
+ green_smoothie = GreenSmoothie.new(:name => 'Mango')
91
+ green_smoothie.updated_at.should be_nil
92
+ green_smoothie.updated_on.should be_nil
93
+ green_smoothie.save
94
+ green_smoothie.updated_at.should be_a_kind_of(DateTime)
95
+ green_smoothie.updated_on.should be_a_kind_of(Date)
96
+ original_updated_at = green_smoothie.updated_at
97
+ original_updated_on = green_smoothie.updated_on
98
+ time_tomorrow = DateTime.now + 1
99
+ date_tomorrow = Date.today + 1
100
+ DateTime.stub!(:now).and_return { time_tomorrow }
101
+ Date.stub!(:today).and_return { date_tomorrow }
102
+ green_smoothie.save
103
+ green_smoothie.updated_at.should_not eql(time_tomorrow)
104
+ green_smoothie.updated_on.should_not eql(date_tomorrow)
105
+ green_smoothie.updated_at.should eql(original_updated_at)
106
+ green_smoothie.updated_on.should eql(original_updated_on)
68
107
  end
69
108
  end
109
+
70
110
  end
71
111
  end
data/spec/spec_helper.rb CHANGED
@@ -10,7 +10,7 @@ def load_driver(name, default_uri)
10
10
  lib = "do_#{name}"
11
11
 
12
12
  begin
13
- gem lib, '=0.9.3'
13
+ gem lib, '=0.9.4'
14
14
  require lib
15
15
  DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
16
16
  DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-timestamps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Foy Savas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-24 00:00:00 -05:00
12
+ date: 2008-08-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.3
23
+ version: 0.9.4
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe