Skip to content

Commit

Permalink
Added support for created_at field
Browse files Browse the repository at this point in the history
  • Loading branch information
jerefrer committed Jun 23, 2012
1 parent b436930 commit 7436b51
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def assign uploaded_file
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
Expand Down Expand Up @@ -254,6 +255,15 @@ def content_type
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
Expand Down Expand Up @@ -429,6 +439,7 @@ def queue_all_for_delete #:nodoc:
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

Expand All @@ -453,5 +464,15 @@ def cleanup_filename(filename)
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
35 changes: 35 additions & 0 deletions test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,41 @@ def do_after_all; end
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
Expand Down

0 comments on commit 7436b51

Please sign in to comment.