Created
June 5, 2013 23:06
-
-
Save hrdwdmrbl/5718023 to your computer and use it in GitHub Desktop.
CanCan -- HowTo cache the ability.rb file
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 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 |
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HACK!!!