Commit 8d43e19e by Aditya Sanghi Committed by Prem Sichanugrist

fixes #588 . Attempt to log warning message when 2 models use same attachment…

fixes #588 . Attempt to log warning message when 2 models use same attachment name with default url interpolation
parent 0a77f645
...@@ -185,6 +185,19 @@ module Paperclip ...@@ -185,6 +185,19 @@ module Paperclip
raise e raise e
end end
end end
def check_for_url_clash(name,url,klass)
@names_url ||= {}
default_url = url || Attachment.default_options[:url]
if @names_url[name] && @names_url[name][:url] == default_url && @names_url[name][:class] != klass
log("Duplicate URL for #{name} with #{default_url}. This will clash with attachment defined in #{@names_url[name][:class]} class")
end
@names_url[name] = {:url => default_url, :class => klass}
end
def reset_duplicate_clash_check!
@names_url = nil
end
end end
class PaperclipError < StandardError #:nodoc: class PaperclipError < StandardError #:nodoc:
...@@ -291,6 +304,7 @@ module Paperclip ...@@ -291,6 +304,7 @@ module Paperclip
attachment_definitions[name] = {:validations => []}.merge(options) attachment_definitions[name] = {:validations => []}.merge(options)
Paperclip.classes_with_attachments << self unless Paperclip.classes_with_attachments.include?(self) Paperclip.classes_with_attachments << self unless Paperclip.classes_with_attachments.include?(self)
Paperclip.check_for_url_clash(name,attachment_definitions[name][:url],self.name)
after_save :save_attached_files after_save :save_attached_files
before_destroy :prepare_for_destroy before_destroy :prepare_for_destroy
......
...@@ -84,6 +84,7 @@ def rebuild_class options = {} ...@@ -84,6 +84,7 @@ def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip::Glue) ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, "Dummy") rescue nil Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base)) Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Paperclip.reset_duplicate_clash_check!
Dummy.class_eval do Dummy.class_eval do
include Paperclip::Glue include Paperclip::Glue
has_attached_file :avatar, options has_attached_file :avatar, options
......
...@@ -60,6 +60,24 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -60,6 +60,24 @@ class PaperclipTest < Test::Unit::TestCase
end end
end end
context "Attachments with clashing URLs should raise error" do
setup do
class Dummy2 < ActiveRecord::Base
include Paperclip::Glue
end
end
should "generate warning if attachment is redefined with the same url string" do
Paperclip.expects(:log).with("Duplicate URL for blah with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Dummy class")
Dummy.class_eval do
has_attached_file :blah
end
Dummy2.class_eval do
has_attached_file :blah
end
end
end
context "An ActiveRecord model with an 'avatar' attachment" do context "An ActiveRecord model with an 'avatar' attachment" do
setup do setup do
rebuild_model :path => "tmp/:class/omg/:style.:extension" rebuild_model :path => "tmp/:class/omg/:style.:extension"
......
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