GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: DataMapper port of the Paperclip plugin by Thoughtbot
Homepage: http://invalidlogic.com/dm-paperclip/
Clone URL: git://github.com/krobertson/dm-paperclip.git
Updates for validation methods.  All tests now passing.
krobertson (author)
Wed May 14 23:09:20 -0700 2008
commit  9403e18df600945ce9bb3a7d62533ecd063f3c0f
tree    469e9bbff61ee6f8a13585520d0518ddcb091d19
parent  9d44ba16e10ab40661b6f438df0673218cd1eba8
...
144
145
146
 
 
147
148
149
...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
 
 
178
179
180
...
183
184
185
186
187
188
189
190
191
192
193
194
195
 
 
 
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 
 
 
219
220
221
...
144
145
146
147
148
149
150
151
...
154
155
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
158
159
160
161
162
...
165
166
167
 
 
 
 
 
 
 
 
 
 
168
169
170
171
172
173
174
175
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
178
179
180
181
182
0
@@ -144,6 +144,8 @@ module Paperclip
0
       define_method "#{name}?" do
0
         ! attachment_for(name).original_filename.blank?
0
       end
0
+
0
+ add_validator_to_context(opts_from_validator_args([name]), [name], Paperclip::Validate::CopyAttachmentErrors)
0
     end
0
 
0
     # Places ActiveRecord-style validations on the size of the file assigned. The
0
@@ -152,29 +154,9 @@ module Paperclip
0
     # * +less_than+: equivalent to :in => 0..options[:less_than]
0
     # * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
0
     # * +message+: error message to display, use :min and :max as replacements
0
- #def validates_attachment_size(*fields)
0
- # opts = opts_from_validator_args(fields)
0
- # add_validator_to_context(opts, fields, Paperclip::Validate::SizeValidator)
0
- #end
0
- def validates_attachment_size name, options = {}
0
- @@attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
- unless options[:greater_than].nil?
0
- options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
0
- end
0
- unless options[:less_than].nil?
0
- options[:in] = (0..options[:less_than])
0
- end
0
- unless attachment.original_filename.blank? || options[:in].include?(instance.send(:"#{name}_file_size").to_i)
0
- min = options[:in].first
0
- max = options[:in].last
0
-
0
- if options[:message]
0
- options[:message].gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
0
- else
0
- "file size is not between #{min} and #{max} bytes."
0
- end
0
- end
0
- end
0
+ def validates_attachment_size(*fields)
0
+ opts = opts_from_validator_args(fields)
0
+ add_validator_to_context(opts, fields, Paperclip::Validate::SizeValidator)
0
     end
0
 
0
     # Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
0
@@ -183,39 +165,18 @@ module Paperclip
0
     end
0
 
0
     # Places ActiveRecord-style validations on the presence of a file.
0
- #def validates_attachment_presence(*fields)
0
- # opts = opts_from_validator_args(fields)
0
- # add_validator_to_context(opts, fields, Paperclip::Validate::RequiredFieldValidator)
0
- #end
0
- def validates_attachment_presence name, options = {}
0
- @@attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
- if attachment.original_filename.blank?
0
- options[:message] || "must be set."
0
- end
0
- end
0
+ def validates_attachment_presence(*fields)
0
+ opts = opts_from_validator_args(fields)
0
+ add_validator_to_context(opts, fields, Paperclip::Validate::RequiredFieldValidator)
0
     end
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.
0
     # * +message+: The message to display when the uploaded file has an invalid content type.
0
- #def validates_attachment_content_type(*fields)
0
- # opts = opts_from_validator_args(fields)
0
- # add_validator_to_context(opts, fields, Paperclip::Validate::ContentTypeValidator)
0
- #end
0
- def validates_attachment_content_type name, options = {}
0
- @@attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
- valid_types = [options[:content_type]].flatten
0
-
0
- unless attachment.original_filename.nil?
0
- unless options[:content_type].blank?
0
- content_type = instance.send(:"#{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
0
+ def validates_attachment_content_type(*fields)
0
+ opts = opts_from_validator_args(fields)
0
+ add_validator_to_context(opts, fields, Paperclip::Validate::ContentTypeValidator)
0
     end
0
 
0
     # Returns the attachment definitions defined by each call to has_attached_file.
...
93
94
95
96
 
97
98
99
...
169
170
171
172
 
173
174
175
...
93
94
95
 
96
97
98
99
...
169
170
171
 
172
173
174
175
0
@@ -93,7 +93,7 @@ module Paperclip
0
 
0
     # Returns true if there are any errors on this attachment.
0
     def valid?
0
- errors.length == 0
0
+ @errors.length == 0
0
     end
0
 
0
     # Returns an array containing the errors on this attachment.
0
@@ -169,7 +169,7 @@ module Paperclip
0
     def validate #:nodoc:
0
       unless @validation_errors
0
         @validation_errors = @validations.collect do |v|
0
- v.call(self, instance)
0
+ v.call(self, @instance)
0
         end.flatten.compact.uniq
0
         @errors += @validation_errors
0
       end
...
32
33
34
35
 
36
37
38
...
48
49
50
51
52
53
54
55
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
64
65
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
68
69
...
32
33
34
 
35
36
37
38
...
48
49
50
 
 
 
 
 
 
 
 
 
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
0
@@ -32,7 +32,7 @@ module Paperclip
0
 
0
       def call(target)
0
         field_value = target.validation_property_value(@field_name)
0
- if field_value.nil? || field_value.file.nil? || !File.exist(field_value.file.path)
0
+ if field_value.nil? || field_value.original_filename.blank?
0
           error_message = @options[:message] || "%s must be set".t(DataMapper::Inflection.humanize(@field_name))
0
           add_error(target, error_message , @field_name)
0
           return false
0
@@ -48,15 +48,18 @@ module Paperclip
0
       end
0
 
0
       def call(target)
0
- valid_types = [options[:content_type]].flatten
0
-
0
- unless @options[:content_type].blank?
0
- content_type = target.validation_property_value(:"#{@field_name}_content_type")
0
- unless valid_types.any?{|t| t === content_type }
0
- error_message ||= @options[:message] unless @options[:message].nil?
0
- error_message ||= "%s's content type of '%s' is not a valid content type".t(DataMapper::Inflection.humanize(@field_name), content_type)
0
- add_error(target, error_message , @field_name)
0
- return false
0
+ valid_types = [@options[:content_type]].flatten
0
+ field_value = target.validation_property_value(@field_name)
0
+
0
+ unless field_value.nil? || field_value.original_filename.blank?
0
+ unless @options[:content_type].blank?
0
+ content_type = target.validation_property_value(:"#{@field_name}_content_type")
0
+ unless valid_types.any?{|t| t === content_type }
0
+ error_message ||= @options[:message] unless @options[:message].nil?
0
+ error_message ||= "%s's content type of '%s' is not a valid content type".t(DataMapper::Inflection.humanize(@field_name), content_type)
0
+ add_error(target, error_message , @field_name)
0
+ return false
0
+ end
0
           end
0
         end
0
 
0
@@ -64,6 +67,23 @@ module Paperclip
0
       end
0
     end
0
 
0
+ class CopyAttachmentErrors < DataMapper::Validate::GenericValidator #:nodoc:
0
+ def initialize(field_name, options={})
0
+ super
0
+ @field_name, @options = field_name, options
0
+ end
0
+
0
+ def call(target)
0
+ field_value = target.validation_property_value(@field_name)
0
+ unless field_value.nil? || field_value.original_filename.blank?
0
+ return true if field_value.errors.length == 0
0
+ field_value.errors.each { |message| add_error(target, message, @field_name) }
0
+ return false
0
+ end
0
+ return true
0
+ end
0
+ end
0
+
0
   end
0
 end
0
 
...
100
101
102
 
103
104
105
...
100
101
102
103
104
105
106
0
@@ -100,6 +100,7 @@ class IntegrationTest < Test::Unit::TestCase
0
       @dummy.avatar = "not a valid file but not nil"
0
       assert_equal File.basename(@file.path), @dummy.avatar_file_name
0
       assert @dummy.valid?
0
+ @dummy.other = 'dirty' # must have a change in order to save
0
       assert @dummy.save
0
 
0
       saved_paths.each do |p|
...
82
83
84
85
86
 
 
87
88
89
...
94
95
96
97
98
 
 
99
100
101
...
107
108
109
110
111
112
 
 
 
113
114
115
...
82
83
84
 
 
85
86
87
88
89
...
94
95
96
 
 
97
98
99
100
101
...
107
108
109
 
 
 
110
111
112
113
114
115
0
@@ -82,8 +82,8 @@ class PaperclipTest < Test::Unit::TestCase
0
 
0
           should "not have any errors" do
0
             @dummy.avatar = @file
0
- assert @dummy.avatar.valid?
0
- assert_equal 0, @dummy.avatar.errors.length
0
+ assert @dummy.valid?
0
+ assert_equal 0, @dummy.errors.length
0
           end
0
         end
0
 
0
@@ -94,8 +94,8 @@ class PaperclipTest < Test::Unit::TestCase
0
 
0
           should "have errors" do
0
             @dummy.avatar = @file
0
- assert ! @dummy.avatar.valid?
0
- assert_equal 1, @dummy.avatar.errors.length
0
+ assert ! @dummy.valid?
0
+ assert_equal 1, @dummy.errors.length
0
           end
0
         end
0
         
0
@@ -107,9 +107,9 @@ class PaperclipTest < Test::Unit::TestCase
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
+# assert ! @dummy.valid?
0
+# assert_equal 1, @dummy.errors.length
0
+# assert_equal args[4], @dummy.errors[0]
0
 # end
0
 # end
0
 # end

Comments

    No one has commented yet.