Permalink
Browse files

Added a callback compatability shim for older versions of Rails

  • Loading branch information...
1 parent 1d02330 commit b8506d367f5c379f07f1adb90d869ac1095261fd @jyurek jyurek committed Dec 29, 2008
Showing with 35 additions and 1 deletion.
  1. +1 −0 lib/paperclip.rb
  2. +1 −1 lib/paperclip/attachment.rb
  3. +33 −0 lib/paperclip/callback_compatability.rb
View
@@ -83,6 +83,7 @@ def bit_bucket
def included base #:nodoc:
base.extend ClassMethods
+ base.send(:include, Paperclip::CallbackCompatability) unless base.respond_to?(:define_callbacks)
end
def processor name
@@ -97,7 +97,7 @@ def assign uploaded_file
# and can point to an action in your app, if you need fine grained security.
# This is not recommended if you don't need the security, however, for
# performance reasons.
- # set include_updated_timestamp to false if you want to stop the attachment update time appended to the url
+ # set include_updated_timestamp to false if you want to stop the attachment update time appended to the url
def url style = default_style, include_updated_timestamp = true
url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
include_updated_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
@@ -0,0 +1,33 @@
+module Paperclip
+ # This module is intended as a compatability shim for the differences in callbacks
+ # between Rails 2.0 and Rails 2.1.
+ module CallbackCompatability
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.send(:include, InstanceMethods)
+ end
+
+ module ClassMethods
+ # The implementation of this method is taken from the Rails 1.2.6 source,
+ # from rails/activerecord/lib/active_record/callbacks.rb, line 192.
+ def define_callbacks(*args)
+ args.each do |method|
+ self.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
+ end
+
+ module InstanceMethods
+ # The callbacks in < 2.1 don't worry about the extra options or the
+ # block, so just run what we have available.
+ def run_callbacks(meth, opts = nil, &blk)
+ callback(meth)
+ end
+ end
+ end
+end

0 comments on commit b8506d3

Please sign in to comment.