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

Refactoring towards Component-based Rails Architectures @ RailsConf 2014

Refactoring towards Component-based Rails Architectures @ RailsConf 2014

You have a big Rails app and are feeling the pains? Stories are hard to deliver, code is hard to refactor, and your tests take a looong time? Getting you and your codebase out of this situation requires you to stop developing a "Rails application” and start refactoring towards your domain. I will discuss how and where you refactor towards a more structured and manageable application, a component-based Rails architecture.


April 23, 2014

More Decks by shagemann

Other Decks in Technology


  1. Big Rails

  2. https://github.com/josh-jacobson/Wedloom

  3. Refactoring towards Component- based Rails Architectures Stephan Hagemann ! Pivotal

    Labs Boulder, CO
  4. Component-based Rails architectures

  5. #cbra

  6. None
  7. None
  8. github.com/shageman/the_next_big_thing

  9. Teaser Email Signup Annoyance Event Counter Rails Container

  10. Proxy Partner API Rails Container Registration Internal API

  11. Availability and Booking Rails Travel Site Payment Gateway Conversion Tracking

  12. Rails TV Shows with Social Network Global Admin Show Admin

    SMS Interface Social Network Publisher Admin UI Users and Channels
  13. None
  14. WAT?

  15. Because it helps

  16. How to write #cbra? github.com/shageman/the_next_big_thing ! http://pivotallabs.com/tag/rails-application-suites/ ! confreaks.com/presenters/790-stephan-hagemann confreaks.com/presenters/784-ben-smith

    ! https://leanpub.com/cbra
  17. Big Rails

  18. None
  19. How Big? find . -iname "*.rb" -type f -exec cat

    {} \; | wc -l
  20. How Big? gist.github.com/shageman/11185993

  21. None
  22. None
  23. Growing size

  24. None
  25. None
  26. Exploding complexity Size Possible Interactions

  27. Versions of Large VS

  28. None
  29. None
  30. Size Possible Interactions Reduced (Exploding) complexity

  31. None
  32. the rich get richer and the poor get

  33. - “Ain't We Got Fun?” - Gus Kahn, Raymond B.

    Egan the rich get richer and the poor get—children!
  34. Preferential Attachment

  35. Entities (sorted by size) Size

  36. Big Open-Source Rails Apps browsercms! calagator! canvas-lms! citizenry! diaspora opencongress!

    opengovernment! portlandcrime! railscollab! rubygems.org locomotivecms! fat_free_crm! fulcrum! loc_counts! onebody skyline! snorby! spot-us! spree! teambox tracks
  37. What About Code? find . -iname "*.rb" -type f -exec

    wc -l {} \; | sort -rn
  38. What About Code? https://gist.github.com/shageman/11190909

  39. None
  40. None
  41. user.rb

  42. None
  43. ActiveRecord Associations

  44. has_many

  45. users model

  46. id name home 1 Kate Chicago 2 Pete Boulder 3

    Ian Boulder 4 Sam Chicago users table users model
  47. id name code 1 red #FF0000 2 green #00FF00 3

    blue #0000FF 4 yellow #FFFF00 colors table colors model
  48. How many has many? ack " has_many " -c |

    awk -F ":" '{print $2,$1}' | grep -v "0" | sort -rn
  49. How many has many? https://gist.github.com/shageman/11191030

  50. None
  51. None
  52. user.rb

  53. None
  54. None
  55. None
  56. we can do better

  57. None
  58. None
  59. None
  60. None
  61. SOLID

  62. Single Responsibility Principle

  63. Things that don’t belong

  64. SRP - where? Method Class Module Yes! Yes!

  65. SRP - where? Method Class Namespace Yes! Yes!

  66. Mixins

  67. SRP - where? Method Class Namespace Component Application Now you

    can! Yes! Yes! You should! … Anyone? You should!
  68. Components over SOAs 1 repo 1 test suite (and splittable!)

    1 deployment no additional versioning constraints easier refactorings between parts
  69. Within a SOA componentize your apps

  70. Refactoring towards #cbra

  71. None
  72. Teasing out App Component Extracting Functional Component

  73. https://leanpub.com/cbra

  74. Teasing out App Component

  75. Teasing out App Component 0. Got tests?

  76. Teasing out App Component 1. Find a vertical that makes

    sense on its own V C M
  77. Teasing out App Component 2. Namespace controllers, views, and models

    V C M
  78. Teasing out App Component 2. Namespace controllers, views, and models

    V C M
  79. Teasing out App Component 3. Hunt down other dependencies V

    C M
  80. Teasing out App Component 4. Move all namespaced code into

    an engine V C M
  81. Teasing out App Component 4b. rails plugin new MY_NEW_COMPONENT --full

    --mountable V C M
  82. Teasing out App Component Step 3. PROFIT! V C M

  83. Teasing out App Component PROTIP: Special Case V C M

  84. Extracting Functional Component

  85. Extracting Functional Component 0. Got tests?

  86. Extracting Functional Component 1. Find functional component

  87. Extracting Functional Component 2. bundle gem MY_NEW_COMPONENT

  88. Extracting Functional Component 3. move all files into gem (and

  89. Extracting Functional Component 4. move the other stuff the gem

  90. Extracting Functional Component 5. Require the gem from your app

  91. Extracting Functional Component 6. Add shims/ports/adapters to make the app

  92. Extracting Functional Component Step 3. PROFIT!

  93. None
  94. I will help @shageman

  95. None
  96. None
  97. Thanks! @shageman