Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ActionController::API controllers #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Add support for `ActionController::API` controllers

*fatkodima*


## 1.2.2 (May 10, 2021)

* Add support for Rails 6.1
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------

Expand Down
10 changes: 7 additions & 3 deletions lib/action_controller/caching/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
88 changes: 72 additions & 16 deletions test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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