Pro Yearly is on sale from $80 to $50! »

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

D79fc498d7a5b2ce12180890247476f0?s=47 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.

D79fc498d7a5b2ce12180890247476f0?s=128

Jon Rowe

December 11, 2012
Tweet

Transcript

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

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

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

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

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

  6. HEXAWAT?

  7. HEXAGONAL Hexagonal Architecture

  8. HEXAGONAL Hexagonal Architecture Describes the structure of an app

  9. HISTORY

  10. HISTORY Originally by Alistair Cockburn

  11. Application

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

  13. HEXAGONAL

  14. HEXAGONAL Decouple the application

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

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

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

    Provide adaptors for technology Provide adaptors for I/O
  18. HEXAGONAL RAILS

  19. HEXAGONAL RAILS Describes structure of application

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

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

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

    the application ... Rails is the HTTP Adaptor Describes building apps *with* Rails
  23. Application

  24. RAILS DB LOGGING CMD LINE Application

  25. WHY?

  26. WHY? Separate responsibilities

  27. WHY? Separate responsibilities Push dependencies inwards

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

  29. WHY?

  30. WHY? Modular code

  31. WHY? Modular code Isolated / Testable code

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

  33. HOW?

  34. HOW? Dependency Injection / Inversion

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

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

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

    Objects Define an API
  38. BUT WAIT... WASN’T THIS TALK ABOUT THE ROUTER?

  39. Pushing Inwards

  40. Pushing Inwards Dependency Inversion

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

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

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

    Take decisions early Tell don’t ask
  44. Rails

  45. Rails The router is top of the Rails stack

  46. Rails The router is top of the Rails stack Push

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

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

    inwards from there Without an addition to the framework?
  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
  50. resources :listing class ListingController before_filter :ensure_admin!, except: :show #snip... end

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

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

    do resources :listing, only: :show end Router
  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
  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
  55. Summary

  56. Summary Push things inwards

  57. Summary Push things inwards Isolate your business logic

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

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

    to your application Have fun!
  60. THANKS @jonrowe (Twitter, GitHub, etc) http://jonrowe.co.uk P.S. I’m available...