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

Abusing The Router (For Hexagonal Fun) - RoRoSyd December 12

Jon Rowe
December 11, 2012

Abusing The Router (For Hexagonal Fun) - RoRoSyd December 12

A short overview of the Hexagonal Architecture style with some fun usages of the router to help us out.

Jon Rowe

December 11, 2012
Tweet

More Decks by Jon Rowe

Other Decks in Programming

Transcript

  1. ABUSING
    THE ROUTER
    (FOR HEXAGONAL FUN)
    JON ROWE

    View Slide

  2. World.new.hello!
    Hi everybody. I’m Jon.

    View Slide

  3. World.new.hello!
    Hi everybody. I’m Jon.
    Freelance Rubyist.

    View Slide

  4. World.new.hello!
    Hi everybody. I’m Jon.
    Freelance Rubyist.
    Why is the rum gone?

    View Slide

  5. ABUSING
    THE ROUTER
    (FOR HEXAGONAL FUN)
    JON ROWE

    View Slide

  6. HEXAWAT?

    View Slide

  7. HEXAGONAL
    Hexagonal Architecture

    View Slide

  8. HEXAGONAL
    Hexagonal Architecture
    Describes the structure of an app

    View Slide

  9. HISTORY

    View Slide

  10. HISTORY
    Originally by Alistair Cockburn

    View Slide

  11. Application

    View Slide

  12. HISTORY
    Originally by Alistair Cockburn
    Also called “Ports and Adaptors”

    View Slide

  13. HEXAGONAL

    View Slide

  14. HEXAGONAL
    Decouple the application

    View Slide

  15. HEXAGONAL
    Decouple the application
    Make the business logic the focus

    View Slide

  16. HEXAGONAL
    Decouple the application
    Make the business logic the focus
    Provide adaptors for technology

    View Slide

  17. HEXAGONAL
    Decouple the application
    Make the business logic the focus
    Provide adaptors for technology
    Provide adaptors for I/O

    View Slide

  18. HEXAGONAL RAILS

    View Slide

  19. HEXAGONAL RAILS
    Describes structure of application

    View Slide

  20. HEXAGONAL RAILS
    Describes structure of application
    but... Rails is not the application

    View Slide

  21. HEXAGONAL RAILS
    Describes structure of application
    but... Rails is not the application
    ... Rails is the HTTP Adaptor

    View Slide

  22. HEXAGONAL RAILS
    Describes structure of application
    but... Rails is not the application
    ... Rails is the HTTP Adaptor
    Describes building apps *with* Rails

    View Slide

  23. Application

    View Slide

  24. RAILS
    DB
    LOGGING
    CMD LINE
    Application

    View Slide

  25. WHY?

    View Slide

  26. WHY?
    Separate responsibilities

    View Slide

  27. WHY?
    Separate responsibilities
    Push dependencies inwards

    View Slide

  28. WHY?
    Separate responsibilities
    Push dependencies inwards
    Make things more explicit

    View Slide

  29. WHY?

    View Slide

  30. WHY?
    Modular code

    View Slide

  31. WHY?
    Modular code
    Isolated / Testable code

    View Slide

  32. WHY?
    Modular code
    Isolated / Testable code
    Cleaner code

    View Slide

  33. HOW?

    View Slide

  34. HOW?
    Dependency Injection / Inversion

    View Slide

  35. HOW?
    Dependency Injection / Inversion
    Pure Ruby service classes

    View Slide

  36. HOW?
    Dependency Injection / Inversion
    Pure Ruby service classes
    Response Objects

    View Slide

  37. HOW?
    Dependency Injection / Inversion
    Pure Ruby service classes
    Response Objects
    Define an API

    View Slide

  38. BUT WAIT...
    WASN’T THIS TALK ABOUT THE ROUTER?

    View Slide

  39. Pushing Inwards

    View Slide

  40. Pushing Inwards
    Dependency Inversion

    View Slide

  41. Pushing Inwards
    Dependency Inversion
    Push things down into the app

    View Slide

  42. Pushing Inwards
    Dependency Inversion
    Push things down into the app
    Take decisions early

    View Slide

  43. Pushing Inwards
    Dependency Inversion
    Push things down into the app
    Take decisions early
    Tell don’t ask

    View Slide

  44. Rails

    View Slide

  45. Rails
    The router is top of the Rails stack

    View Slide

  46. Rails
    The router is top of the Rails stack
    Push inwards from there

    View Slide

  47. Rails
    The router is top of the Rails stack
    Push inwards from there
    Without an addition to the framework?

    View Slide

  48. Rails
    The router is top of the Rails stack
    Push inwards from there
    Without an addition to the framework?

    View Slide

  49. resources :one, controller: :generic, type: One
    resources :two, controller: :generic, type: Two
    controller GenericController
    def show
    params[:type].find params[:id]
    end
    #snip...
    end
    Router

    View Slide

  50. resources :listing
    class ListingController
    before_filter :ensure_admin!, except: :show
    #snip...
    end
    Router

    View Slide

  51. namespace :admin
    resources :listing, except: :show
    end
    resources :listing, only: :show
    Router

    View Slide

  52. constraint IsAdmin do
    resources :listing, except: :show
    end
    constraint IsUser do
    resources :listing, only: :show
    end
    Router

    View Slide

  53. Constraints
    class IsAdmin
    def self.matches? request
    request.env[‘warden.env’].user!.is_admin?
    end
    end
    class IsUser
    def self.matches? request
    request.env[‘warden.env’].user!
    end
    end

    View Slide

  54. Constraints
    constraint AllowedAccess.new(:beta) do
    resource :shiney_thing
    end
    class AllowedAccess < Struct.new(:feature)
    attr_reader :feature
    def matches? request
    unless Features.new(user).allow? feature
    throw :warden, Warden::Redirect...
    end
    end
    end

    View Slide

  55. Summary

    View Slide

  56. Summary
    Push things inwards

    View Slide

  57. Summary
    Push things inwards
    Isolate your business logic

    View Slide

  58. Summary
    Push things inwards
    Isolate your business logic
    Bend Rails to your application

    View Slide

  59. Summary
    Push things inwards
    Isolate your business logic
    Bend Rails to your application
    Have fun!

    View Slide

  60. THANKS
    @jonrowe (Twitter, GitHub, etc)
    http://jonrowe.co.uk
    P.S. I’m available...

    View Slide