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

Use this in an API Controller? #85

Open
walterdavis opened this issue Feb 23, 2024 · 1 comment
Open

Use this in an API Controller? #85

walterdavis opened this issue Feb 23, 2024 · 1 comment

Comments

@walterdavis
Copy link

walterdavis commented Feb 23, 2024

I'm not 100% sure this is intended use, but I just bodged my way into adding this to a API controller, and it appears to work. The error that I hit when I first tried was:

A NoMethodError occurred in titles#show:

 undefined method `cache_configured?' for #<Api::V1::TitlesController:0x0000000003e7d8>

     unless cache_configured? && cacheable_req
            ^^^^^^^^^^^^^^^^^
Did you mean?  cache_version_data
 app/controllers/api/v1/titles_controller.rb:9:in `show'

(Rails 7.0.8.1)

And that's confusing, unless this line doesn't do what I imagine it does.

Which led me down the way to a bit of example code in one of the tests, which seems to also work in my API controller. I wonder if this leaves anything out that the other controllers get? All the other (non-API) routes seem to work just fine.

module Api
  module V1
    class TitlesController < ActionController::API
      helper ApplicationHelper, PeopleHelper
      include ResponseBank::Controller # <--- this

      def show
        response_cache do
          @title = Title.published.includes(:headings, :roles, :collections).friendly.find(params[:id])
        end
      end

      private

      # and this method
      def cache_configured?
        true
      end
    end
  end
end

I'm going to try this in production for a couple of minutes, but are there any alarm bells going off for you?

@walterdavis
Copy link
Author

Follow-up: this seems to work just great. Might be worth a documentation note for anyone else who tries this on an API controller. Please let me know if you'd like a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant