Skip to content
Browse files

Include a module other than top-level Paperclip to prevent namespace …

…collisions.
  • Loading branch information...
1 parent 6868ff9 commit 9e6afe4ff5b6b07bb1d50a4cb4bdd85feb1c9bbf @jyurek jyurek committed Aug 19, 2010
Showing with 22 additions and 14 deletions.
  1. +11 −9 lib/paperclip.rb
  2. +1 −1 lib/paperclip/railtie.rb
  3. +4 −4 test/helper.rb
  4. +6 −0 test/paperclip_test.rb
View
20 lib/paperclip.rb
@@ -103,15 +103,6 @@ def run cmd, *params
CommandLine.new(cmd, *params).run
end
- def included base #:nodoc:
- base.extend ClassMethods
- if base.respond_to?("set_callback")
- base.send :include, Paperclip::CallbackCompatability::Rails3
- else
- base.send :include, Paperclip::CallbackCompatability::Rails21
- end
- end
-
def processor name #:nodoc:
name = name.to_s.camelize
processor = Paperclip.const_get(name)
@@ -159,6 +150,17 @@ class NotIdentifiedByImageMagickError < PaperclipError #:nodoc:
class InfiniteInterpolationError < PaperclipError #:nodoc:
end
+ module Glue
+ def self.included base #:nodoc:
+ base.extend ClassMethods
+ if base.respond_to?("set_callback")
+ base.send :include, Paperclip::CallbackCompatability::Rails3
+ else
+ base.send :include, Paperclip::CallbackCompatability::Rails21
+ end
+ end
+ end
+
module ClassMethods
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
# is typically a file stored somewhere on the filesystem and has been uploaded by a user.
View
2 lib/paperclip/railtie.rb
@@ -17,7 +17,7 @@ class Railtie < Rails::Railtie
class Railtie
def self.insert
- ActiveRecord::Base.send(:include, Paperclip)
+ ActiveRecord::Base.send(:include, Paperclip::Glue)
File.send(:include, Paperclip::Upfile)
end
end
View
8 test/helper.rb
@@ -61,10 +61,10 @@ def setup
ActiveRecord::Base.establish_connection(config['test'])
def reset_class class_name
- ActiveRecord::Base.send(:include, Paperclip)
+ ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
- klass.class_eval{ include Paperclip }
+ klass.class_eval{ include Paperclip::Glue }
klass
end
@@ -90,11 +90,11 @@ def rebuild_model options = {}
end
def rebuild_class options = {}
- ActiveRecord::Base.send(:include, Paperclip)
+ ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Dummy.class_eval do
- include Paperclip
+ include Paperclip::Glue
has_attached_file :avatar, options
end
end
View
6 test/paperclip_test.rb
@@ -172,6 +172,12 @@ class ::SubDummy < Dummy; end
end
end
+ should "not have Attachment in the ActiveRecord::Base namespace" do
+ assert_raises(NameError) do
+ ActiveRecord::Base::Attachment
+ end
+ end
+
def self.should_validate validation, options, valid_file, invalid_file
context "with #{validation} validation and #{options.inspect} options" do
setup do

1 comment on commit 9e6afe4

@sikachu
thoughtbot, inc. member

Glad to see this commit! I always had to do has_many :attachments, :class_name => "::Attachment" because it tried to create instance of Paperclip's Attachment class when loading the association. :)

Please sign in to comment.
Something went wrong with that request. Please try again.