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

Get started with Component-Based Rails @ RailsC...

shagemann
April 21, 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.

shagemann

April 21, 2015
Tweet

More Decks by shagemann

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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)
  7. 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
  8. Common Base Misc General Lib <My Company name> Side note:

    If you feel like naming your engine...
  9. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games AR Engine Can we split up WebUI?
  10. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

    UI Teams Games WebStyle AR Engine UI Engine
  11. 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
  12. Code Review Recap • No tests in WebStyle • Application

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

    need to extract until you try to extract something
  14. Sportsball Rails Engine Gem Predictor Team UI Game UI Prediction

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

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

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

    UI Prediction UI Teams Games WebStyle AR Engine UI Engine
  18. Code Review Recap • Rerouting necessary ◦ App routes (config/routes.rb)

    ◦ Engine routes (*_ui/config/routes.rb) ◦ Tests
  19. Sportsball Welcome UI Rails Engine Gem Predictor Team UI Game

    UI Prediction UI Teams Games WebStyle AR Engine UI Engine That’s it!