Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Routes Generation. Susanin will Help! - Alexey Osipenko

104 views

Published on

Ruby Meditation #15
May 13, 2017
Dnipro, I Coworking Hub

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Routes Generation. Susanin will Help! - Alexey Osipenko

  1. 1. cimon.io Алексей Осипенко cimon.io «URL generation in Rails» Hello I’m Alexey
  2. 2. cimon.io Алексей Осипенко cimon.io «Susanin»
  3. 3. cimon.io Rails 5.1
  4. 4. cimon.io Rails 5.1 Direct Resolved routes &
  5. 5. cimon.io Rails 5.1 direct(:homepage) { "http://www.rubyonrails.org" }
  6. 6. cimon.io Rails 5.1 direct(:homepage) { "http://www.rubyonrails.org" } homepage_url()
  7. 7. cimon.io Rails 5.1 direct :commentable do |model|   [ model, anchor: model.dom_id ] end
  8. 8. cimon.io Rails 5.1 commentable_url(resource) direct :commentable do |model|   [ model, anchor: model.dom_id ] end
  9. 9. cimon.io What's the problem?
  10. 10. cimon.io resources :companies do resources :projects do resources :comments resources :issues do resources :comments end end end
  11. 11. cimon.io companies_projects_issues_comments_path( comment.issue.project.company, comment.issue.project, comment.issue, comment )
  12. 12. cimon.io comments_path(comment)
  13. 13. cimon.io url_for(comment)
  14. 14. cimon.io url_for([comment])
  15. 15. cimon.io everything is resource or collection
  16. 16. cimon.io everything is resource or collection or parent
  17. 17. cimon.io link_to(resource.name, [resource])
  18. 18. cimon.io Admin::UsersController Client::ProfilesController PublicApp::ProfilesController
  19. 19. cimon.io Admin::UsersController Client::ProfilesController PublicApp::ProfilesController link_to resource.name, [:admin, resource] link_to resource.name, [:client, resource] link_to resource.name, [:public, resource]
  20. 20. cimon.io Admin::UsersController Client::ProfilesController PublicApp::ProfilesController link_to resource.name, [resource] link_to resource.name, [resource] link_to resource.name, [resource]
  21. 21. cimon.io Admin::UsersController Client::ProfilesController PublicApp::ProfilesController link_to resource.name, [resource] link_to resource.name, [resource] link_to resource.name, [resource] User
  22. 22. cimon.io Admin::ProjectsController Client::MyProjectsController PublicApp::ProjectsController link_to resource.name, [resource] link_to resource.name, [resource] link_to resource.name, [resource] Project
  23. 23. cimon.io url_for [:edit, :client, resource]
  24. 24. cimon.io url_for [:edit, resource]
  25. 25. cimon.io url_for [ :edit, :client, resource.project.company, resource.project, resource ]
  26. 26. namespace :client do resource :profiles end namespace :admin do resources :users end namespace :public_app do resource :profiles end
  27. 27. module Client class ApplicationController def url_for([User] = resource) super([:client, resource]) end end end
  28. 28. module Client class ApplicationController def users_url(resource) client_users_url(resource) end end end
  29. 29. module Client class ApplicationController def url_for([..., User, ...] = resource) url_for([..., :client, resource, ...]) end end end
  30. 30. module Client class ApplicationController def url_for([..., User, ...] = resource) url_for([..., resource.company, resource, ...]) end def url_for([..., Company, ...]) url_for([:client, current_company]) end end end
  31. 31. url_for([user]) url_for([:client, current_company, user])
  32. 32. cimon.io user company, user :client, company, user
  33. 33. module Client class ApplicationController susanin({ [User] => ->(user) { [user.company, user]}, [Company] => ->(company) { [:client, company] }, }) end end
  34. 34. susanin({ [:client] => ->(_) { [:jericho] }, [User] => ->(r) { [r.company, r] }, [Project] => ->(r) { [:client, r] }, [Company] => ->(r) { [:client, r] }, })
  35. 35. susanin({ [:client] => ->(_) { [:jericho] }, [User] => ->(r) { [r.company, r] }, [Project] => ->(r) { [:client, r] }, [Company] => ->(r) { [:client, r] }, }) url_for([user]) url_for([:jericho, company, user]) jericho_company_user_url(company, user)
  36. 36. susanin({ [:client] => ->(_) { [:jericho] }, [User] => ->(r) { [r.company, r] }, [Project] => ->(r) { [:client, r] }, [Company] => ->(r) { [:client, r] }, }) url_for([:edit, user]) url_for([:edit, company, user]) url_for([:edit, :client, company, user]) url_for([:edit, :jericho, company, user]) edit_jericho_company_user_url(company, user)
  37. 37. susanin({ [User] => ->(r) { [:client, r] }, [:frontend, User] => ->(_, r) { [:public_app, r] }, }) [user] => [:client, user] [:frontend, user] => [:public_app, user] [:edit, user] => [:edit, :client, user] [:edit, :frontend, user] => [:edit, :public_app, user]
  38. 38. susanin({ [User] => ->(r) { [:client, r] }, [:frontend, User] => ->(_, r) { [:public_app, r] }, }) url_for([:frontend, user]) url_for([:client, :frontend, user]) url_for([:public_app, user])
  39. 39. [A, B, C] => (a, b, c) { [1] } [A] => (a) { [2] } [A, B] => (a, b) { [3] } [a, b, c] => [1]
  40. 40. [A, B] => ->(a, b) { 1 } [A] => ->(a) { 2 } [A, B, C] => ->(a, b, c) { 3 } [a, b, c] => [1, c]
  41. 41. [A, B] => ->(a, b) { [a] } [A] => ->(a) { 2 } [A, B, C] => ->(a, b, c) { 3 } [a, b, c] => [2, c]
  42. 42. cimon.io
  43. 43. alexey@cimon.io Спасибо за внимание telegram@aratak alexey.osipenko@fb.me
  44. 44. https://t.me/itextrapolation

×