Skip to content

Instantly share code, notes, and snippets.

@nevans
Created September 3, 2010 15:23
Show Gist options
  • Save nevans/564033 to your computer and use it in GitHub Desktop.
Save nevans/564033 to your computer and use it in GitHub Desktop.
Paperclip on rails 1.2.6
# stick this code at the bottom of your config/environment.rb
# (unless, of course, you already have something similar)
initializer_glob = File.join(RAILS_ROOT, "config/initializers/*.rb")
initializers = Dir[initializer_glob].map {|f| File.expand_path(f) }.sort
initializers.each {|f| require f }
# and stick this code into config/initializers/paperclip_on_rails_126.rb
# Paperclip only claims rails 2.1 compatibility, but it seems to mostly work
# with rails 1.2.6. Where it doesn't, we've put any necessary monkey-patches in
# here.
# If you notice any further bugs, please feel free to comment on or fork this gist.
require 'paperclip/railtie'
module Paperclip
module CallbackCompatability
module Rails12
def self.included(base)
base.extend(Defining)
base.send(:include, Running)
end
module Defining
def define_paperclip_callbacks(*args)
args.each do |callback|
define_callbacks("before_#{callback}")
define_callbacks("after_#{callback}")
end
end
def define_callbacks(method)
class_eval <<-"end_eval"
def self.#{method}(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(#{method.to_sym.inspect}, callbacks)
end
end_eval
end
end
module Running
def run_paperclip_callbacks(callback, opts = nil, &blk)
# The overall structure of this isn't ideal since after callbacks run even if
# befores return false. But this is how rails 3's callbacks work, unfortunately.
return false if callback(:"before_#{callback}") == false
returning(blk.call) do
callback(:"after_#{callback}")
end
end
end
end
end
end
module Paperclip
def self.included(base)
base.extend ClassMethods
base.send :include, Paperclip::CallbackCompatability::Rails12
end
end
Paperclip::Railtie.insert
module Rails
def self.root
RAILS_ROOT
end
def self.env
RAILS_ENV
end
end
@nevans
Copy link
Author

nevans commented Sep 3, 2010

forgot Rails.env in the first revision (it is required by Paperclip::Storage::S3)

@nevans
Copy link
Author

nevans commented Feb 25, 2011

Updated gist with recommendation on how to load it via config/initializers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment