Created
February 9, 2012 02:20
-
-
Save JamesHarrison/1776559 to your computer and use it in GitHub Desktop.
This file contains 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 Image | |
def self.set_scope(description, criteria, params, attribute=:id_number) | |
opts = criteria.options | |
selector = criteria.selector | |
scope_params = {} | |
scope_params[:controller] = params[:controller] | |
scope_params[:action] = params[:action] | |
scope_params[:uploader_id] = params[:uploader_id] | |
scope_params[:id] = params[:id] | |
scope_params[:ignore_spoiler_for] = params[:ignore_spoiler_for] | |
scope_params = scope_params.delete_if{|k,v|v == nil} | |
key = "scope_#{description.downcase.gsub(" ","-").gsub(/[^a-z0-9\-]/,'')}_#{Digest::SHA1.hexdigest(description+opts.to_json+selector.to_json+scope_params.to_s+attribute.to_s)}" | |
return Rails.cache.fetch(key, :expires_in=>21600, :race_condition_ttl=>360) { {scope_id: key, options: opts, selector: selector, description: description, params: scope_params, attribute: attribute} } | |
end | |
def self.get_scope(scope_id) | |
d = Rails.cache.read(scope_id) rescue nil | |
return d if d | |
return {} | |
end | |
end |
This file contains 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 ImagesController < ApplicationController | |
def index | |
@hidden_tags = Tag.get_hidden_tags(current_user) | |
@images = Image.order_by([:created_at, :desc]).not_in(tag_ids: @hidden_tags) # Make our criteria | |
@list_scope = Image.set_scope("All Images", @images, params) # Store it in our scope | |
@images = @images.page(params[:page]).per(current_user ? current_user.images_per_page : 15) | |
respond_to do |format| | |
format.html { render stream: true } | |
format.json { render json: @images } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment