結論
から言うと、
gem 'rails-api' # 投稿時点で 0.4.0
gem 'garage', github: 'cookpad/garage' # 投稿時点で v1.5.0(tag)
gem 'responders', '~> 2.0', require: false
class ActionController::API
include ActionController::MimeResponds
include ActionController::ImplicitRender
end
require 'responders'
こういう感じになる。(色々ツッコミ所はあるのは認めます)
なんでこうしたのか?
garageで使われているrespondersでは、controllerでrespond_to
メソッドを使用しています。
rails-apiでこのメソッドを使う為にはドキュメントにもあるように、
ActionController::MimeResponds
, ActionController::ImplicitRender
が必要になります。
ActionController::MimeResponds (and ActionController::ImplicitRender for Rails 4): Support for content negotiation (respond_to, respond_with).
そんな訳で、
class ApplicationController < ActionController::API
include ActionController::MimeResponds
include ActionController::ImplicitRender
include Garage::ControllerHelper
end
と書きたくなるのだが、読み込み順の問題でこれでは思ったような結果にはなりませんでした。
全部追った訳ではないのでただの予想ですが、
少なくともconfig/initializers/garage.rb
などでGarage
の設定をする前に必要な各モジュールをinclude
しておけば問題なさそうです。
なので、config/initializers/_rails_api.rb
などと_
から始まる名前にして早く読み込まれるようにしました。
別の話ですが、garageのgemspecにhamlとかsassとか入ってるの、なんだこれ?って思ってたんですが中に便利そうなRails Engineが入ってる為でした。
今回は用途が用途(API onlyなのでjsもcssもhtmlも不要…)なだけに、
ここを切り出して別途gemにしようと考えました。
が、garage自体が良い感じにrubygemsに登録されてないので、
切り出したgemのdependencyにgarageを書けない→テストしづらいっていう悲しみに…