Skip to content

Instantly share code, notes, and snippets.

@tooky
Created March 17, 2009 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooky/80472 to your computer and use it in GitHub Desktop.
Save tooky/80472 to your computer and use it in GitHub Desktop.
# Monkeypatch paperclip 2.1.2 to work with merb
module Paperclip
class Attachment
def assign uploaded_file
%w(file_name).each do |field|
unless @instance.class.column_names.include?("#{name}_#{field}")
raise PaperclipError.new("#{self} model does not have required column '#{name}_#{field}'")
end
end
if uploaded_file.is_a?(Paperclip::Attachment)
uploaded_file = uploaded_file.to_file(:original)
end
return nil unless valid_assignment?(uploaded_file)
logger.info("[paperclip] Assigning #{uploaded_file.inspect} to #{name}")
queue_existing_for_delete
@errors = []
@validation_errors = nil
return nil if uploaded_file.nil?
logger.info("[paperclip] Writing attributes for #{name}")
if (uploaded_file.respond_to?(:original_filename) && uploaded_file.respond_to?(:content_type))
@queued_for_write[:original] = uploaded_file.to_tempfile
@instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip.gsub /[^\w\d\.\-]+/, '_'
@instance[:"#{@name}_content_type"] = uploaded_file.content_type.strip
@instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
else
@queued_for_write[:original] = uploaded_file[:tempfile]
@instance[:"#{@name}_file_name"] = uploaded_file[:filename].strip.gsub /[^\w\d\.\-]+/, '_'
@instance[:"#{@name}_content_type"] = uploaded_file[:content_type].strip
@instance[:"#{@name}_file_size"] = uploaded_file[:size].to_i
end
@instance[:"#{@name}_updated_at"] = Time.now
@dirty = true
post_process
ensure
validate
end
def geometry style = default_style
@geometries ||= {}
@geometries[style] ||= Geometry.from_file path(style)
end
private
def valid_assignment? file #:nodoc:
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type)) || (file.key?(:filename) && file.key?(:content_type))
end
def logger
instance.logger
end
end
end
Paperclip::Attachment.interpolations[:rails_root] = lambda{|attachment,style| Merb.root } if defined? Paperclip::Attachment.interpolations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment