Skip to content

Instantly share code, notes, and snippets.

@hrdwdmrbl
Created June 5, 2013 23:06
Show Gist options
  • Save hrdwdmrbl/5718023 to your computer and use it in GitHub Desktop.
Save hrdwdmrbl/5718023 to your computer and use it in GitHub Desktop.
CanCan -- HowTo cache the ability.rb file
class Ability
include CanCan::Ability
def marshal_dump
#blocks cannot be cached
@rules.reject{|rule| rule.instance_variable_get :@block }.map{|rule| Marshal.dump(rule) }
end
def marshal_load array
#blocks cannot be cached, so blocks must be re-defined
can :read, Comment do |comment|
comment.length > 100
end
@rules += array.map{|rule| Marshal.load(rule) }
end
end
class ApplicationController < ActionController::Base
if !Rails.env.development?
def current_ability
cached_ability = Rails.cache.read("#{current_user.cache_key}::ability")
if cached_ability.present?
ability = Marshal.load( cached_ability )
ability.user = current_user
else
ability = super
Rails.cache.write("#{current_user.cache_key}::ability", Marshal.dump( ability ))
end
@current_ability = ability
end
end
end
@hrdwdmrbl
Copy link
Author

HACK!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment