Commit c72812fb by Jon Yurek

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

parent 9c510875
......@@ -62,7 +62,6 @@ module Paperclip
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 @@ module Paperclip
# 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 @@ module Paperclip
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 @@ module Paperclip
# 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 @@ module Paperclip
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
......
......@@ -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
......@@ -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))
......
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