Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fixes #588 . Attempt to log warning message when 2 models use same at…
Browse files Browse the repository at this point in the history
…tachment name with default url interpolation
  • Loading branch information
Aditya Sanghi authored and sikachu committed Sep 23, 2011
1 parent 0a77f64 commit 8d43e19
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/paperclip.rb
Expand Up @@ -185,6 +185,19 @@ def class_for(class_name)
raise e
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

class PaperclipError < StandardError #:nodoc:
Expand Down Expand Up @@ -291,6 +304,7 @@ def has_attached_file name, options = {}

attachment_definitions[name] = {:validations => []}.merge(options)
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
before_destroy :prepare_for_destroy
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Expand Up @@ -84,6 +84,7 @@ def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Paperclip.reset_duplicate_clash_check!
Dummy.class_eval do
include Paperclip::Glue
has_attached_file :avatar, options
Expand Down
18 changes: 18 additions & 0 deletions test/paperclip_test.rb
Expand Up @@ -60,6 +60,24 @@ class ::Four; 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
setup do
rebuild_model :path => "tmp/:class/omg/:style.:extension"
Expand Down

0 comments on commit 8d43e19

Please sign in to comment.