diff --git a/CHANGELOG.md b/CHANGELOG.md index 7786525..f43686a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add support for `ActionController::API` controllers + + *fatkodima* + + ## 1.2.2 (May 10, 2021) * Add support for Rails 6.1 diff --git a/README.md b/README.md index ad7da74..4ed62c1 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,28 @@ indicates a HTML request the fragment is stored without the extension but if an explicit `"html"` is passed in `:format` then that _is_ used for storing the fragment. +`ActionController::API` controllers +----- + +You need to manually include the `ActionController::Caching` module +and configure it. + +```ruby +class ApplicationController < ActionController::API + include ActionController::Caching + + # enable caching + self.perform_caching = true + + # configure the cache store (or just use `self.cache_store = Rails.cache`) + self.cache_store = :redis_cache_store +end + +class ListsController < ApplicationController + caches_action :index, :show +end +``` + Contributing ------------ diff --git a/lib/action_controller/caching/actions.rb b/lib/action_controller/caching/actions.rb index a45c801..fd037bb 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -159,9 +159,13 @@ def around(controller) body = controller.read_fragment(cache_path.path, @store_options) unless body - controller.action_has_layout = false unless cache_layout - yield - controller.action_has_layout = true + if controller.respond_to?(:action_has_layout=) + controller.action_has_layout = false unless cache_layout + yield + controller.action_has_layout = true + else + yield + end body = controller._save_fragment(cache_path.path, @store_options) end diff --git a/test/caching_test.rb b/test/caching_test.rb index 47780d5..626beb1 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -174,6 +174,21 @@ def render(options) end end +class APIActionCachingTestController < ActionController::API + include ActionController::Caching + + self.perform_caching = true + self.cache_store = :file_store, FILE_STORE_PATH + + caches_action :index + + def index + render json: { key: "value" } + end + + alias_method :destroy, :index +end + class CacheContent def self.to_s # Let Time spicy to assure that Time.now != Time.now @@ -208,7 +223,31 @@ def parameters; { format: nil }; end end end +module Helpers + def fragment_exist?(path) + @controller.fragment_exist?(path) + end + + def draw(&block) + @routes = ActionDispatch::Routing::RouteSet.new + @routes.draw(&block) + @controller.extend(@routes.url_helpers) + end + + if ActionPack::VERSION::STRING < "5.0" + def get(action, options = {}) + format = options.slice(:format) + params = options[:params] || {} + session = options[:session] || {} + flash = options[:flash] || {} + + super(action, params.merge(format), session, flash) + end + end +end + class ActionCacheTest < ActionController::TestCase + include Helpers tests ActionCachingTestController def setup @@ -890,28 +929,45 @@ def content_to_cache @controller.instance_variable_get(:@cache_this) end - def fragment_exist?(path) - @controller.fragment_exist?(path) - end - def read_fragment(path) @controller.read_fragment(path) end +end + +class APIActionCacheTest < ActionController::TestCase + include Helpers + tests APIActionCachingTestController - def draw(&block) - @routes = ActionDispatch::Routing::RouteSet.new - @routes.draw(&block) - @controller.extend(@routes.url_helpers) + def setup + super + + @request.host = "hostname.com" + FileUtils.mkdir_p(FILE_STORE_PATH) + end + + def teardown + super + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) + end + + def test_simple_action_cache + draw do + get "/api_action_caching_test", to: "api_action_caching_test#index" end - if ActionPack::VERSION::STRING < "5.0" - def get(action, options = {}) - format = options.slice(:format) - params = options[:params] || {} - session = options[:session] || {} - flash = options[:flash] || {} + get :index + assert_response :success + + assert fragment_exist?("hostname.com/api_action_caching_test") + end - super(action, params.merge(format), session, flash) - end + def test_simple_action_not_cached + draw do + get "/api_action_caching_test/destroy", to: "api_action_caching_test#destroy" end + + get :destroy + assert_response :success + assert !fragment_exist?("hostname.com/api_action_caching_test/destroy") + end end