paperclip-cloudfiles 2.3.1.1.1 → 2.3.1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/paperclip.rb CHANGED
@@ -46,7 +46,7 @@ end
46
46
  # documentation for Paperclip::ClassMethods for more useful information.
47
47
  module Paperclip
48
48
 
49
- VERSION = "2.3.1.1.1"
49
+ VERSION = "2.3.1.1.2"
50
50
 
51
51
  class << self
52
52
  # Provides configurability to Paperclip. There are a number of options available, such as:
@@ -240,7 +240,7 @@ module Paperclip
240
240
 
241
241
  validates_each(name) do |record, attr, value|
242
242
  attachment = record.attachment_for(name)
243
- attachment.send(:flush_errors)
243
+ attachment.send(:flush_errors) unless attachment.valid?
244
244
  end
245
245
  end
246
246
 
@@ -14,6 +14,7 @@ module Paperclip
14
14
  :convert_options => {},
15
15
  :default_url => "/:attachment/:style/missing.png",
16
16
  :default_style => :original,
17
+ :validations => [],
17
18
  :storage => :filesystem,
18
19
  :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
19
20
  }
@@ -37,6 +38,7 @@ module Paperclip
37
38
  @styles = options[:styles]
38
39
  @normalized_styles = nil
39
40
  @default_url = options[:default_url]
41
+ @validations = options[:validations]
40
42
  @default_style = options[:default_style]
41
43
  @storage = options[:storage]
42
44
  @whiny = options[:whiny_thumbnails] || options[:whiny]
@@ -46,6 +48,7 @@ module Paperclip
46
48
  @queued_for_delete = []
47
49
  @queued_for_write = {}
48
50
  @errors = {}
51
+ @validation_errors = nil
49
52
  @dirty = false
50
53
 
51
54
  initialize_storage
@@ -66,11 +69,13 @@ module Paperclip
66
69
  end
67
70
 
68
71
  # What gets called when you call instance.attachment = File. It clears
69
- # errors, assigns attributes, and processes the file. It
72
+ # errors, assigns attributes, processes the file, and runs validations. It
70
73
  # also queues up the previous file for deletion, to be flushed away on
71
74
  # #save of its host. In addition to form uploads, you can also assign
72
75
  # another Paperclip attachment:
73
76
  # new_user.avatar = old_user.avatar
77
+ # If the file that is assigned is not valid, the processing (i.e.
78
+ # thumbnailing, etc) will NOT be run.
74
79
  def assign uploaded_file
75
80
  ensure_required_accessors!
76
81
 
@@ -94,12 +99,13 @@ module Paperclip
94
99
 
95
100
  @dirty = true
96
101
 
97
- post_process
102
+ post_process if valid?
98
103
 
99
104
  # Reset the file size if the original file was reprocessed.
100
105
  instance_write(:file_size, @queued_for_write[:original].size.to_i)
101
106
  ensure
102
107
  uploaded_file.close if close_uploaded_file
108
+ validate
103
109
  end
104
110
 
105
111
  # Returns the public URL of the attachment, with a given style. Note that
@@ -127,6 +133,12 @@ module Paperclip
127
133
  url(style_name)
128
134
  end
129
135
 
136
+ # Returns true if there are no errors on this attachment.
137
+ def valid?
138
+ validate
139
+ errors.empty?
140
+ end
141
+
130
142
  # Returns an array containing the errors on this attachment.
131
143
  def errors
132
144
  @errors
@@ -140,10 +152,15 @@ module Paperclip
140
152
  # Saves the file, if there are no errors. If there are, it flushes them to
141
153
  # the instance's errors and returns false, cancelling the save.
142
154
  def save
143
- flush_deletes
144
- flush_writes
145
- @dirty = false
146
- true
155
+ if valid?
156
+ flush_deletes
157
+ flush_writes
158
+ @dirty = false
159
+ true
160
+ else
161
+ flush_errors
162
+ false
163
+ end
147
164
  end
148
165
 
149
166
  # Clears out the attachment. Has the same effect as previously assigning
@@ -152,6 +169,7 @@ module Paperclip
152
169
  def clear
153
170
  queue_existing_for_delete
154
171
  @errors = {}
172
+ @validation_errors = nil
155
173
  end
156
174
 
157
175
  # Destroys the attachment. Has the same effect as previously assigning
@@ -264,6 +282,53 @@ module Paperclip
264
282
  file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
265
283
  end
266
284
 
285
+ def validate #:nodoc:
286
+ unless @validation_errors
287
+ @validation_errors = @validations.inject({}) do |errors, validation|
288
+ name, options = validation
289
+ errors[name] = send(:"validate_#{name}", options) if allow_validation?(options)
290
+ errors
291
+ end
292
+ @validation_errors.reject!{|k,v| v == nil }
293
+ @errors.merge!(@validation_errors)
294
+ end
295
+ @validation_errors
296
+ end
297
+
298
+ def allow_validation? options #:nodoc:
299
+ (options[:if].nil? || check_guard(options[:if])) && (options[:unless].nil? || !check_guard(options[:unless]))
300
+ end
301
+
302
+ def check_guard guard #:nodoc:
303
+ if guard.respond_to? :call
304
+ guard.call(instance)
305
+ elsif ! guard.blank?
306
+ instance.send(guard.to_s)
307
+ end
308
+ end
309
+
310
+ def validate_size options #:nodoc:
311
+ if file? && !options[:range].include?(size.to_i)
312
+ options[:message].gsub(/:min/, options[:min].to_s).gsub(/:max/, options[:max].to_s)
313
+ end
314
+ end
315
+
316
+ def validate_presence options #:nodoc:
317
+ options[:message] unless file?
318
+ end
319
+
320
+ def validate_content_type options #:nodoc:
321
+ valid_types = [options[:content_type]].flatten
322
+ unless original_filename.blank?
323
+ unless valid_types.blank?
324
+ content_type = instance_read(:content_type)
325
+ unless valid_types.any?{|t| content_type.nil? || t === content_type }
326
+ options[:message] || "is not one of the allowed file types."
327
+ end
328
+ end
329
+ end
330
+ end
331
+
267
332
  def initialize_storage #:nodoc:
268
333
  @storage_module = Paperclip::Storage.const_get(@storage.to_s.classify)
269
334
  self.extend(@storage_module)
@@ -14,6 +14,18 @@ class AttachmentTest < Test::Unit::TestCase
14
14
  assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
15
15
  end
16
16
 
17
+ should "call a proc sent to check_guard" do
18
+ @dummy = Dummy.new
19
+ @dummy.expects(:one).returns(:one)
20
+ assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one })
21
+ end
22
+
23
+ should "call a method name sent to check_guard" do
24
+ @dummy = Dummy.new
25
+ @dummy.expects(:one).returns(:one)
26
+ assert_equal :one, @dummy.avatar.send(:check_guard, :one)
27
+ end
28
+
17
29
  context "Attachment default_options" do
18
30
  setup do
19
31
  rebuild_model
@@ -462,6 +474,8 @@ class AttachmentTest < Test::Unit::TestCase
462
474
  @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
463
475
  @attachment.expects(:queue_existing_for_delete)
464
476
  @attachment.expects(:post_process)
477
+ @attachment.expects(:valid?).returns(true)
478
+ @attachment.expects(:validate)
465
479
  @dummy.avatar = @not_file
466
480
  end
467
481
 
@@ -185,6 +185,27 @@ class PaperclipTest < Test::Unit::TestCase
185
185
  should "be valid" do
186
186
  assert @dummy.valid?
187
187
  end
188
+
189
+ context "then has a validation added that makes it invalid" do
190
+ setup do
191
+ assert @dummy.save
192
+ Dummy.class_eval do
193
+ validates_attachment_content_type :avatar, :content_type => ["text/plain"]
194
+ end
195
+ @dummy2 = Dummy.find(@dummy.id)
196
+ end
197
+
198
+ should "be invalid when reloaded" do
199
+ assert ! @dummy2.valid?, @dummy2.errors.inspect
200
+ end
201
+
202
+ should "be able to call #valid? twice without having duplicate errors" do
203
+ @dummy2.avatar.valid?
204
+ first_errors = @dummy2.avatar.errors
205
+ @dummy2.avatar.valid?
206
+ assert_equal first_errors, @dummy2.avatar.errors
207
+ end
208
+ end
188
209
  end
189
210
 
190
211
  context "a validation with an if guard clause" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-cloudfiles
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1.1.1
4
+ version: 2.3.1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Yurek
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-27 00:00:00 -06:00
13
+ date: 2010-01-31 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency