Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # This observing only seem to happend once (the logger only output once), after starting the app.
  2. # To have it run again I must restart the application (another save wont trigger it)
  3.  
  4. ## environment
  5.  
  6. config.active_record.observers = :user_observer
  7.  
  8. ## observer
  9.  
  10. class UserObserver < ActiveRecord::Observer
  11.  
  12. def after_save(user)
  13. !! user.logger.info("*** AFTER SAVE ***")
  14. end
  15.  
  16. end
  17.  
  18. ## model
  19.  
  20. require 'digest/sha1'
  21. class User < ActiveRecord::Base
  22.  
  23. validates_presence_of :firstname, :lastname
  24. validates_presence_of :email_new, :password, :password_confirmation, :on => :create
  25.  
  26. # virtual attribute
  27. attr_accessor :password_confirmation
  28.  
  29. attr_protected :email
  30.  
  31. def password
  32. @password
  33. end
  34.  
  35. def password=(pwd)
  36. @password = pwd
  37. unless pwd.blank?
  38. self.password_salt = User.generate_token
  39. self.password_hash = User.hash_password(self.password, self.password_salt)
  40. end
  41. end
  42.  
  43.  
  44. private
  45.  
  46. def self.hash_password(password, salt)
  47. Digest::SHA1.hexdigest(password + salt)
  48. end
  49.  
  50.  
  51. def self.generate_token
  52. Digest::SHA1.hexdigest(Time.now.to_s + rand.to_s);
  53. end
  54.  
  55. end
  56.  
  57. ## controller
  58.  
  59. class AccountController < ApplicationController
  60.  
  61. before_filter :authorize, :only => [:index, :edit]
  62.  
  63. def signup
  64. @user = User.new(params[:user])
  65. if request.post? and @user.save
  66. flash[:notice] = "User #{@user.firstname} created"
  67. #@user = User.new REMOVE?
  68. end
  69. end
  70.  
  71. def edit
  72. @user = User.find(session[:user_id])
  73. if request.post? and @user.update_attributes(@params[:user])
  74. flash[:notice] = "Ändringarna har sparats"
  75. end
  76. end
Add Comment
Please, Sign In to add comment