Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This observing only seem to happend once (the logger only output once), after starting the app.
- # To have it run again I must restart the application (another save wont trigger it)
- ## environment
- config.active_record.observers = :user_observer
- ## observer
- class UserObserver < ActiveRecord::Observer
- def after_save(user)
- !! user.logger.info("*** AFTER SAVE ***")
- end
- end
- ## model
- require 'digest/sha1'
- class User < ActiveRecord::Base
- validates_presence_of :firstname, :lastname
- validates_presence_of :email_new, :password, :password_confirmation, :on => :create
- # virtual attribute
- attr_accessor :password_confirmation
- attr_protected :email
- def password
- @password
- end
- def password=(pwd)
- @password = pwd
- unless pwd.blank?
- self.password_salt = User.generate_token
- self.password_hash = User.hash_password(self.password, self.password_salt)
- end
- end
- private
- def self.hash_password(password, salt)
- Digest::SHA1.hexdigest(password + salt)
- end
- def self.generate_token
- Digest::SHA1.hexdigest(Time.now.to_s + rand.to_s);
- end
- end
- ## controller
- class AccountController < ApplicationController
- before_filter :authorize, :only => [:index, :edit]
- def signup
- @user = User.new(params[:user])
- if request.post? and @user.save
- flash[:notice] = "User #{@user.firstname} created"
- #@user = User.new REMOVE?
- end
- end
- def edit
- @user = User.find(session[:user_id])
- if request.post? and @user.update_attributes(@params[:user])
- flash[:notice] = "Ändringarna har sparats"
- end
- end
Add Comment
Please, Sign In to add comment