Commit 7436b512 by Jérémy FRERE

Added support for created_at field

parent b4369301
...@@ -99,6 +99,7 @@ module Paperclip ...@@ -99,6 +99,7 @@ module Paperclip
instance_write(:content_type, file.content_type.to_s.strip) instance_write(:content_type, file.content_type.to_s.strip)
instance_write(:file_size, file.size) instance_write(:file_size, file.size)
instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint) instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
instance_write(:created_at, Time.now) if has_enabled_but_unset_created_at?
instance_write(:updated_at, Time.now) instance_write(:updated_at, Time.now)
@dirty = true @dirty = true
...@@ -254,6 +255,15 @@ module Paperclip ...@@ -254,6 +255,15 @@ module Paperclip
instance_read(:content_type) instance_read(:content_type)
end end
# Returns the creation time of the file as originally assigned, and
# lives in the <attachment>_created_at attribute of the model.
def created_at
if able_to_store_created_at?
time = instance_read(:created_at)
time && time.to_f.to_i
end
end
# Returns the last modified time of the file as originally assigned, and # Returns the last modified time of the file as originally assigned, and
# lives in the <attachment>_updated_at attribute of the model. # lives in the <attachment>_updated_at attribute of the model.
def updated_at def updated_at
...@@ -429,6 +439,7 @@ module Paperclip ...@@ -429,6 +439,7 @@ module Paperclip
instance_write(:content_type, nil) instance_write(:content_type, nil)
instance_write(:file_size, nil) instance_write(:file_size, nil)
instance_write(:fingerprint, nil) instance_write(:fingerprint, nil)
instance_write(:created_at, nil) if has_enabled_but_unset_created_at?
instance_write(:updated_at, nil) instance_write(:updated_at, nil)
end end
...@@ -453,5 +464,15 @@ module Paperclip ...@@ -453,5 +464,15 @@ module Paperclip
filename filename
end end
end end
# Check if attachment database table has a created_at field
def able_to_store_created_at?
@instance.respond_to?("#{name}_created_at".to_sym)
end
# Check if attachment database table has a created_at field which is not yet set
def has_enabled_but_unset_created_at?
able_to_store_created_at? && !instance_read(:created_at)
end
end end
end end
...@@ -1101,6 +1101,41 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1101,6 +1101,41 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal File.size(@file), @dummy.avatar.size assert_equal File.size(@file), @dummy.avatar.size
end end
should "return the right value when sent #avatar_file_size" do
@dummy.avatar = @file
assert_equal File.size(@file), @dummy.avatar.size
end
context "and avatar_created_at column" do
setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
rebuild_class
@dummy = Dummy.new
end
should "not error when assigned an attachment" do
assert_nothing_raised { @dummy.avatar = @file }
end
should "return the creation time when sent #avatar_created_at" do
now = Time.now
Time.stubs(:now).returns(now)
@dummy.avatar = @file
assert_equal now.to_i, @dummy.avatar.created_at
end
should "return the creation time when sent #avatar_created_at and the entry has been updated" do
creation = 2.hours.ago
now = Time.now
Time.stubs(:now).returns(creation)
@dummy.avatar = @file
Time.stubs(:now).returns(now)
@dummy.avatar = @file
assert_equal creation.to_i, @dummy.avatar.created_at
assert_not_equal now.to_i, @dummy.avatar.created_at
end
end
context "and avatar_updated_at column" do context "and avatar_updated_at column" do
setup do setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment