1.1K Views
November 13, 25
スライド概要
頑張って活動しています
Professional Ruby and Rails programmer in Tokyo.
Railsのcontrollerに CRUD以外定義するな 委員会 活動報告2 @Omotesando.rb#115
もとつねの日常 @daily_mototsune Ruby on Railsを中心に扱っているフリーランスエンジニア ドメインモデリングからAWSでの運用、スクラム運営やチームビルディ ングなどなど楽しくやらせてもらってます 普段はOmotesando.rbやGinza.rbによくいます 🍷♨✈🏎
ここまでのお話 https://www.docswell.com/s/daily_mototsune/Z8W6QE-2025-09-05-095911
ここまでのお話 - RailsはRESTfulな設計を前提としている - Fat Controllerを防ぐことができる - 無駄な認知コストを省くことができる
今回はリソースとは というお話です
リソースとは
Roy Fielding曰く The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. https://ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
Roy Fielding曰く The key abstraction of information in REST is a resource. Any RESTにおける情報の中心的な抽象概念は「リソース」である。名 前を付けられるあらゆるものはリソースになり得る。 information that can be named can be a resource: a document or 文書や画像、時間依存のサービス(例:「ロサンゼルスの今日の image, a temporal service (e.g. "today's weather in Los Angeles"), a 天気」)、他のリソースのコレクション、物理的な実体(例:人間)な collection of other resources, a non-virtual object (e.g. a person), ど、どんなものでも含まれる。 and so on. https://ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
例えば
よくある実装 class UsersController < ApplicationController # resources :users do # get :admin, on: :collection # end def admin user = User.admin end end
UserとAdminUserは 別のリソースとして 捉えることができる
よくある実装 class AdminUsersController < ApplicationController # resources :admin_user, only: :index def index user = User.admin end end
Controllerの責務が明確になり FatControllerを避けることができる
大事なポイントとして ControllerとModelは1:1出なくて良 い
Roy Fielding曰く The key abstraction of information in REST is a resource. Any RESTにおける情報の中心的な抽象概念は「リソース」である。 名 information that can be named can be a resource: a document or 前を付けられるあらゆるものはリソースになり得 image, a temporal service (e.g. "today's weather in Los Angeles"), a る。 collection of other resources, a non-virtual object (e.g. a person), 文書や画像、時間依存のサービス(例:「ロサンゼルスの今日の and so on. 天気」)、他のリソースのコレクション、物理的な実体(例:人間)な ど、どんなものでも含まれる。 https://ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
名指しできる概念=リソース - モデル コレクション ドキュメント 状態 ある時点でのビュー
例えば
ドキュメントの例 # root "welcome#show" class WelcomeController < ApplicationController def show if Current.user.rooms.any? redirect_to room_url(last_room_visited) else render end end end https://github.com/basecamp/once-campfire/blob/main/app/controllers/welcome_controller.rb
WelcomeController#show - ユーザがroomに所属していたらroomにredirect - そうでなければWelcomeページ(ドキュメント)を表示 - Modelと相対しているわけではなくドキュメントをリソースとして 扱った例 - HomeControllerで静的ページを返す的なそれ
状態の例 # resource :first_run class FirstRunsController < ApplicationController def show @user = User.new end def create user = FirstRun.create!(user_params) start_new_session_for user redirect_to root_url end end https://github.com/basecamp/once-campfire/blob/main/app/controllers/first_runs_controller.rb
FirstRunsController - アプリケーションがセットアップされる状態をリソースとした Controller - FirstRunsController#createで初回起動というリソースを作成し ている - FirstRunというモデルも存在しているので興味あればこちらも 参照して下さい
活動報告まとめ - すべてのURLはリソース操作として設計できる。 “条件集合・状態・体験”も名指し可能ならリソース。 Controllerはリソースの表現器。迷ったら名詞で切れるか?で判断。 CRUD以外を定義させないことでコードは責務が明確になりキャッシュ、権 限、テストなどの悩みポイントが大幅に減る
お悩み相談承っています 興味ある方はご連絡ください