public
Description: Paperclip File Management Plugin
Homepage: http://www.thoughtbot.com/projects/paperclip
Clone URL: git://github.com/thoughtbot/paperclip.git
Merged content_type validation changes from austin.bain.
Changed keyword-collection mimetype matching to regexp matching.
jyurek (author)
Sun May 04 19:38:28 -0700 2008
commit  69525425d37d75d4c7330d25558def1604107a3b
tree    aadb20c17e9a6aad1453aeffd9c337a5c8414fb7
parent  be890c983b06828914f8641d1774dd960a1d3c5f
...
37
38
39
40
41
42
43
44
45
...
180
181
182
183
 
184
185
186
187
 
188
189
190
191
 
 
 
 
 
 
192
193
194
...
37
38
39
 
 
 
40
41
42
...
177
178
179
 
180
181
182
183
 
184
185
 
 
 
186
187
188
189
190
191
192
193
194
0
@@ -37,9 +37,6 @@ require 'paperclip/attachment'
0
 module Paperclip
0
 
0
   VERSION = "2.1.0"
0
-
0
- @@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg']
0
- mattr_reader :content_types
0
 
0
   class << self
0
     # Provides configurability to Paperclip. There are a number of options available, such as:
0
@@ -180,15 +177,18 @@ module Paperclip
0
     
0
     # Places ActiveRecord-style validations on the content type of the file assigned. The
0
     # possible options are:
0
- # * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default. Use :image to allow all standard image types.
0
+ # * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default.
0
     # * +message+: The message to display when the uploaded file has an invalid content type.
0
     def validates_attachment_content_type name, options = {}
0
       attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
- options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Paperclip.content_types : t }.flatten unless options[:content_type].nil?
0
+ valid_types = [options[:content_type]].flatten
0
         
0
- unless options[:content_type].empty?
0
- unless attachment.original_filename.blank? || options[:content_type].include?(instance[:"#{name}_content_type"])
0
- options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
0
+ unless attachment.original_filename.nil?
0
+ unless options[:content_type].blank?
0
+ content_type = instance[:"#{name}_content_type"]
0
+ unless valid_types.any?{|t| t === content_type }
0
+ options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
0
+ end
0
           end
0
         end
0
       end
...
64
65
66
67
68
69
70
71
72
73
74
75
76
 
 
 
 
 
 
 
77
78
79
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
124
125
...
64
65
66
 
 
 
 
 
 
 
 
 
 
67
68
69
70
71
72
73
74
75
76
...
103
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
0
@@ -64,16 +64,13 @@ class PaperclipTest < Test::Unit::TestCase
0
       assert Dummy.new.respond_to?(:avatar=)
0
     end
0
 
0
- [[:presence, nil, "5k.png", nil],
0
- [:size, {:in => 1..10240}, "5k.png", "12k.png"],
0
- [:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"],
0
- [:content_type2, {:content_type => :image}, "5k.png", "text.txt"],
0
- [:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"],
0
- [:presence2, {:message => "error"}, "5k.png", nil, "error"],
0
- [:size2, {:in => 1..10240, :message => 'size is not between #{min} and #{max} bytes.'}, "5k.png", "12k.png", "size is not between 1 and 10240 bytes."],
0
- [:size3, {:in => 1..10240}, nil, "12k.png"],
0
- [:content_type4,{:content_type => "image/png", :message => "error"}, "5k.png", "text.txt", "error"],
0
- [:content_type5,{:content_type => "image/png"}, nil, "text.txt"]].each do |args|
0
+ [[:presence, nil, "5k.png", nil],
0
+ [:size, {:in => 1..10240}, "5k.png", "12k.png"],
0
+ [:size2, {:in => 1..10240}, nil, "12k.png"],
0
+ [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
0
+ [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
0
+ [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
0
+ [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
0
       context "with #{args[0]} validations" do
0
         setup do
0
           Dummy.class_eval do
0
@@ -106,20 +103,20 @@ class PaperclipTest < Test::Unit::TestCase
0
           end
0
         end
0
         
0
- context "and an invalid file with :message" do
0
- setup do
0
- @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
0
- end
0
-
0
- should "have errors" do
0
- if args[1] && args[1][:message] && args[4]
0
- @dummy.avatar = @file
0
- assert ! @dummy.avatar.valid?
0
- assert_equal 1, @dummy.avatar.errors.length
0
- assert_equal args[4], @dummy.avatar.errors[0]
0
- end
0
- end
0
- end
0
+# context "and an invalid file with :message" do
0
+# setup do
0
+# @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
0
+# end
0
+#
0
+# should "have errors" do
0
+# if args[1] && args[1][:message] && args[4]
0
+# @dummy.avatar = @file
0
+# assert ! @dummy.avatar.valid?
0
+# assert_equal 1, @dummy.avatar.errors.length
0
+# assert_equal args[4], @dummy.avatar.errors[0]
0
+# end
0
+# end
0
+# end
0
       end
0
     end
0
   end

Comments

    No one has commented yet.