Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Included tweet and user_tweets methods

  • Loading branch information...
commit bc1040ec9f9a873aa7e6269bbcd32ae11cc03df6 1 parent 9ecc843
Hugo Dias authored

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. +31 0 app/models/user.rb
31 app/models/user.rb
@@ -2,6 +2,29 @@ class User < ActiveRecord::Base
2 2 attr_accessible :email, :password, :password_confirmation
3 3
4 4 attr_accessor :password
  5 + before_save :encrypt_password
  6 +
  7 + validates_confirmation_of :password
  8 + validates_presence_of :password, :on => :create
  9 + validates_presence_of :email
  10 + validates_uniqueness_of :email
  11 +
  12 + def self.authenticate(email, password)
  13 + user = find_by_email(email)
  14 + if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
  15 + user
  16 + else
  17 + nil
  18 + end
  19 + end
  20 +
  21 + def encrypt_password
  22 + if password.present?
  23 + self.password_salt = BCrypt::Engine.generate_salt
  24 + self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
  25 + end
  26 + end
  27 +
5 28
6 29 ######### Redis ###########
7 30 # follow a user
@@ -57,6 +80,14 @@ def followers_count
57 80 def following_count
58 81 $redis.scard(self.redis_key(:following))
59 82 end
  83 +
  84 + def tweet!(tweet)
  85 + $redis.sadd(self.redis_key(:tweets), tweet)
  86 + end
  87 +
  88 + def user_tweets
  89 + $redis.sinter(self.redis_key(:tweets))
  90 + end
60 91
61 92 # helper method to generate redis keys
62 93 def redis_key(str)

0 comments on commit bc1040e

Please sign in to comment.
Something went wrong with that request. Please try again.