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

Architecture Over Framework: Rethink Your App Structure

Architecture Over Framework: Rethink Your App Structure

With the kind of maturity Rails has gained over the past few years, saying our business logic resides in app/ makes us look like we are from 2009. However, that is also a truth we are bound to, as Rails exhibits a very rigid app structure. Every app has models, views, concerns, mailers etc. However, they don't have to always be in app/views, they could be in src/authentication/templates for what it's worth.

In this talk, we'll take a typical Rails app and completely mess it up by restructuring the content. We'll fragment it in a way that is relevant to the application context. No more app/models and app/controllers and such. What we will get as a result, is a big screaming pile of fail!

But, this is a start of a new red-green-refactor cycle. We will try to make it work by changing one thing at a time, looking at one error at a time. And in the end we'll make it all work, or give up hopelessly, but with a far more understanding of the magic happening under the hood.

Swanand Pagnis

July 19, 2014
Tweet

More Decks by Swanand Pagnis

Other Decks in Programming

Transcript

  1. Architecture
    Over
    Framework
    Rethink Your App Structure

    View full-size slide

  2. Tweet@_swanand
    GitHub@swanandp
    StackOverflow@18678
    Build { Simplero }
    [email protected]
    Ruby, Clojure, Lisp, Rails,
    Android, Emacs, TextMate,
    RubyMine, Minitest, MySQL,
    Zsh, Curl, Gmail, Hadoop,
    Mavericks, Solarized,
    Retina-MBP, Nexus 5
    Oscar Wilde, Robert Jordan, J K Rowling, Quentin
    Tarantino, Chris Nolan, Leonardo DiCaprio, Charlize
    Theron, Metallica, Dream Theatre, Pink Floyd

    View full-size slide

  3. !
    if magic_code
    puts "I am in IF block"
    else
    puts "In ELSE block I am"
    end
    !
    => I am in IF block
    => In ELSE block I am
    how?

    View full-size slide

  4. !
    if fork
    puts "I am in IF block"
    else
    puts "In ELSE block I am"
    end
    !
    => I am in IF block
    => In ELSE block I am
    how?

    View full-size slide

  5. Looking Ahead
    Why do this ?
    Case Study: Discourse

    View full-size slide

  6. Looking Ahead
    Why do this ?
    Case Study: Discourse

    View full-size slide

  7. Why do this ?
    Clear Intent
    Better Understanding

    View full-size slide

  8. Why do this ?
    Clear Intent
    Better Understanding

    View full-size slide

  9. Architecture
    Elements of a system
    and their relationship
    with each other

    View full-size slide

  10. Architecture
    Designed such that
    intent is apparent and
    palpable

    View full-size slide

  11. Framework
    An abstraction you
    build upon, to provide
    app specific software

    View full-size slide

  12. This picture screams RAILS!

    View full-size slide

  13. –Robert Martin
    “This is good
    for DHH; but
    not So good for
    you.”
    http://www.confreaks.com/videos/759-rubymidwest2011-keynote-architecture-the-lost-years

    View full-size slide

  14. Why do this ?
    Clear Intent
    Better Understanding

    View full-size slide

  15. Debugging
    bad code:
    Frustration

    View full-size slide

  16. Debugging
    good code:
    Enlightenment

    View full-size slide

  17. Debugging
    good code:
    Learning

    View full-size slide

  18. Looking Ahead
    Why do this?
    Case Study: Discourse

    View full-size slide

  19. That’s not a lot, but
    if you are new to
    the project, then …

    View full-size slide

  20. http://media0.giphy.com/media/KE9cblgPK6EPS/200_s.gif

    View full-size slide

  21. File lookups are
    fixed by managing
    load path

    View full-size slide

  22. Const lookups are
    fixed by managing
    auto-load path

    View full-size slide

  23. config/application.rb

    View full-size slide

  24. $ rails console

    View full-size slide

  25. $ rails console

    View full-size slide

  26. http://38.media.tumblr.com/tumblr_maeu8e5wuj1rxmai6o1_500.gif

    View full-size slide

  27. First real limitation:
    namespace based
    look ups

    View full-size slide

  28. application_controller

    View full-size slide

  29. Second limitation:
    unfriendly helper
    customisation

    View full-size slide

  30. Second limitation:
    unfriendly
    customisation

    View full-size slide

  31. action_controller/helpers

    View full-size slide

  32. rails/railtie

    View full-size slide

  33. config/helper_railtie

    View full-size slide

  34. Looking Back
    File structure (Load Path)
    Const lookups (Auto Loads)
    View Paths (Namespacing)
    Helper Paths (Railties)

    View full-size slide

  35. Rearrange tests
    Rearrange assets
    Custom Rake tasks
    Possibilities

    View full-size slide

  36. Muddle with Metal
    Build your Rackware
    Examine pre-forking
    Possibilities

    View full-size slide