public
Description: ActiveRecord::Extension (aka ar-extensions) is a plugin to extend and enhance the functionality of ActiveRecord
Homepage: http://www.continuousthinking.com/tags/arext
Clone URL: git://github.com/zdennis/ar-extensions.git
added timestamps options to not automatically add timestamps even if record 
timestamps is disabled in ActiveRecord::Base
Mon Aug 18 04:43:48 -0700 2008
zdennis (committer)
Tue Aug 19 07:13:28 -0700 2008
commit  8b2ae95a80289c8c75993173d3af58faa22fb2f3
tree    07829c12ce5af77323299746456f7e7b63017e96
parent  0b290a2b87d758d457313e0034ce42ceebdb17dc
...
22
23
24
 
 
25
26
27
...
22
23
24
25
26
27
28
29
0
@@ -22,6 +22,8 @@ ActiveRecord::Schema.define do
0
     t.column :replies_count, :integer
0
     t.column :parent_id, :integer
0
     t.column :type, :string
0
+    t.column :created_at, :datetime
0
+    t.column :updated_at, :datetime
0
   end
0
 
0
   create_table :projects, :force=>true do |t|
...
93
94
95
 
 
96
97
98
...
149
150
151
152
 
153
154
155
...
194
195
196
197
 
198
199
200
...
93
94
95
96
97
98
99
100
...
151
152
153
 
154
155
156
157
...
196
197
198
 
199
200
201
202
0
@@ -93,6 +93,8 @@ class ActiveRecord::Base
0
     # * +synchronize+ - an array of ActiveRecord instances for the model
0
     #   that you are currently importing data into. This synchronizes
0
     #   existing model instances in memory with updates from the import.
0
+    # * +timestamps+ - true|false, tells import to not add timestamps \
0
+    #   (if false) even if record timestamps is disabled in ActiveRecord::Base
0
     #
0
     # == Examples  
0
     #  class BlogPost < ActiveRecord::Base ; end
0
@@ -149,7 +151,7 @@ class ActiveRecord::Base
0
     def import( *args )
0
       @logger = Logger.new(STDOUT)
0
       @logger.level = Logger::DEBUG
0
-      options = { :validate=>true }
0
+      options = { :validate=>true, :timestamps=>true }
0
       options.merge!( args.pop ) if args.last.is_a? Hash
0
       
0
       # assume array of model objects
0
@@ -194,7 +196,7 @@ class ActiveRecord::Base
0
       array_of_attributes = array_of_attributes.dup
0
 
0
       # record timestamps unless disabled in ActiveRecord::Base
0
-      if record_timestamps
0
+      if record_timestamps && options.delete( :timestamps )
0
          add_special_rails_stamps column_names, array_of_attributes, options
0
       end
0
 
...
336
337
338
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
...
336
337
338
 
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
0
@@ -336,5 +336,20 @@ class MysqlImportTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
-  
0
+  def test_import_without_timestamps
0
+    columns = %W{ id author_name }
0
+    values = []
0
+    values << [ 1, "Jerry Carter" ]
0
+    values << [ 2, "Chad Fowler" ]
0
+    
0
+    expected_count = Topic.count
0
+    Topic.import( columns, values,
0
+      :validate=>false,
0
+      :timestamps=>false )
0
+    
0
+    assert_equal expected_count+values.size, Topic.count, "#{ values.size } new records should have been created!"
0
+    assert_equal Topic.find( 1 ).created_at, nil, "created_at should be nil"
0
+    assert_equal Topic.find( 2 ).updated_at, nil, "updated_at should be nil"
0
+  end
0
+
0
 end

Comments