Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created March 24, 2010 20:06
Show Gist options
  • Save patrick99e99/342717 to your computer and use it in GitHub Desktop.
Save patrick99e99/342717 to your computer and use it in GitHub Desktop.
class Admin::PhotosController < Admin::TablesController
cache_sweeper :photo_sweeper, :only => [:create, :update, :destroy]
# standard methods for index, show, new, edit, update, destroy
end
FLEXI_METHODS = [
{:method => :large_thumb_access_cover, :width => 150, :height => 150 },
{:method => :table_show, :width => 75, :height => 75},
{:method => :sort_photo, :width => 50, :height => 50},
{:method => :builder_show, :width => 125, :height => 125},
{:method => :builder_thumb, :width => 25, :height => 25},
{:method => :resource_photo, :width => 125, :height => 125},
{:method => :record_show, :width => 500, :height => 500},
{:method => :record_show_thumb, :width => 30, :height => 30},
{:method => :show },
]
class PhotoSweeper < ActionController::Caching::Sweeper
observe Photo
def after_save(photo)
expire_action_cache(photo)
end
def after_destroy(photo)
expire_action_cache(photo)
end
private
def expire_action_cache(photo)
FLEXI_METHODS.each do |flexi|
# Expired fragment should look like: views/localhost:3000/photos/44/large_thumb_access_cover.jpg
expire_fragment(:controller => :photos, :action => photo.id, :id => "#{flexi[:method]}.#{photo.get_format}")
end
end
end
class Assets::PhotosController < ApplicationController
FLEXI_METHODS.each {|flexi| caches_action flexi[:method] }
FLEXI_METHODS.each do |flexi|
define_method flexi[:method] do
@photo = Photo.find(params[:id])
if flexi[:width] && flexi[:height]
@width = flexi[:width]
@height = flexi[:height]
render :template => "assets/photos/flexi/resize"
else
render :template => "assets/photos/flexi/#{flexi[:method]}"
end
end
end
@photo.operate do |photo|
photo.resize("#{@width}x#{@height}", :crop => true, :upsample => false, :stretch => false)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment