Whitespace cleanup
[merb_mart.git] / app / models / user.rb
blob4a03266e0056de38d6975d70512d16faf0174461
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
12   include DataMapper::Resource
13   include AuthenticatedSystem::Model
15   attr_accessor :password, :password_confirmation
17   property :id,                         Fixnum,   :serial => true
18   property :login,                      String,   :unique => true
19   property :email,                      String
20   property :crypted_password,           String
21   property :salt,                       String
22   property :activation_code,            String
23   property :activated_at,               DateTime
24   property :remember_token_expires_at,  DateTime
25   property :remember_token,             String
26   property :created_at,                 DateTime
27   property :updated_at,                 DateTime
29   validates_length        :login,                   :within => 3..40
30   validates_is_unique     :login
31   validates_present       :email
32   validates_format        :email,                   :as => :email_address
33   validates_length        :email,                   :within => 3..100
34   validates_is_unique     :email
35   validates_present       :password,                :if => lambda { |r| r.password_required? }
36   validates_present       :password_confirmation,   :if => lambda { |r| r.password_required? }
37   validates_length        :password,                :within => 4..40, :if => lambda { |r| r.password_required? }
38   validates_is_confirmed  :password#,                :groups => :create
40   before :save,   :encrypt_password
41   #before_class_method :create, :make_activation_code
42   #after_class_method  :create, :send_signup_notification
44   def login=(value)
45     @login = value.downcase unless value.nil?
46   end
48   EMAIL_FROM = "info@mysite.com"
49   SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE.  Please activate your account."
50   ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
52   # Activates the user in the database
53   def activate
54     @activated = true
55     self.activated_at = Time.now.utc
56     self.activation_code = nil
57     save
59     # send mail for activation
60     UserMailer.dispatch_and_deliver(  :activation_notification,
61                                   {   :from => User::EMAIL_FROM,
62                                       :to   => self.email,
63                                       :subject => User::ACTIVATE_MAIL_SUBJECT },
65                                       :user => self )
67   end
69   #def send_signup_notification
70   #  UserMailer.dispatch_and_deliver(
71   #      :signup_notification,
72   #    { :from => User::EMAIL_FROM,
73   #      :to  => self.email,
74   #      :subject => User::SIGNUP_MAIL_SUBJECT },
75   #      :user => self
76   #  )
77   #end
79 end