Skip to content

Commit

Permalink
Include a module other than top-level Paperclip to prevent namespace …
Browse files Browse the repository at this point in the history
…collisions.
  • Loading branch information
Jon Yurek committed Aug 19, 2010
1 parent 6868ff9 commit 9e6afe4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
20 changes: 11 additions & 9 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e6afe4

Please sign in to comment.