Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Fields other than <name>_file_name are not required for operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Dec 10, 2008
1 parent 9c51087 commit c72812f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/paperclip/attachment.rb
Expand Up @@ -62,7 +62,6 @@ def assign uploaded_file

if uploaded_file.is_a?(Paperclip::Attachment)
uploaded_file = uploaded_file.to_file(:original)
close_uploaded_file = true if uploaded_file.respond_to?(:close)
end

return nil unless valid_assignment?(uploaded_file)
Expand All @@ -89,7 +88,6 @@ def assign uploaded_file
# Reset the file size if the original file was reprocessed.
instance_write(:file_size, uploaded_file.size.to_i)
ensure
uploaded_file.close if close_uploaded_file
validate
end

Expand Down Expand Up @@ -153,6 +151,10 @@ def save
def original_filename
instance_read(:file_name)
end

def content_type
instance_read(:content_type)
end

def updated_at
time = instance_read(:updated_at)
Expand Down Expand Up @@ -193,7 +195,6 @@ def self.interpolations
# again.
def reprocess!
new_original = Tempfile.new("paperclip-reprocess")
new_original.binmode
if old_original = to_file(:original)
new_original.write( old_original.read )
new_original.rewind
Expand All @@ -214,11 +215,15 @@ def file?
end

def instance_write(attr, value)
instance.send(:"#{name}_#{attr}=", value)
setter = :"#{name}_#{attr}="
responds = instance.respond_to?(setter)
instance.send(setter, value) if responds || attr.to_s == "file_name"
end

def instance_read(attr)
instance.send(:"#{name}_#{attr}")
getter = :"#{name}_#{attr}"
responds = instance.respond_to?(getter)
instance.send(getter) if responds || attr.to_s == "file_name"
end

private
Expand Down
57 changes: 57 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -367,4 +367,61 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end

context "An attachment with only a avatar_file_name column" do
setup do
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
table.column :avatar_file_name, :string
end
rebuild_class
@dummy = Dummy.new
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
end

should "not error when assigned an attachment" do
assert_nothing_raised { @dummy.avatar = @file }
end

should "return nil when sent #avatar_updated_at" do
@dummy.avatar = @file
assert_nil @dummy.avatar.updated_at
end

context "and avatar_updated_at column" do
setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_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 right value when sent #avatar_updated_at" do
now = Time.now
Time.stubs(:now).returns(now)
@dummy.avatar = @file
assert_equal now.to_i, @dummy.avatar.updated_at
end
end

context "and avatar_content_type column" do
setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
rebuild_class
@dummy = Dummy.new
end

should "not error when assigned an attachment" do
assert_nothing_raised { @dummy.avatar = @file }
end

should "return the right value when sent #avatar_content_type" do
@dummy.avatar = @file
assert_equal "image/png", @dummy.avatar.content_type
end
end

end
end
3 changes: 3 additions & 0 deletions test/helper.rb
Expand Up @@ -35,7 +35,10 @@ def rebuild_model options = {}
table.column :avatar_file_size, :integer
table.column :avatar_updated_at, :datetime
end
rebuild_class options
end

def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Expand Down

0 comments on commit c72812f

Please sign in to comment.