Commit 7436b512 by Jérémy FRERE

Added support for created_at field

parent b4369301
......@@ -99,6 +99,7 @@ module Paperclip
instance_write(:content_type, file.content_type.to_s.strip)
instance_write(:file_size, file.size)
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)
@dirty = true
......@@ -254,6 +255,15 @@ module Paperclip
instance_read(:content_type)
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
# lives in the <attachment>_updated_at attribute of the model.
def updated_at
......@@ -429,6 +439,7 @@ module Paperclip
instance_write(:content_type, nil)
instance_write(:file_size, nil)
instance_write(:fingerprint, nil)
instance_write(:created_at, nil) if has_enabled_but_unset_created_at?
instance_write(:updated_at, nil)
end
......@@ -453,5 +464,15 @@ module Paperclip
filename
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
......@@ -1101,6 +1101,41 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal File.size(@file), @dummy.avatar.size
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
setup do
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