Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Included tweet and user_tweets methods

  • Loading branch information...
commit bc1040ec9f9a873aa7e6269bbcd32ae11cc03df6 1 parent 9ecc843
@hugodias authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 app/models/user.rb
View
31 app/models/user.rb
@@ -2,6 +2,29 @@ class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
attr_accessor :password
+ before_save :encrypt_password
+
+ validates_confirmation_of :password
+ validates_presence_of :password, :on => :create
+ validates_presence_of :email
+ validates_uniqueness_of :email
+
+ def self.authenticate(email, password)
+ user = find_by_email(email)
+ if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
+ user
+ else
+ nil
+ end
+ end
+
+ def encrypt_password
+ if password.present?
+ self.password_salt = BCrypt::Engine.generate_salt
+ self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
+ end
+ end
+
######### Redis ###########
# follow a user
@@ -57,6 +80,14 @@ def followers_count
def following_count
$redis.scard(self.redis_key(:following))
end
+
+ def tweet!(tweet)
+ $redis.sadd(self.redis_key(:tweets), tweet)
+ end
+
+ def user_tweets
+ $redis.sinter(self.redis_key(:tweets))
+ end
# helper method to generate redis keys
def redis_key(str)
Please sign in to comment.
Something went wrong with that request. Please try again.