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