Converted view ERB syntax to Haml
[merb_mart.git] / app / models / user.rb
blob045852a54d482abaa19ded2f8d8437e53dec9e47
1 require 'digest/sha1'
2 require "date"
3 require "dm-aggregates"
4 require "dm-validations"
5 begin
6   require File.join(File.dirname(__FILE__), '..', '..', "lib", "authenticated_system", "authenticated_dependencies")
7 rescue 
8   nil
9 end
10 class User
11   
12   include DataMapper::Resource
13   include DataMapper::Validate
14   include AuthenticatedSystem::Model
15   
16   attr_accessor :password, :password_confirmation
17   
18   property :id,                         Fixnum,   :serial => true
19   property :login,                      String,   :unique => true
20   property :email,                      String
21   property :crypted_password,           String
22   property :salt,                       String
23   property :activation_code,            String
24   property :activated_at,               DateTime
25   property :remember_token_expires_at,  DateTime
26   property :remember_token,             String
27   property :created_at,                 DateTime
28   property :updated_at,                 DateTime
29   
30   validates_length_of         :login,                   :within => 3..40
31   validates_uniqueness_of     :login
32   validates_presence_of       :email
33   validates_format_of         :email,                   :as => :email_address
34   validates_length_of         :email,                   :within => 3..100
35   validates_uniqueness_of     :email
36   validates_presence_of       :password,                :if => lambda { |r| r.password_required? }
37   validates_presence_of       :password_confirmation,   :if => lambda { |r| r.password_required? }
38   validates_length_of         :password,                :within => 4..40, :if => lambda { |r| r.password_required? }
39   validates_confirmation_of   :password#,                :groups => :create
40   
41   before :save,   :encrypt_password
42   #before_class_method :create, :make_activation_code
43   #after_class_method  :create, :send_signup_notification
44   
45   def login=(value)
46     @login = value.downcase unless value.nil?
47   end
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   
80 end