Guest User

Untitled

a guest
Oct 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2.  
  3. attr_accessible :name, :email, :password, :password_confirmation
  4. has_secure_password
  5. has_many :microposts, dependent: :destroy
  6. has_many :relationships, foreign_key: "followed_id", dependent: :destroy
  7. has_many :followed_users, through: :relationships, source: "followed_id"
  8. has_many :reverse_relationships, foreign_key: "followed id",
  9. class_name: "Relationships",
  10. dependent: :destroy
  11. has_many :followers, through: :reverse_relationships, source: :follower
  12.  
  13. before_save { |user| user.email = email.downcase }
  14. before_save :create_remember_token
  15.  
  16. validates :name, presence: true, length: { maximum: 50 }
  17. VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  18. validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
  19. uniqueness: { case_sensative: false }
  20. validates :password, length: { minimum: 6 }
  21. validates :password_confirmation, presence: true
  22.  
  23. def feed
  24. Micropost.where("user_id = ?", id)
  25. end
  26.  
  27. def following?(other_user)
  28. relationships.find_by_followed_id(other_user.id)
  29. end
  30.  
  31. def follow!(other_user)
  32. relationships.create!(followed_id: other_user.id)
  33. end
  34.  
  35. def unfollow!(other_user)
  36. relationship.find_by_followed_id(other_user.id).destroy
  37. end
  38.  
  39. private
  40.  
  41. def create_remember_token
  42. self.remember_token = SecureRandom.urlsafe_base64
  43. end
  44. end
Add Comment
Please, Sign In to add comment