Created
June 1, 2009 21:43
-
-
Save twinge/121823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_text(text, field) | |
return if instance_variable_get("@#{field}_processed") | |
return if text.blank? | |
processed_text = returning(text.dup) do |processed_text| | |
text.to_s.scan(/(<img[^>]*src="[^"]*"[^>]*>)/).each do |image| image = image.first | |
original_image_content = image.dup | |
image.to_s.scan(/src="([^"]*)"/).each do |url| url = url.first | |
# First pass, import/stage images from user directory into a real asset | |
if match = url.match(/^(.*)(\/uploads\/\d*\/.*)$/) | |
filename = File.join(RAILS_ROOT, 'public', match[2].gsub(/\?.*$/,'')) | |
file = File.open(filename, 'r') | |
puts "Creating asset from #{filename}" | |
asset = assets.create :photo => file | |
file.close | |
File.unlink(filename) | |
image.gsub!(url, asset.photo.url) | |
end | |
# Second pass, add link to oversized asset version | |
next unless match = url.match(/\/\d*\/(\d*)\//) | |
if Asset.oversized.find(:first, :conditions => {:id => match[1].to_i}) | |
# no routes in models makes this semi-painful | |
image.gsub!(/^/, "<a target=\"_blank\" href=\"/assets/#{asset.to_param}?dismissable=true\">") | |
image.gsub!(/$/, "</a>") | |
end | |
end | |
processed_text.gsub!(original_image_content, image) if original_image_content != image | |
end | |
end | |
instance_variable_set("@#{field}_processed", true) | |
update_attribute field, processed_text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment