Sha256: b434a9212a852ae3f3ac788861f6ba8b27e81eb52fdf90828028f3fd158abf7c

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

module ActionController #:nodoc:
  module Caching
    class Sweeper < ActiveRecord::Observer #:nodoc:
      attr_accessor :controller

      def initialize(*args)
        super
        @controller = nil
      end

      def before(controller)
        self.controller = controller
        callback(:before) if controller.perform_caching
        true # before method from sweeper should always return true
      end

      def after(controller)
        self.controller = controller
        callback(:after) if controller.perform_caching
      end

      def around(controller)
        before(controller)
        yield
        after(controller)
      ensure
        clean_up
      end

      protected
      # gets the action cache path for the given options.
      def action_path_for(options)
        Actions::ActionCachePath.new(controller, options).path
      end

      # Retrieve instance variables set in the controller.
      def assigns(key)
        controller.instance_variable_get("@#{key}")
      end

      private
      def clean_up
        # Clean up, so that the controller can be collected after this request
        self.controller = nil
      end

      def callback(timing)
        controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
        action_callback_method_name     = "#{controller_callback_method_name}_#{controller.action_name}"

        __send__(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
        __send__(action_callback_method_name)     if respond_to?(action_callback_method_name, true)
      end

      def method_missing(method, *arguments, &block)
        return super unless @controller
        @controller.__send__(method, *arguments, &block)
      end
    end
  end
end

Version data entries

5 entries across 4 versions & 3 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/rails-observers-0.1.5/lib/rails/observers/action_controller/caching/sweeper.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/rails-observers-0.1.5/lib/rails/observers/action_controller/caching/sweeper.rb
rails-observers-0.1.5 lib/rails/observers/action_controller/caching/sweeper.rb
rails-observers-0.1.4 lib/rails/observers/action_controller/caching/sweeper.rb
rails-observers-hp-0.1.3 lib/rails/observers/action_controller/caching/sweeper.rb