$30 off During Our Annual Pro Sale. View Details »

Off the Rails: Patterns for fixing fat models, out-of-controllers, and other Rails problems

Off the Rails: Patterns for fixing fat models, out-of-controllers, and other Rails problems

Alexander Clark

April 23, 2019
Tweet

More Decks by Alexander Clark

Other Decks in Technology

Transcript

  1. Off the Rails
    Patterns for fixing fat models, out-of-controllers, and
    other Rails problems

    View Slide

  2. What I’m about to tell
    you is wrong.

    View Slide

  3. YMMV

    View Slide

  4. MVC

    View Slide

  5. View
    Controller
    Model

    View Slide

  6. 3 Tier Architecture

    View Slide

  7. Business Logic
    Presentation
    Data

    View Slide

  8. Business Logic
    Presentation
    Data

    View Slide

  9. Business Logic
    Presentation
    Data
    View
    Controller
    Model

    View Slide

  10. Business Logic
    Presentation
    Data
    View Controller
    Model

    View Slide

  11. View Controller
    Model
    Business Logic
    Presentation
    Data

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Controller
    Model

    View Slide

  16. Model
    Service
    View Controller

    View Slide

  17. Business Logic
    Presentation
    Data

    View Slide

  18. Business Logic
    Presentation
    Data

    View Slide

  19. Business Logic
    Presentation
    Data
    Service

    View Slide

  20. View Slide

  21. View Slide

  22. Controller Rule of Thumb
    A Controller May Contain:
    • Public REST methods

    • Private before_action setters

    • Private strong params method

    View Slide

  23. Models

    View Slide

  24. Business Logic
    Presentation
    Data
    View Controller
    Model

    View Slide

  25. Business Logic
    Presentation
    Data
    View Controller
    Model

    View Slide

  26. Business Logic
    Presentation
    Data
    View Controller
    Model
    Repository

    View Slide

  27. View Slide

  28. Business Logic
    Presentation
    Data
    View Controller
    Validator
    Model

    View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. –Sandi Metz
    “duplication is far cheaper than the wrong
    abstraction”

    View Slide

  34. YAGNI

    View Slide

  35. Bonus Tip

    View Slide

  36. View Slide

  37. View Slide

  38. Thanks for Listening
    Alexander Clark
    atheclark
    alexander-clark.com
    alexander-clark

    View Slide

  39. Further reading
    • https://codeclimate.com/blog/7-ways-to-decompose-fat-
    activerecord-models/

    • Patterns of Enterprise Application Architecture (Fowler)

    • Domain Driven Design (Evans)

    • All the Little Things (Metz) https://www.youtube.com/
    watch?v=8bZh5LMaSmE

    • Ruby Object Mapper https://rom-rb.org/

    View Slide

  40. Questions?

    View Slide