Commit dc53432b by Steve Madsen Committed by Jon Yurek

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".
parent a83de65c
...@@ -70,7 +70,7 @@ module Paperclip ...@@ -70,7 +70,7 @@ module Paperclip
Hash.new.tap do |missing_styles| Hash.new.tap do |missing_styles|
current_styles.each do |klass, attachment_definitions| current_styles.each do |klass, attachment_definitions|
attachment_definitions.each do |attachment_name, styles| attachment_definitions.each do |attachment_name, styles|
registered = registered_styles[klass][attachment_name] rescue [] registered = registered_styles[klass][attachment_name] || [] rescue []
missed = styles - registered missed = styles - registered
if missed.present? if missed.present?
klass_sym = klass.to_s.to_sym klass_sym = klass.to_s.to_sym
......
...@@ -69,6 +69,22 @@ class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase ...@@ -69,6 +69,22 @@ class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
assert_equal Hash.new, Paperclip.missing_attachments_styles assert_equal Hash.new, Paperclip.missing_attachments_styles
end 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 # It's impossible to build styles hash without loading from database whole bunch of records
should "skip lambda-styles" do should "skip lambda-styles" do
rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} } rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment