ããã«ã¡ã¯ï¼
Misocaéçºãã¼ã ã®ãããã(@renyamizuno_)ã§ãï¼
æè¿LEGOã©ã³ããéåããã®ã§è¡ãããã®ã§ãããå°å³ã«é ãããè¡ãã¦ãªããã³ã§ãã
æè¿ãã£ãé¢ç½ããã¨ã¯ã ã¹ããã§ã¹ã¯ã·ã§ã¨ãæ®ããªããªãã¨æã£ã¦è²ã 確èªãããã
æè¿ã¹ã¯ã·ã§ã¨ãã«ã¡ã©æ®ãããªã¼ã¨æã£ã¦ã¹ãã¬ã¼ã¸ããã£ã±ããªã®ããªã
— ãããäºããããã (@renyamizuno_) 2017å¹´4æ7æ¥
ã¨æã£ã¦è¦ãã
æ大ã25GBã«å¯¾ãã¦ã¢ããª1TBã¨ãã§ã¯ã½ç¬ã pic.twitter.com/inymgYrToH
ã¢ããªã¨ããã£ãã·ã¥ã¨ããã¹ããã®ã¹ãã¬ã¼ã¸å®¹éãããããªããããè¶ è¶ãã¦ããã¨ã§ãã
ã¯ãã
ä»åã¯æè¿ããããªãã»ã©å¤§ãããªã£ã¦ãã¾ã£ãControllerãããã«ãªãã¡ã¯ã¿ãªã³ã°ããã*1ã¨ããã®ããã£ã¨æ¸ãããã¨æãã¾ãã
ãããããªãController
ã©ããã¦ã大ãããªã£ã¡ããã¾ãããã
ãªã«ãèããªãã¨å¤§ãããªã£ã¡ããã®ã¯ç¥ã£ã¦ãã®ã§*2ã Controllerã®ã¢ã¯ã·ã§ã³ãmoduleã¨ãã¦åãåºãã¦ãControllerã«includeããããã«ãã¦ãã¾ããã
# app/controllers/hoge_contoller.rb class HogeController < ApplicationController include HogeController::FugaActions def show end # ... end # app/controllers/hoge_contoller/fuga_actions.rb class HogeController module FugaActions def fuga # ... end end end
æåã®é ã¯ãããããã£ã¡ããããããæé«ããããã¨åãã§ãã¾ããã
ã§ãããããã§æèãåæ¢ãã¦ãã¾ãæ©è½ãå¢ãããã³ã«HogeActionsãå¢ããã¨ããæè¡ããã£ã¦ãã¾ããã æèãåæ¢ãã¦ããã®ã§ãªã¼ãã«ãæ°ã«ãã追å ãã¦ããã®ã§ãããããæã¡ã³ãã¼ãã ãããã¯â¦ã²ã©ããâ¦ãã ã¨ãã声ãä¸ãã£ã¦ããã¨ãã¦è¦ç´ããã
class HogeController < ApplicationController include HogeController::FugaActions include HogeController::FugaFugaActions include HogeController::FugaFugaFugaActions include HogeController::FugaFugaFugaFugaActions # ... before_action :hoge before_action :hogehoge, only: :add_hoge # ... def show end # ... end
å®éã®ã³ã¼ããä¹ã£ããã®ã¯æµç³ã«ã¢ã¬ãªã®ã§ãã£ããã¨ãããããªãã®ã§ããã å®éã¯ãã£ã¨ã²ã©ãã¦ãªãã大å¤ã§ããâ¦ã
ãããªãã¡ã¯ã¿ãªã³ã°ã
ç¾ç¶ããã£ã¹ãã®ã¯èªèã§ããã®ã§ããããã©ãç´ããèãæ¹éãã¾ã¨ãã¦ã
ã¡ã³ãã¼ã«ã¬ãã¥ã¼ãã¦ãããæ¹éãåºãã¾ããã
大ãã3ã¹ãããã§é²ãããã¨ãªãã¾ããã
1. ããã¨ãããã¹ãã足ã
ãããããªãã§ãã¹ã足ãã¦ãªããã ãâ¦ã¨ãã話ãªã®ã§ãããç³ã訳ã
Misocaã§ã¯ãã¸ãã¹ãã¸ãã¯ãã¯ã©ã¹ã«åãåºãã¦ãã¦ã¼ã¹ã±ã¼ã¹ãªãã¸ã§ã¯ããã¨å¼ãã§ãã¾ãã Controllerã¯ã¦ã¼ã¹ã±ã¼ã¹ãªãã¸ã§ã¯ããå¼ã³åºãã¦ããã¸ãã¹ãã¸ãã¯å¦çã®çµæãåãåãã¨ãã風ã«ãã¦ãã¾ãã ã§ãã®ã¦ã¼ã¹ã±ã¼ã¹ãªãã¸ã§ã¯ãã®ã¦ããããã¹ããæããã«è¶³ãã¦ããªããã®ãå¤ãããã¾ããâ¦ã
ãªãã¡ã¯ã¿ãªã³ã°ããã¦åä½ãå£ãã¦ããã¹ãããªãã¨ããããªãã®ã§ãã¾ããã®ã¸ãã®ãã¹ããã¡ããã¨ä¸éãæ¸ããã¨ãªãã¾ããã
2.責åãåãé¢ã
ã¦ã¼ã¹ã±ã¼ã¹ãªãã¸ã§ã¯ãã«ãããããããã£ã¦ãããããããããã®ãããªæãã®ç©ãããã¾ããã å ·ä½çã«ã¯ã権éã確èªããã¦ãåé¡ãªããã°ããªã«ããä½ããã®ãããªæãã§ããã
ããã¯è¤æ°ã®äºãä¸ã¤ã®ã¯ã©ã¹ã§ãããªã£ã¦ããããè¯ããªãããã¨ãããããªæãã«ãªãã¾ããã ãªã®ã§è²¬åãåãåãã¦ãå¥ã®ã¯ã©ã¹ã«åãåºãã¾ããããã¡ãããã¹ããã¡ããã¨ããã¾ããã
3.HogeActionsãHogeControllerã«
æå¾ã«HogeActionsã¨ãªã£ã¦ããã®ãHogeControllerã«å¤æ´ãã¾ãã
æ¹éã¨ãã¦ã¯ãä¸ã®è¨äºãåèã«ãã¦ããã¾ããã
ããã¯ã¨ã¦ãä¸è¬çãªãã¿ã¼ã³ã§ããããç§ã以åã¯ãã£ã¨ãã®ãã¿ã¼ã³ã使ã£ã¦ãã¾ããã ãããä»ã¯ãããããéããã¨æãããã«ãªãã¾ãããindexã¨ããåä¸ã®ã¡ã½ããã®ã¿ãæã¤Inboxes::PendingsControllerã¨ãããæ°ããã³ã³ããã¼ã©ãæã¤ã®ã§ãã
åºæ¬çã«å½¼ãè¨ã£ã¦ããã®ã¯ãã³ã³ããã¼ã©ã¯ããã©ã«ãã®CRUDã¢ã¯ã·ã§ã³indexãshowãnewãeditãcreateãupdateãdestroyã®ã¿ã使ãã¹ãã ã¨ãããã¨ã§ãããã®ä»ã®ã¢ã¯ã·ã§ã³ã¯ã©ããå°ç¨ã®ï¼ããèªä½ã¯ããã©ã«ãã®CRUDã¢ã¯ã·ã§ã³ããæããªãï¼ã³ã³ããã¼ã©ã®ä½æã«ã¤ãªããã®ã§ãã
ç¾ç¶ã®ã³ã¼ããè¦ãã¨ã
class HogeController module FugaActions def fuga end end end
ã¨ãªã£ã¦ããã
ã®ã§ãããä¸ã®è¨äºã«å¾ã£ã¦Controllerã«æ¸ãæãã¾ãã
module Hoge class FugaController def create end end end
ãã®fuga
ã¢ã¯ã·ã§ã³ã¯Fuga
ãä½ããã®ã ã£ãã®ã§ãDHHã®æãã«å¾ã£ã¦create
ã«ãªãã¼ã ãã¾ããã
ãã¨ã¯routes.rbãä¿®æ£ããã®ã¿ã§ãã
resources :hoge, only: [:show] do member do post :fuga end end
ã¨ããã®ã
resources :hoge, only: [:show] do member do resources :fuga, only: :create, module: 'hoge' end end
ã®ããã«ä¿®æ£ãã¦å®æã§ãï¼ï¼ï¼
ã¾ã¨ã
ã¾ã ãªãã¡ã¯ã¿ãªã³ã°éä¸ãªã®ã§ã¢ã¬ã§ããâ¦
before
class HogeController < ApplicationController include HogeController::FugaActions include HogeController::FugaFugaActions include HogeController::FugaFugaFugaActions include HogeController::FugaFugaFugaFugaActions # ... before_action :hoge before_action :hogehoge, only: :add_hoge # ... def show end # ... end
after
# app/controllers/hoge_contoller.rb class HogeController < ApplicationController def show # ... end end # app/controllers/hoge/fuga_contoller.rb module Hoge class FugaController < ApplicationController # ã¢ã¯ã»ã¹ã§ããã§ããã¦ã¼ã¶ãç¢ºèª before :authenticate def create # ... end private def authenticate # ... end end end
å¥ã®ã¯ã©ã¹ã«ãªãã®ã§ã©ãã¾ã§å½±é¿ãããã®ããããããããªãã¾ãããã
ãã¨ãã°before_action
ãã å¥ãã¡ã¤ã«ã«ããã¢ã¯ã·ã§ã³ã«å½±é¿ããã¨ãããªããªã£ãã®ã§è¯ãã§ããã
ã¾ãCRUDã¢ã¯ã·ã§ã³ã ãã«ãªãã®ã§ããªã«ãããã®ããControllerã®ååã ãã¿ãã°ãããããã«ãªã£ãã®ãè¯ãã£ãã§ãã
ãã¨DHHããããã£ã¦ãã£ã¦ããã®ããã£ã¡ãå®å¿æããã£ã¦ããã£ãã§ãã
ãããã
Misocaã§ã¯ãã¥ã¼ããªãã¯ä½åã好ããªäººãåéãã¦ãã¾ãï¼