Skip to content

Commit

Permalink
Added a callback compatability shim for older versions of Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Dec 29, 2008
1 parent 1d02330 commit b8506d3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions lib/paperclip/callback_compatability.rb
Original file line number Diff line number Diff line change
@@ -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.