Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. devise_for :users, path: '',
  2. path_names: {
  3. sign_up: '',
  4. registration: 'signup',
  5. sign_in: 'login',
  6. password: 'password',
  7. confirmation: 'verification'
  8. },
  9. controllers: {
  10. sessions: 'users/sessions',
  11. registrations: 'users/registrations',
  12. omniauth_callbacks: 'users/omniauth_callbacks',
  13. passwords: 'users/passwords'
  14. }
  15.  
  16. resources :users, path: '', only: [:show] do
  17. member do
  18. get 'reviews', to: 'users#reviews', as: :reviews
  19. get :following, :followers
  20. post :follow, to: 'users#follow_user'
  21. post :unfollow, to: 'users#unfollow_user'
  22. end
  23. end
  24.  
  25. resources :companies, path: '', only: [:show], as: :company do
  26. resources :products, path: '', only: [:show], as: :product
  27. end
  28.  
  29. module Constraints
  30. class UserProfile
  31. def matches?(request)
  32. if request.path.include?('@')
  33. slug = request.path.delete('/@')
  34. User.where(slug: slug).exists?
  35. end
  36. end
  37. end
  38. end
  39.  
  40. def set_user
  41. @user = User.includes(:reviews).find(params[:id].delete('@'))
  42. end
  43.  
  44. # overwrite normalize function because it's stripping
  45. # out '@' when calling .parameterize
  46. def normalize_friendly_id(value)
  47. "@" + value.to_s.parameterize
  48. end
  49.  
  50. User.each do |u|
  51. u.update_attribute(:slug, nil)
  52. end
  53.  
  54. User.find_each(&:save)
  55.  
  56. constraints(Constraints::UserProfile.new) do
  57. resources :users,
  58. ....
  59. end
  60.  
  61. module Constraints
  62. class UserProfile
  63. def matches?(request)
  64. if request.path.match? //@(.*)/
  65. slug = request.path.split('/')[1]
  66. User.where(slug: slug).exists?
  67. end
  68. end
  69. end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement