-
-
Save tatey/3916280 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
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