Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fields other than <name>_file_name are not required for operation.

  • Loading branch information...
commit c72812fb8a369bfa09f4c94a49f06f3b165f5904 1 parent 9c51087
@jyurek jyurek authored
View
15 lib/paperclip/attachment.rb
@@ -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)
@@ -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
@@ -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)
@@ -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
@@ -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
View
57 test/attachment_test.rb
@@ -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
View
3  test/helper.rb
@@ -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))
Please sign in to comment.
Something went wrong with that request. Please try again.