Skip to content
This repository
Browse code

Added support for created_at field

  • Loading branch information...
commit 7436b512d4a198914c65647661549a326cd7d498 1 parent b436930
Jérémy FRERE jerefrer authored

Showing 2 changed files with 56 additions and 0 deletions. Show diff stats Hide diff stats

  1. +21 0 lib/paperclip/attachment.rb
  2. +35 0 test/attachment_test.rb
21 lib/paperclip/attachment.rb
@@ -99,6 +99,7 @@ def assign uploaded_file
99 99 instance_write(:content_type, file.content_type.to_s.strip)
100 100 instance_write(:file_size, file.size)
101 101 instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
  102 + instance_write(:created_at, Time.now) if has_enabled_but_unset_created_at?
102 103 instance_write(:updated_at, Time.now)
103 104
104 105 @dirty = true
@@ -254,6 +255,15 @@ def content_type
254 255 instance_read(:content_type)
255 256 end
256 257
  258 + # Returns the creation time of the file as originally assigned, and
  259 + # lives in the <attachment>_created_at attribute of the model.
  260 + def created_at
  261 + if able_to_store_created_at?
  262 + time = instance_read(:created_at)
  263 + time && time.to_f.to_i
  264 + end
  265 + end
  266 +
257 267 # Returns the last modified time of the file as originally assigned, and
258 268 # lives in the <attachment>_updated_at attribute of the model.
259 269 def updated_at
@@ -429,6 +439,7 @@ def queue_all_for_delete #:nodoc:
429 439 instance_write(:content_type, nil)
430 440 instance_write(:file_size, nil)
431 441 instance_write(:fingerprint, nil)
  442 + instance_write(:created_at, nil) if has_enabled_but_unset_created_at?
432 443 instance_write(:updated_at, nil)
433 444 end
434 445
@@ -453,5 +464,15 @@ def cleanup_filename(filename)
453 464 filename
454 465 end
455 466 end
  467 +
  468 + # Check if attachment database table has a created_at field
  469 + def able_to_store_created_at?
  470 + @instance.respond_to?("#{name}_created_at".to_sym)
  471 + end
  472 +
  473 + # Check if attachment database table has a created_at field which is not yet set
  474 + def has_enabled_but_unset_created_at?
  475 + able_to_store_created_at? && !instance_read(:created_at)
  476 + end
456 477 end
457 478 end
35 test/attachment_test.rb
@@ -1101,6 +1101,41 @@ def do_after_all; end
1101 1101 assert_equal File.size(@file), @dummy.avatar.size
1102 1102 end
1103 1103
  1104 + should "return the right value when sent #avatar_file_size" do
  1105 + @dummy.avatar = @file
  1106 + assert_equal File.size(@file), @dummy.avatar.size
  1107 + end
  1108 +
  1109 + context "and avatar_created_at column" do
  1110 + setup do
  1111 + ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
  1112 + rebuild_class
  1113 + @dummy = Dummy.new
  1114 + end
  1115 +
  1116 + should "not error when assigned an attachment" do
  1117 + assert_nothing_raised { @dummy.avatar = @file }
  1118 + end
  1119 +
  1120 + should "return the creation time when sent #avatar_created_at" do
  1121 + now = Time.now
  1122 + Time.stubs(:now).returns(now)
  1123 + @dummy.avatar = @file
  1124 + assert_equal now.to_i, @dummy.avatar.created_at
  1125 + end
  1126 +
  1127 + should "return the creation time when sent #avatar_created_at and the entry has been updated" do
  1128 + creation = 2.hours.ago
  1129 + now = Time.now
  1130 + Time.stubs(:now).returns(creation)
  1131 + @dummy.avatar = @file
  1132 + Time.stubs(:now).returns(now)
  1133 + @dummy.avatar = @file
  1134 + assert_equal creation.to_i, @dummy.avatar.created_at
  1135 + assert_not_equal now.to_i, @dummy.avatar.created_at
  1136 + end
  1137 + end
  1138 +
1104 1139 context "and avatar_updated_at column" do
1105 1140 setup do
1106 1141 ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp

0 comments on commit 7436b51

Please sign in to comment.
Something went wrong with that request. Please try again.