Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get started with Component-Based Rails @ RailsConf 2015

Get started with Component-Based Rails @ RailsConf 2015

Component-based Rails helps you regain control over your sprawling Rails application. It helps you structure your new Rails application in a way that it will stay more manageable longer. It helps you think completely differently about writing applications - not just in Ruby and Rails!

This session will help you pass the initial hurdle of getting started with component-based Rails. While there is nothing really new, there is a lot that is just a little different. We will go over those differences so your start with component-based Rails is a smooth one.

Affa5b7b3a311fcf5f868859d586b410?s=128

shagemann

April 21, 2015
Tweet

Transcript

  1. $ git clone https://github.com/shageman/sportsball.git $ cd sportsball $ ./build.sh

  2. What brings you here?

  3. Getting started with #cbra Component-Based Rails Applications RailsConf 2015, Labs

    Workshop
  4. Stephan Hagemann @shageman shagemann@pivotal.io Pivotal Labs

  5. Why #cbra?

  6. Improved Communication Improved Collaboration Improved Creation Improved Maintenance Improved Comprehension

  7. None
  8. Communication

  9. None
  10. Collaboration

  11. None
  12. None
  13. Creation

  14. graphs on/off

  15. Maintenance

  16. reports2

  17. Comprehension

  18. None
  19. None
  20. $ git clone https://github.com/shageman/sportsball.git $ cd sportsball $ ./build.sh

  21. Sportsball - what does it do?

  22. Status Quo

  23. Sportsball App Rails Engine Gem

  24. Code Review!

  25. Code Review Recap • No app folder! • App gem

    loaded through path (Gemfile) • App is an engine (engine.rb) • App is mounted (config/routes.rb) • App routes defined in engine (app/config/routes.rb) • App defines and loads migrations (app/db/migrate/, engine. rb) • Non-published gems are funky (both Gemfiles) • Tests are at the appropriate levels and aggregated via scripts (build.sh, test.sh) • Deployment is unchanged
  26. Finding Components

  27. What should be extracted first?

  28. Sportsball App Rails Engine Gem

  29. What is the intrinsic structure of this app?

  30. 1st Refactor Extract Domain Gem

  31. Sportsball App Rails Engine Gem

  32. Sportsball App Rails Engine Gem Predictor

  33. bundle gem predictor #answer questions rm -rf predictor/.git* Move classes,

    move tests Rename module (in gem) Fix tests (in gem) Load gem (in engine) Fix tests (in engine) Check build (in main app) DIY - Extract Predictor Gem
  34. Code Review!

  35. Code Review Recap • path … do (Gemfile) • Require

    gem in (lib/app.rb) • Non-test code changes only in Module names (prediction_controller.rb) • OpenStruct App::Team and App::Game (predictor_spec.rb) • Test runner is simplified (predictor/test.sh)
  36. Lessons Learned • Have good tests around the refactor •

    Keep running those tests! • Run tests inside out (up the dependency graph) • Reduce component dependencies as much as possible ◦ Simplifies component ◦ Fewest possible reasons to change ◦ Keeps dependency graph flat
  37. Sportsball App Rails Engine Gem predictor

  38. What else is in this app?

  39. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games
  40. 2nd & 3rd Refactor Push Persistence Down

  41. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games AR Engine
  42. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games AR Engine
  43. rails plugin new --help rails plugin new teams -BGVSJT --mountable

    \ --dummy-path=spec/dummy Move classes, move tests, move migration Create table renaming migration (in teams engine) Rename module (in teams engine) Fix tests (in teams engine) Load gem (in app engine) Create testhelper (in teams engine) and use (in app engine) Fix tests (in app engine) Check build (in main app) DIY - Push Teams Persistence Down
  44. Code Review!

  45. Code Review Recap • Dummy app (teams/spec/dummy) • String renames

    of AR associations! (app/game.rb) • Teams test helper ◦ Create test helper (teams/spec/support/object_creation_methods.rb) ◦ Expose test helper (teams/lib/teams/test_helpers.rb) ◦ Use teams test helper (app/spec/spec_helper.rb)
  46. Lessons Learned • Two ways of engine-out-of-engine extraction ◦ Create

    fresh + move + rename ◦ Duplicate + delete + rename • Don’t mess with existing migrations! ◦ Move affected existing migrations ◦ Create new migration for table rename • Test helpers nice for ActiveRecord models • “App” is a terrible name to refactor away from
  47. 4th Refactor Better Naming of Component

  48. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games Layout WebUI AR Engine
  49. Common Base Misc General Lib <My Company name> Side note:

    If you feel like naming your engine...
  50. Everything Cruft Random Don’t Know Don’t Care Duh Here are

    some alternatives
  51. Everything Cruft Random DontKnow DontCare Duh Here are some alternatives

    (Proper module names)
  52. Give the most specific name you can think of refactor

    a lot continue the conversation
  53. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games AR Engine
  54. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games AR Engine Can we split up WebUI?
  55. 5th Refactor Extract Layout and Style

  56. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games WebStyle AR Engine UI Engine
  57. Copy WebUI into WebStyle Remove everything but assets/layout - don’t

    forget vendor! (in WebStyle) Remove assets (in WebUI) Fix asset loading (in WebUI) Check build (in main app) DIY - Extract WebStyle
  58. Code Review!

  59. Code Review Recap • No tests in WebStyle • Application

    layout (web_style/application.html.slim) • Layout selection (web_ui/application_controller.rb)
  60. Lessons Learned • You won’t know what components you will

    need to extract until you try to extract something
  61. 6th, 7th, 8th Refactor Separate independent UIs

  62. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games WebStyle AR Engine UI Engine
  63. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games WebStyle AR Engine UI Engine
  64. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games WebStyle AR Engine UI Engine
  65. Sportsball Welcome UI Rails Engine Gem Predictor Team UI Game

    UI Prediction UI Teams Games WebStyle AR Engine UI Engine
  66. Pick any one of the potential UI engines DIY -

    Split off a UI Engine
  67. Code Review!

  68. Code Review Recap • Rerouting necessary ◦ App routes (config/routes.rb)

    ◦ Engine routes (*_ui/config/routes.rb) ◦ Tests
  69. Lessons Learned

  70. Sportsball Welcome UI Rails Engine Gem Predictor Team UI Game

    UI Prediction UI Teams Games WebStyle AR Engine UI Engine That’s it!
  71. Now - Your App!

  72. leanpub.com/cbra/c/railsconf

  73. cbra.info