Commit c72812fb by Jon Yurek

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

parent 9c510875
...@@ -62,7 +62,6 @@ module Paperclip ...@@ -62,7 +62,6 @@ module Paperclip
if uploaded_file.is_a?(Paperclip::Attachment) if uploaded_file.is_a?(Paperclip::Attachment)
uploaded_file = uploaded_file.to_file(:original) uploaded_file = uploaded_file.to_file(:original)
close_uploaded_file = true if uploaded_file.respond_to?(:close)
end end
return nil unless valid_assignment?(uploaded_file) return nil unless valid_assignment?(uploaded_file)
...@@ -89,7 +88,6 @@ module Paperclip ...@@ -89,7 +88,6 @@ module Paperclip
# Reset the file size if the original file was reprocessed. # Reset the file size if the original file was reprocessed.
instance_write(:file_size, uploaded_file.size.to_i) instance_write(:file_size, uploaded_file.size.to_i)
ensure ensure
uploaded_file.close if close_uploaded_file
validate validate
end end
...@@ -154,6 +152,10 @@ module Paperclip ...@@ -154,6 +152,10 @@ module Paperclip
instance_read(:file_name) instance_read(:file_name)
end end
def content_type
instance_read(:content_type)
end
def updated_at def updated_at
time = instance_read(:updated_at) time = instance_read(:updated_at)
time && time.to_i time && time.to_i
...@@ -193,7 +195,6 @@ module Paperclip ...@@ -193,7 +195,6 @@ module Paperclip
# again. # again.
def reprocess! def reprocess!
new_original = Tempfile.new("paperclip-reprocess") new_original = Tempfile.new("paperclip-reprocess")
new_original.binmode
if old_original = to_file(:original) if old_original = to_file(:original)
new_original.write( old_original.read ) new_original.write( old_original.read )
new_original.rewind new_original.rewind
...@@ -214,11 +215,15 @@ module Paperclip ...@@ -214,11 +215,15 @@ module Paperclip
end end
def instance_write(attr, value) 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 end
def instance_read(attr) 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 end
private private
......
...@@ -367,4 +367,61 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -367,4 +367,61 @@ class AttachmentTest < Test::Unit::TestCase
end end
end 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 end
...@@ -35,7 +35,10 @@ def rebuild_model options = {} ...@@ -35,7 +35,10 @@ def rebuild_model options = {}
table.column :avatar_file_size, :integer table.column :avatar_file_size, :integer
table.column :avatar_updated_at, :datetime table.column :avatar_updated_at, :datetime
end end
rebuild_class options
end
def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip) ActiveRecord::Base.send(:include, Paperclip)
Object.send(:remove_const, "Dummy") rescue nil Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base)) 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