public
Rubygem
Description: A lightweight and opinionated but hackable library for attaching images to ActiveRecord models.
Homepage:
Clone URL: git://github.com/norman/has_image.git
added option to not delete image files on destroy
updated option documentation
Gerrit Kaiser (author)
Wed Oct 22 01:08:33 -0700 2008
commit  cb5ffb6f02bc7d7439b2e195a88c9851f93812ec
tree    5cf94cf4942f809b6fb6251f9c966073198de241
parent  69c169cab758cafb2b8e5a430487cb578338ff2d
...
38
39
40
 
 
41
42
43
 
44
45
46
47
48
49
50
 
51
52
53
54
55
 
56
57
58
...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 
 
 
 
 
 
 
 
 
 
 
 
 
89
90
91
...
183
184
185
186
 
187
188
189
...
38
39
40
41
42
43
44
 
45
46
47
48
49
50
51
 
52
53
54
55
56
57
58
59
60
61
...
78
79
80
 
 
 
 
 
 
 
 
 
 
 
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
188
189
190
 
191
192
193
194
0
@@ -38,21 +38,24 @@ module HasImage
0
     #
0
     # * :resize_to => "200x200",
0
     # * :thumbnails => {},
0
+    # * :auto_generate_thumbnails => true,
0
+    # * :delete => true,
0
     # * :max_size => 12.megabytes,
0
     # * :min_size => 4.kilobytes,
0
-    # * :path_prefix => klass.to_s.tableize,
0
+    # * :path_prefix => klass.table_name,
0
     # * :base_path => File.join(RAILS_ROOT, 'public'),
0
     # * :column => :has_image_file,
0
     # * :convert_to => "JPEG",
0
     # * :output_quality => "85",
0
     # * :invalid_image_message => "Can't process the image.",
0
     # * :image_too_small_message => "The image is too small.",
0
-    # * :image_too_big_message => "The image is too big.",
0
+    # * :image_too_big_message => "The image is too big."
0
     def default_options_for(klass)
0
       {
0
         :resize_to => "200x200",
0
         :thumbnails => {},
0
         :auto_generate_thumbnails => true,
0
+        :delete => true,
0
         :max_size => 12.megabytes,
0
         :min_size => 4.kilobytes,
0
         :path_prefix => klass.table_name,
0
@@ -75,17 +78,19 @@ module HasImage
0
     # The different setting options are described below.
0
     # 
0
     # Options:
0
-    # *  <tt>:resize_to</tt> - Dimensions to resize to. This should be an ImageMagick {geometry string}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sizes are recommended.
0
-    # *  <tt>:thumbnails</tt> - A hash of thumbnail names and dimensions. The dimensions should be ImageMagick {geometry strings}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sized are recommended.
0
-    # *  <tt>:min_size</tt> - Minimum file size allowed. It's recommended that you set this size in kilobytes.
0
-    # *  <tt>:max_size</tt> - Maximum file size allowed. It's recommended that you set this size in megabytes.
0
-    # *  <tt>:base_path</tt> - Where to install the images. You should probably leave this alone, except for tests.
0
-    # *  <tt>:path_prefix</tt> - Where to install the images, relative to basepath. You should probably leave this alone.
0
-    # *  <tt>:convert_to</tt> - An ImageMagick format to convert images to. Recommended formats: JPEG, PNG, GIF.
0
-    # *  <tt>:output_quality</tt> - Image output quality passed to ImageMagick.
0
-    # *  <tt>:invalid_image_message</tt> - The message that will be shown when the image data can't be processed.
0
-    # *  <tt>:image_too_small_message</tt> - The message that will be shown when the image file is too small. You should ideally set this to something that tells the user what the minimum is.
0
-    # *  <tt>:image_too_big_message</tt> - The message that will be shown when the image file is too big. You should ideally set this to something that tells the user what the maximum is.
0
+    # *  +:resize_to+ - Dimensions to resize to. This should be an aImageMagick {geometry string}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sizes are recommended.
0
+    # *  +:thumbnails</tt> - A hash of thumbnail names and dimensions. The dimensions should be ImageMagick {geometry strings}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sized are recommended.
0
+    # *  +:auto_generate_thumbnails+ - Flag to indicate whether to automatically generate thumbnails when the image_data changes (e.g. on create). If you set this to false, your application code needs to take care of generating Thumbnails, e.g. using +#generate_thumbnail+
0
+    # *  +:delete+ - Flag to indicate if the images should be delete from the storage (e.g. the Filesystem) when the record is destroyed
0
+    # *  +:min_size</tt> - Minimum file size allowed. It's recommended that you set this size in kilobytes.
0
+    # *  +:max_size</tt> - Maximum file size allowed. It's recommended that you set this size in megabytes.
0
+    # *  +:base_path</tt> - Where to install the images. You should probably leave this alone, except for tests.
0
+    # *  +:path_prefix</tt> - Where to install the images, relative to basepath. You should probably leave this alone.
0
+    # *  +:convert_to</tt> - An ImageMagick format to convert images to. Recommended formats: JPEG, PNG, GIF.
0
+    # *  +:output_quality</tt> - Image output quality passed to ImageMagick.
0
+    # *  +:invalid_image_message</tt> - The message that will be shown when the image data can't be processed.
0
+    # *  +:image_too_small_message</tt> - The message that will be shown when the image file is too small. You should ideally set this to something that tells the user what the minimum is.
0
+    # *  +:image_too_big_message</tt> - The message that will be shown when the image file is too big. You should ideally set this to something that tells the user what the maximum is.
0
     #
0
     # Examples:
0
     #   has_image # uses all default options
0
@@ -183,7 +188,7 @@ module HasImage
0
     
0
     # Deletes the image from the storage.
0
     def remove_images
0
-      return if send(has_image_options[:column]).blank?
0
+      return if send(has_image_options[:column]).blank? || !has_image_options[:delete]
0
       self.class.transaction do
0
         begin
0
           storage.remove_images(self, send(has_image_options[:column]))
...
96
97
98
 
 
 
 
 
 
 
99
100
101
...
96
97
98
99
100
101
102
103
104
105
106
107
108
0
@@ -96,6 +96,13 @@ class PicTest < Test::Unit::TestCase
0
     @pic.save!
0
     assert @pic.destroy
0
   end
0
+  
0
+  def test_destroy_should_not_remove_image_file_if_option_is_set
0
+    Pic.has_image_options[:delete] = false
0
+    @pic = Pic.create!(:image_data => fixture_file_upload("/image.jpg", "image/jpeg"))
0
+    @pic.destroy
0
+    assert File.exist?(@pic.absolute_path)
0
+  end
0
 
0
   def test_destroy_model_with_images_already_deleted_from_filesystem
0
     @pic = Pic.new

Comments