Updated Rake tasks and test, spec to reflect change in how Merb environments
[merb_mart.git] / app / models / user.rb
blob7b908ba1736c2ddcf2a05528082dee3b58d38e0c
1 require 'digest/sha1'
2 begin
3   require File.join(File.dirname(__FILE__), '..', '..', "lib", "authenticated_system", "authenticated_dependencies")
4 rescue 
5   nil
6 end
7 class User
8   
9   include DataMapper::Persistable
10   include AuthenticatedSystem::Model
11   
12   attr_accessor :password, :password_confirmation
13   
14   property :login,                      :string
15   property :email,                      :string
16   property :crypted_password,           :string
17   property :salt,                       :string
18   property :activation_code,            :string
19   property :activated_at,               :datetime
20   property :remember_token_expires_at,  :datetime
21   property :remember_token,             :string
22   property :created_at,                 :datetime
23   property :updated_at,                 :datetime
24   
25   validates_length_of         :login,                   :within => 3..40
26   validates_uniqueness_of     :login
27   validates_presence_of       :email
28   # validates_format_of         :email,                   :as => :email_address
29   validates_length_of         :email,                   :within => 3..100
30   validates_uniqueness_of     :email
31   validates_presence_of       :password,                :if => proc {password_required?}
32   validates_presence_of       :password_confirmation,   :if => proc {password_required?}
33   validates_length_of         :password,                :within => 4..40, :if => proc {password_required?}
34   validates_confirmation_of   :password,                :groups => :create
35     
36   before_save :encrypt_password
37   before_create :make_activation_code
38   after_create :send_signup_notification
39   
40   def login=(value)
41     @login = value.downcase unless value.nil?
42   end
43     
44   
45   EMAIL_FROM = "info@mysite.com"
46   SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE.  Please activate your account."
47   ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
48   
49   # Activates the user in the database
50   def activate
51     @activated = true
52     self.activated_at = Time.now.utc
53     self.activation_code = nil
54     save
56     # send mail for activation
57     UserMailer.dispatch_and_deliver(  :activation_notification,
58                                   {   :from => User::EMAIL_FROM,
59                                       :to   => self.email,
60                                       :subject => User::ACTIVATE_MAIL_SUBJECT },
62                                       :user => self )
64   end
65   
66   def send_signup_notification
67     UserMailer.dispatch_and_deliver(
68         :signup_notification,
69       { :from => User::EMAIL_FROM,
70         :to  => self.email,
71         :subject => User::SIGNUP_MAIL_SUBJECT },
72         :user => self        
73     )
74   end
75   
78   
79 end