Permalink
Browse files

Don't crash when a new attachment is added.

If a model has an existing attachment and styles, and later a new attachment is added (not just new styles), "rake paperclip:refresh:missing_styles" would crash with "TypeError: can't convert nil into Array".
  • Loading branch information...
1 parent a83de65 commit dc53432b3166a1cc711a526e1057931881babf7d @sjmadsen sjmadsen committed with jyurek Dec 14, 2011
Showing with 17 additions and 1 deletion.
  1. +1 −1 lib/paperclip/missing_attachment_styles.rb
  2. +16 −0 test/paperclip_missing_attachment_styles_test.rb
@@ -70,7 +70,7 @@ def self.missing_attachments_styles
Hash.new.tap do |missing_styles|
current_styles.each do |klass, attachment_definitions|
attachment_definitions.each do |attachment_name, styles|
- registered = registered_styles[klass][attachment_name] rescue []
+ registered = registered_styles[klass][attachment_name] || [] rescue []
missed = styles - registered
if missed.present?
klass_sym = klass.to_s.to_sym
@@ -68,6 +68,22 @@ class ::Book < ActiveRecord::Base
Paperclip.save_current_attachments_styles!
assert_equal Hash.new, Paperclip.missing_attachments_styles
end
+
+ should "be able to calculate differences when a new attachment is added to a model" do
+ rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
+ Paperclip.save_current_attachments_styles!
+
+ class ::Dummy
+ has_attached_file :photo, :styles => {:small => 'x100', :large => '1000x1000>'}
+ end
+
+ expected_hash = {
+ :Dummy => {:photo => [:large, :small]}
+ }
+ assert_equal expected_hash, Paperclip.missing_attachments_styles
+ Paperclip.save_current_attachments_styles!
+ assert_equal Hash.new, Paperclip.missing_attachments_styles
+ end
# It's impossible to build styles hash without loading from database whole bunch of records
should "skip lambda-styles" do

0 comments on commit dc53432

Please sign in to comment.