Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. def self.setup_rails_for_action_cache_options
  2. ::ActionController::Caching::Actions::ActionCacheFilter.class_eval do
  3. def initialize(*actions, &block)
  4. if [].respond_to?(:extract_options!)
  5. @options = actions.extract_options!
  6. @actions = actions.inject(@options.except(:cache_path)) do |hsh, action|
  7. action.is_a?(Hash) ? hsh.merge(action) : hsh.merge(action => { :ttl => nil })
  8. end
  9. @options.slice!(:cache_path)
  10. else
  11. @actions = actions.inject({}) do |hsh, action|
  12. action.is_a?(Hash) ? hsh.merge(action) : hsh.merge(action => { :ttl => nil })
  13. end
  14. end
  15. end
  16.  
  17.  
  18. def before(controller)
  19. if !@actions.include?(controller.action_name.intern)
  20. #return #this line has been commented out
  21. end
  22.  
  23. if @options
  24. action_cache_path = ActionController::Caching::Actions::ActionCachePath.new(controller, path_options_for(controller, @options))
  25. else
  26. action_cache_path = ActionController::Caching::Actions::ActionCachePath.new(controller)
  27. end
  28.  
  29.  
  30. #if conditional = @actions[controller.action_name.intern][:if] # existing code
  31. conditional = @options[:if] # revised code
  32. if conditional = @options[:if]
  33. conditional = conditional.respond_to?(:call) ? conditional.call(controller) : controller.send(conditional)
  34. end
  35. @actions.delete(controller.action_name.intern) if conditional == false
  36.  
  37. tmp = action_cache_path.path
  38. cache = controller.read_fragment(tmp)
  39. if cache && (conditional || conditional.nil?)
  40. controller.rendered_action_cache = true
  41. if method(:set_content_type!).arity == 2
  42. set_content_type!(controller, action_cache_path.extension)
  43. else
  44. set_content_type!(action_cache_path)
  45. end
  46. controller.send(:render, :text => cache)
  47. false
  48. else
  49. controller.action_cache_path = action_cache_path if controller.respond_to? :action_cache_path
  50. end
  51. end
  52.  
  53. def after(controller)
  54. puts 'after in plugin is called'
  55. #return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache # existing code
  56. return if controller.rendered_action_cache || !caching_allowed(controller) # revised code
  57. path = controller.respond_to?(:action_cache_path) ? controller.action_cache_path.path : ActionController::Caching::Actions::ActionCachePath.path_for(controller)
  58. controller.write_fragment(path, controller.response.body, action_ttl(controller))
  59. end
  60.  
  61. end
  62. end
  63.  
  64. end
  65. end
Add Comment
Please, Sign In to add comment