Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## View
- <% form_for :user, @user do |f| %>
- <table>
- <tr>
- <td><label for="user_username">Username:</label></td>
- <td><%= f.text_field :username %></td>
- </tr>
- <tr>
- <td><label for="user_email">Email:</label></td>
- <td><%= f.text_field :email %></td>
- </tr>
- <tr>
- <td><label for="user_password">Password:</label></td>
- <td><%= f.password_field :password %></td>
- </tr>
- <tr>
- <td><label for="user_password_confirmation">Password (confirm):</label></td>
- <td><%= f.password_field :password_confirmation %></td>
- </tr>
- <tr>
- <td colspan="2"><%= f.check_box :terms_of_service, :onclick => "toggleSubmit()" %> <label for="user_terms_of_service">I have read and agree to the <%= link_to 'Terms of Service', page_url(:url_name => 'tos') %> and <%= link_to 'Privacy Policy', page_url(:url_name => 'privacy') %>.</label></td>
- </tr>
- </table>
- <%= submit_tag 'Register', :id => 'submit' %>
- <% end %>
- ## Model
- class User < ActiveRecord::Base
- has_many :pages
- belongs_to :school
- has_many :memberships, :conditions => 'memberships.status > 0',
- :dependent => :destroy
- has_many :groups, :through => :memberships
- attr_accessor :password, :old_password
- attr_accessible :username, :password, :email
- validates_length_of :username, :in => 3..12
- validates_length_of :password, :in => 4..20, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_acceptance_of :terms_of_service
- validates_uniqueness_of :username, :email
- validates_format_of :email,
- :with => /^[a-zA-Z0-9._+%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,
- :message => 'format does not appear to be valid'
- validates_format_of :username, :with => /^[a-zA-Z0-9_]+$/,
- :message => 'may only contain alphanumerical characters and "_"'
- #...
- private
- def password_required?
- !@password.nil? or self.hashed_password.blank?
- end
- #...
- end
- ## Breakpoint!
- =begin
- Executing break point at script/../config/../app/controllers/user_controller.rb:16 in `register'
- irb(#<UserController:0xb77bbbb0>):001:0> params
- => {"user"=>{"password_confirmation"=>"", "username"=>"technel", "terms_of_service"=>"1", "password"=>"", "email"=>""}, "commit"=>"Register", "action"=>"register", "controller"=>"user"}
- Executing break point at script/../config/../app/models/user.rb:90 in `validate'
- irb(#<User:0xb778a9ac>):001:0> password
- => ""
- irb(#<User:0xb778a9ac>):002:0> password_confirmation
- => nil
- irb(#<User:0xb778a9ac>):003:0> self.errors
- => #<ActiveRecord::Errors:0xb7562378 @base=#<User:0xb778a9ac @errors=#<ActiveRecord::Errors:0xb7562378 ...>, @__bp_line=90, @password="", @attributes={"status"=>0, "hashed_password"=>"a23c713da2124e9dc3c651e18e382af2f4418aabbc9be557b8d7e4203db9e3a2", "password_salt"=>"clE7EckB-608414506", "username"=>"technel", "school_id_updated_on"=>nil, "last_logged_in_at"=>nil, "school_id"=>0, "getting_started"=>true, "created_at"=>nil, "email"=>""}, @__bp_file="script/../config/../app/models/user.rb", @new_record=true>, @errors={"username"=>["has already been taken"], "password"=>["is too short (minimum is 4 characters)"], "email"=>["format does not appear to be valid"]}>
- =end
- ## Diff with previously working model
- michael@michael-c:~/sapp_bak_2-28-06/sapp/app/models$ diff ./user.rb /home/michael/sapp/app/models/user.rb
- 4,8c4,8
- < has_many :pages
- < has_many :enrollments, :conditions => 'enrollments.status > 0', :dependent => :destroy
- < has_many :schools, :through => :enrollments
- < has_many :memberships, :conditions => 'memberships.status > 0', :dependent => :destroy
- < has_many :groups, :through => :memberships
- ---
- > has_many :pages
- > belongs_to :school
- > has_many :memberships, :conditions => 'memberships.status > 0',
- > :dependent => :destroy
- > has_many :groups, :through => :memberships
- 10c10
- < attr_accessor :password, :current_password, :school
- ---
- > attr_accessor :password, :old_password, :password_confirmation
- 13,23c13,23
- < validates_length_of :username, :in => 3..12
- < validates_length_of :password, :in => 4..20
- < validates_acceptance_of :terms_of_service
- < validates_uniqueness_of :username, :email
- < validates_confirmation_of :password
- < validates_format_of :email, :with => /^[a-zA-Z0-9._+%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/, :message => 'format does not appear to be valid'
- < validates_format_of :username, :with => /^[a-zA-Z0-9_]+$/, :message => 'may only contain alphanumerical characters and "_"'
- <
- < # This method searches for the username in the database. If the account exists, it checks
- < # the password. If it matches, the user variable will be returned with the
- < # object in it. Otherwise, we set the user variable to nil and return that
- ---
- > validates_length_of :username, :in => 3..12
- > validates_length_of :password, :in => 4..20#, :if => :password_required?
- > validates_confirmation_of :password#, :if => :password_required?
- > validates_acceptance_of :terms_of_service
- > validates_uniqueness_of :username, :email
- > validates_format_of :email,
- > :with => /^[a-zA-Z0-9._+%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,
- > :message => 'format does not appear to be valid'
- > validates_format_of :username, :with => /^[a-zA-Z0-9_]+$/,
- > :message => 'may only contain alphanumerical characters and "_"'
- >
- 25c25
- < # Accept both usernames and IDs
- ---
- > # Accept both username strings and IDs
- 33c33
- < unless user.hashed_password == encrypt(password, user.password_salt)
- ---
- > unless user.hashed_password == User.encrypt(password, user.password_salt)
- 41,51c41,42
- < def school
- < self.schools[0]
- < end
- <
- < def enrollment
- < self.enrollments[0]
- < end
- <
- < # The virtual attribute "password" is saved to "hashed_password" after being
- < # run through the encryption method. A plain-text version is temporarily kept
- < # for the life of the object so that the length/confirmation can be evaluated.
- ---
- > # The virtual attribute "password" stores a plaintext password that is then hashed
- > # and stored in "hashed_password" after the plaintext is validated.
- 55c46
- < create_salt
- ---
- > self.password_salt = generate_salt
- 58a50,53
- > # Expected values for hash:
- > # old_password
- > # password
- > # password_confirmation
- 60,65c55,62
- < unless User.authenticate(self.id, hash[:current_password])
- < errors.add(:current_password, 'did not match the one on record')
- < return false
- < else
- < self.password = hash[:password]
- < self.password_confirmation = hash[:password_confirmation]
- ---
- > unless User.authenticate(self.id, hash[:old_password])
- > return errors.add(:old_password, 'was not correct')
- > end
- >
- > self.password = hash[:password]
- > self.password_confirmation = hash[:password_confirmation]
- >
- > if self.valid?
- 66a64,65
- > else
- > return false
- 70,74c69,73
- < def ban!
- < self.memberships.update_attribute(:status, -1)
- < self.enrollment.update_attribute(:status, -1)
- < self.update_attribute(:status, -1)
- < return self.save
- ---
- > def ban
- > self.school_id = nil
- > self.memberships.destroy_all
- > self.status = -1
- > self.save
- 78,81d76
- < def create_salt
- < self.password_salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp + object_id.to_s
- < end
- <
- 84a80,91
- >
- > def generate_salt
- > [Array.new(6){rand(256).chr}.join].pack("m").chomp + object_id.to_s
- > end
- >
- > def password_required?
- > !@password.nil? or self.hashed_password.blank?
- > end
- >
- > def validate
- > breakpoint
- > end
Add Comment
Please, Sign In to add comment