jsonapi-suite / jsonapi_compliable

MIT License
20 stars 35 forks source link

Prevent non-resource backed controller from throwing no method error when mix-in is inherited from ApplicationController #46

Closed daniel-sullivan closed 7 years ago

daniel-sullivan commented 7 years ago

Change to allow 'include JsonapiSuite::ControllerMixin' to be called directly from ApplicationController without needing to specify 'jsonapi resource: < name >' in every descendant controller.

Fix for https://github.com/jsonapi-suite/jsonapi_compliable/issues/45

richmolj commented 7 years ago

Hey @daniel-sullivan, thanks for this PR! I'm not sure if it's the way I would go, though. If you have controllers without resources, these are likely not JSONAPI endpoints. If they are not JSONAPI endpoints, I would split my controllers to make this explicit:

class ApplicationController < ActionController::API
end

class JSONAPIController < ApplicationController
  include JsonapiSuite::ControllerMixin
end

class EmployeesController < JSONAPIController
  jsonapi resource: EmployeeResource
end

class PostsController < ApplicationController
  # no jsonapi here
end

This treats the situation more as a ruby issue than an issue specific to JSONAPI Suite. Would that work for you?

daniel-sullivan commented 7 years ago

I agree and I have moved to doing it that way. The initial misconception came from when I was originally following the tutorial (this has since changed and I can't find the reference anywhere), the recommendation was to do the definition in application.rb. In my case where this is an existing project, that generated all sorts of errors on controllers that weren't being used for the API functions.

Now that those references have gone (if they even existed at all - it may have been a fever dream, I've been working a lot of overtime recently...), there's really nothing to get confused about so I'm going to close this and #46