activerecord_save_many 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/trampoline/activerecord_save_many"
12
12
  gem.authors = ["mccraigmccraig"]
13
13
  gem.add_dependency "activerecord_execute_raw", ">= 0.1.0"
14
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "rspec", ">= 1.2.8"
15
15
  gem.add_development_dependency "yard", ">= 0"
16
16
  gem.add_development_dependency "rr", ">= 0.10.5"
17
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.0
@@ -57,6 +57,22 @@ module ActiveRecord
57
57
  @save_many_max_rows || ActiveRecord::SaveMany::default_max_rows
58
58
  end
59
59
 
60
+ def set_create_timestamps(timestamp, instance)
61
+ if instance.record_timestamps
62
+ instance.write_attribute('created_at', timestamp) if instance.respond_to?(:created_at) && instance.created_at.nil?
63
+ instance.write_attribute('created_on', timestamp) if instance.respond_to?(:created_on) && instance.created_on.nil?
64
+ instance.write_attribute('updated_at', timestamp) if instance.respond_to?(:updated_at) && instance.updated_at.nil?
65
+ instance.write_attribute('updated_on', timestamp) if instance.respond_to?(:updated_on) && instance.updated_on.nil?
66
+ end
67
+ end
68
+
69
+ def set_update_timestamps(timestamp, instance)
70
+ if instance.record_timestamps
71
+ instance.write_attribute('updated_at', timestamp) if instance.respond_to?(:updated_at)
72
+ instance.write_attribute('updated_on', timestamp) if instance.respond_to?(:updated_on)
73
+ end
74
+ end
75
+
60
76
  def save_many(values, options={})
61
77
  Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS , options)
62
78
  return if values.nil? || values.empty?
@@ -67,14 +83,17 @@ module ActiveRecord
67
83
  max_rows = options[:max_rows] || save_many_max_rows
68
84
  batches = Functions::slice_array(max_rows, values)
69
85
 
86
+ timestamp = Time.now
70
87
  batches.each do |rows|
71
88
  rows = rows.map do |obj|
72
89
  if obj.is_a? ActiveRecord::Base
73
90
  obj.send( :callback, :before_save )
74
91
  if obj.id
75
92
  obj.send( :callback, :before_update)
93
+ set_update_timestamps(timestamp, obj)
76
94
  else
77
95
  obj.send( :callback, :before_create )
96
+ set_create_timestamps(timestamp, obj)
78
97
  end
79
98
  raise "#{obj.errors.full_messages.join(', ')}" if !obj.valid?
80
99
  end
@@ -107,10 +107,62 @@ module ActiveRecord
107
107
  k2.save_many_max_rows.should == 100
108
108
  end
109
109
 
110
+ describe "set_create_timestamps" do
111
+ it "should set creation timestamps" do
112
+ k = new_ar_class("Foo") {
113
+ attr_accessor :created_at
114
+ attr_accessor :created_on
115
+ attr_accessor :updated_at
116
+ attr_accessor :updated_on
117
+ def write_attribute(name,value)
118
+ self.send(name+"=", value)
119
+ end
120
+ }
121
+ kinst = k.new
122
+ stub(kinst).record_timestamps{true}
123
+
124
+ t = Time.now
125
+ k.set_create_timestamps(t,kinst)
126
+ kinst.created_at.should == t
127
+ kinst.created_on.should == t
128
+ kinst.updated_at.should == t
129
+ kinst.updated_on.should == t
130
+ end
131
+ end
132
+
133
+ describe "set_update_timestamps" do
134
+ it "should set update timestamps" do
135
+ k = new_ar_class("Foo") {
136
+ attr_accessor :created_at
137
+ attr_accessor :created_on
138
+ attr_accessor :updated_at
139
+ attr_accessor :updated_on
140
+ def write_attribute(name,value)
141
+ self.send(name+"=", value)
142
+ end
143
+ }
144
+ kinst = k.new
145
+ stub(kinst).record_timestamps{true}
146
+
147
+ t = Time.now
148
+ k.set_update_timestamps(t,kinst)
149
+ kinst.created_at.should == nil
150
+ kinst.created_on.should == nil
151
+ kinst.updated_at.should == t
152
+ kinst.updated_on.should == t
153
+ end
154
+ end
155
+
110
156
  # argh. am i really testing the correct sql is generated ?
111
157
  describe "save_many" do
112
158
  def new_ar_stub(classname, column_names, tablename, match_sql)
113
- k=new_ar_class(classname)
159
+ k=new_ar_class(classname) {
160
+ attr_accessor :created_at
161
+ attr_accessor :updated_at
162
+ def write_attribute(name,value)
163
+ self.send(name+"=", value)
164
+ end
165
+ }
114
166
  stub(k).table_name{tablename}
115
167
  cns = column_names.map{|cn| col=Object.new ; stub(col).name{cn} ; col}
116
168
  stub(k).columns{cns}
@@ -135,6 +187,8 @@ module ActiveRecord
135
187
  end
136
188
  mock(kinst).valid?(){valid}
137
189
 
190
+ mock(kinst).record_timestamps(){true}
191
+
138
192
  field_hash.keys.each{ |key|
139
193
  mock(kinst).[](key){field_hash[key]}
140
194
  }
@@ -145,6 +199,8 @@ module ActiveRecord
145
199
  k=new_ar_stub("Foo", [:foo, :bar], "foos", "insert into foos (foo,bar) values ('foofoo','barbar')")
146
200
  kinst = new_ar_inst(k, nil, true, {:foo=>"foofoo", :bar=>"barbar"})
147
201
  k.save_many([kinst])
202
+ kinst.created_at.should_not == nil
203
+ kinst.updated_at.should_not == nil
148
204
  end
149
205
 
150
206
  it "should generate extended insert sql for all model columns with existing objects" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 7
8
- - 1
9
- version: 0.7.1
7
+ - 8
8
+ - 0
9
+ version: 0.8.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - mccraigmccraig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-17 00:00:00 +00:00
17
+ date: 2010-05-11 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -41,8 +41,8 @@ dependencies:
41
41
  segments:
42
42
  - 1
43
43
  - 2
44
- - 9
45
- version: 1.2.9
44
+ - 8
45
+ version: 1.2.8
46
46
  type: :development
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency