Skip to content

Instantly share code, notes, and snippets.

@tatey
Forked from coop/option_a.rb
Created October 19, 2012 04:46
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 tatey/3916280 to your computer and use it in GitHub Desktop.
Save tatey/3916280 to your computer and use it in GitHub Desktop.
class Page < ActiveRecord::Base
has_attached_file :image, sizes: {small: '10x10', large: '100x100'}
has_attached_file :uploaded_image
def safe_image_url
if image
image_url
else
'spinner.gif'
end
end
end
module AttachmentResizer
def self.dispatch page
DelayedJob.enqueue SomeJob.new(page.class, page.id) if page.uploaded_image?
end
def self.resize page
page.image = open(page.uploaded_image_url)
page.uploaded_image = nil
page.save
end
end
class SomeJob
def initialize klass, id
@klass, @id = klass, id
end
def perform
AttachmentResizer.resize @klass.find(@id)
end
end
class PagesController < ApplicationController
def create
@page = Page.new params[:page]
@page.save
AttachmentResizer.dispatch(@page)
redirect_to page_path(@page)
end
end
# app/views/page.erb
<%= image_tag page.safe_image_url %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment