Skip to content
Browse files

dfd

  • Loading branch information...
1 parent 870be5f commit 9bc7311eaf409e420eee2391b95105d1f7990a3d @newfront committed Nov 29, 2010
Showing with 84 additions and 0 deletions.
  1. +25 −0 week_three/notes (Autosaved).rtf
  2. +19 −0 week_three/notes.rtf
  3. +40 −0 week_three/social_space/app/models/user.rb
View
25 week_three/notes (Autosaved).rtf
@@ -0,0 +1,25 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
+{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\margl1440\margr1440\vieww9000\viewh8400\viewkind0
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
+
+\f0\fs24 \cf0 require 'digest'\
+\
+Digest::SHA1.hexdigest(string)\
+\
+>> Digest::SHA1.hexdigest("Scott")\
+=> "2ad0c93dcf651c0afe5e5aa7a8778f7c3ec5769e"\
+\
+\
+protected \
+\
+def encrypted(val)\
+ return Digest::SHA1.hexdigest(val.to_s)\
+end\
+\
+----------------------------------\
+\
+Recreate User login\
+1. Add logic to User Model to encrypt the User's name (username)\
+2. Now when a user is added into the database (encrypt the username first)}
View
19 week_three/notes.rtf
@@ -0,0 +1,19 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
+{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\margl1440\margr1440\vieww9000\viewh8400\viewkind0
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
+
+\f0\fs24 \cf0 require 'digest'\
+\
+Digest::SHA1.hexdigest(string)\
+\
+>> Digest::SHA1.hexdigest("Scott")\
+=> "2ad0c93dcf651c0afe5e5aa7a8778f7c3ec5769e"\
+\
+\
+protected \
+\
+def encrypted(val)\
+ return Digest::SHA1.hexdigest(val.to_s)\
+end}
View
40 week_three/social_space/app/models/user.rb
@@ -9,4 +9,44 @@ class User < ActiveRecord::Base
# We can find all comments by a user through Review which has comments
has_many :replies, :through => :reviews, :source => :comments
+ # run some code before the user is saved
+ before_save :update_username
+
+ # run some code right after the user is saved
+ after_save :total_users
+
+ # Validations
+ validates :username, :presence => true, :uniqueness => true
+ validates :password, :presence => true
+ validates :email_address, :uniqueness => true, :presence => true,
+ :length => {:within => 5..50},
+ :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
+
+ # Example Errors
+ #=> #<User id: nil, username: nil, password: nil, created_at: nil, updated_at: nil, email_address: nil>
+ #>> u.errors
+ #=> #<OrderedHash {:email_address=>["can't be blank", "is too short (minimum is 5 characters)", "is invalid"], :password=>["can't be blank"], :username=>["can't be blank"]}>
+
+ def update_username
+ # This will be fired right before the data is saved
+ puts "USERNAME: #{self.encrypt(self.username)}"
+ end
+
+ def total_users
+ puts "Total Users: #{User.all.count}"
+ end
+
+ #>> u = User.create(:username => "scott", :password => "chores", :email_address => "scott@nf.com")
+ #USERNAME: 625600233cb3bcab32268c17610882e0fdaed295
+ #Total Users: 3
+
+ protected
+
+ # Create a way for the username to become encrypted
+
+ def encrypt(val)
+ #Usage self.encrypt(val)
+ return Digest::SHA1.hexdigest(val.to_s)
+ end
+
end

0 comments on commit 9bc7311

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