Skip to content

Commit

Permalink
remove temp_path= and temp_data= methods so they cant be assigned wit…
Browse files Browse the repository at this point in the history
…h multiple attributes
  • Loading branch information
technoweenie committed Oct 6, 2008
1 parent 88af790 commit cda2d14
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 23 deletions.
17 changes: 5 additions & 12 deletions lib/technoweenie/attachment_fu.rb
Expand Up @@ -167,6 +167,7 @@ def self.extended(base)
base.after_save :after_process_attachment
base.after_destroy :destroy_file
base.after_validation :process_attachment
base.attr_accessible
if defined?(::ActiveSupport::Callbacks)
base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
end
Expand Down Expand Up @@ -331,9 +332,9 @@ def uploaded_data=(file_data)
end
if file_data.is_a?(StringIO)
file_data.rewind
self.temp_data = file_data.read
set_temp_data file_data.read
else
self.temp_path = file_data
self.temp_paths.unshift file_data
end
end

Expand All @@ -352,22 +353,14 @@ def temp_paths
[] : [copy_to_temp_file(full_filename)])
end

# Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no
# attempt to remove the files, so Tempfiles should be used. Tempfiles remove themselves when they go out of scope.
# You can also use string paths for temporary files, such as those used for uploaded files in a web server.
def temp_path=(value)
temp_paths.unshift value
temp_path
end

# Gets the data from the latest temp file. This will read the file into memory.
def temp_data
save_attachment? ? File.read(temp_path) : nil
end

# Writes the given data to a Tempfile and adds it to the collection of temp files.
def temp_data=(data)
self.temp_path = write_to_temp_file data unless data.nil?
def set_temp_data(data)
temp_paths.unshift write_to_temp_file data unless data.nil?
end

# Copies the given file to a randomly named Tempfile.
Expand Down
Expand Up @@ -46,7 +46,7 @@ def resize_image(img, size)
self.height = result.extent.size.height if respond_to?(:height)

# Get a new temp_path for the image before saving
self.temp_path = Tempfile.new(random_tempfile_filename, Technoweenie::AttachmentFu.tempfile_path).path
temp_paths.unshift Tempfile.new(random_tempfile_filename, Technoweenie::AttachmentFu.tempfile_path).path
result.save self.temp_path, OSX::NSJPEGFileType
self.size = File.size(self.temp_path)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/technoweenie/attachment_fu/processors/gd2_processor.rb
Expand Up @@ -44,7 +44,7 @@ def resize_image(img, size)
w, h = [img.width, img.height] / size.to_s
img.resize!(w, h, false)
end
self.temp_path = random_tempfile_filename
temp_paths.unshift random_tempfile_filename
self.size = img.export(self.temp_path)
end

Expand Down
Expand Up @@ -34,7 +34,7 @@ def resize_image(img, size)
# supports.
filename.sub! /gif$/, 'png'
content_type.sub!(/gif$/, 'png')
self.temp_path = write_to_temp_file(filename)
temp_paths.unshift write_to_temp_file(filename)
grab_dimensions = lambda do |img|
self.width = img.width if respond_to?(:width)
self.height = img.height if respond_to?(:height)
Expand Down
Expand Up @@ -51,7 +51,7 @@ def resize_image(img, size)
commands.resize(size.to_s)
end
end
self.temp_path = img
temp_paths.unshift img
end
end
end
Expand Down
Expand Up @@ -46,7 +46,7 @@ def resize_image(img, size)
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows) }
end
img.strip! unless attachment_options[:keep_profile]
self.temp_path = write_to_temp_file(img.to_blob)
temp_paths.unshift write_to_temp_file(img.to_blob)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/backends/file_system_test.rb
Expand Up @@ -52,7 +52,7 @@ def test_should_delete_old_file_when_updating(klass = FileAttachment)
assert_not_created do
use_temp_file 'files/rails.png' do |file|
attachment.filename = 'rails2.png'
attachment.temp_path = File.join(fixture_path, file)
attachment.temp_paths.unshift File.join(fixture_path, file)
attachment.save!
assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
assert !File.exists?(old_filename), "#{old_filename} still exists"
Expand Down
6 changes: 3 additions & 3 deletions test/base_attachment_tests.rb
Expand Up @@ -31,7 +31,7 @@ def test_reassign_attribute_data
assert_valid attachment
assert attachment.size > 0, "no data was set"

attachment.temp_data = 'wtf'
attachment.set_temp_data 'wtf'
assert attachment.save_attachment?
attachment.save!

Expand All @@ -45,7 +45,7 @@ def test_no_reassign_attribute_data_on_nil
assert_valid attachment
assert attachment.size > 0, "no data was set"

attachment.temp_data = nil
attachment.set_temp_data nil
assert !attachment.save_attachment?
end
end
Expand All @@ -55,7 +55,7 @@ def test_should_overwrite_old_contents_when_updating
assert_not_created do # no new db_file records
use_temp_file 'files/rails.png' do |file|
attachment.filename = 'rails2.png'
attachment.temp_path = File.join(fixture_path, file)
attachment.temp_paths.unshift File.join(fixture_path, file)
attachment.save!
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/processors/rmagick_test.rb
Expand Up @@ -181,7 +181,7 @@ def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbs
assert_not_created do
use_temp_file "files/rails.png" do |file|
attachment.filename = 'rails2.png'
attachment.temp_path = File.join(fixture_path, file)
attachment.temp_paths.unshift File.join(fixture_path, file)
attachment.save
new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithT
# #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
# set temp_path at all?
#
# attachment.temp_path = File.join(fixture_path, file)
# attachment.temp_paths.unshift File.join(fixture_path, file)
attachment.save!
end
end
Expand Down

0 comments on commit cda2d14

Please sign in to comment.