public
Description: open-source e-commerce built on merb
Clone URL: git://github.com/myabc/merb_mart.git
Click here to lend your support to: merb_mart and make a donation at www.pledgie.com !
myabc (author)
Wed May 07 17:32:49 -0700 2008
commit  82d5d35a57e3408aa5ae1857e93355bca7b9648a
tree    4139d747e8ff0e433b5bce68c8d920032d0fadd3
parent  74660e9ab6d526687192e2a6383f852a3bc7d125
merb_mart / app / models / user.rb
100644 81 lines (67 sloc) 2.863 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'digest/sha1'
require "date"
require "dm-aggregates"
require "dm-validations"
begin
  require File.join(File.dirname(__FILE__), '..', '..', "lib", "authenticated_system", "authenticated_dependencies")
rescue
  nil
end
class User
  
  include DataMapper::Resource
  include DataMapper::Validate
  include AuthenticatedSystem::Model
  
  attr_accessor :password, :password_confirmation
  
  property :id, Fixnum, :serial => true
  property :login, String, :unique => true
  property :email, String
  property :crypted_password, String
  property :salt, String
  property :activation_code, String
  property :activated_at, DateTime
  property :remember_token_expires_at, DateTime
  property :remember_token, String
  property :created_at, DateTime
  property :updated_at, DateTime
  
  validates_length_of :login, :within => 3..40
  validates_uniqueness_of :login
  validates_presence_of :email
  validates_format_of :email, :as => :email_address
  validates_length_of :email, :within => 3..100
  validates_uniqueness_of :email
  validates_presence_of :password, :if => lambda { |r| r.password_required? }
  validates_presence_of :password_confirmation, :if => lambda { |r| r.password_required? }
  validates_length_of :password, :within => 4..40, :if => lambda { |r| r.password_required? }
  validates_confirmation_of :password#, :groups => :create
  
  before :save, :encrypt_password
  #before_class_method :create, :make_activation_code
  #after_class_method :create, :send_signup_notification
  
  def login=(value)
    @login = value.downcase unless value.nil?
  end
  
  EMAIL_FROM = "info@mysite.com"
  SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE. Please activate your account."
  ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
  
  # Activates the user in the database
  def activate
    @activated = true
    self.activated_at = Time.now.utc
    self.activation_code = nil
    save
 
    # send mail for activation
    UserMailer.dispatch_and_deliver( :activation_notification,
                                  { :from => User::EMAIL_FROM,
                                      :to => self.email,
                                      :subject => User::ACTIVATE_MAIL_SUBJECT },
 
                                      :user => self )
 
  end
  
  #def send_signup_notification
  # UserMailer.dispatch_and_deliver(
  # :signup_notification,
  # { :from => User::EMAIL_FROM,
  # :to => self.email,
  # :subject => User::SIGNUP_MAIL_SUBJECT },
  # :user => self
  # )
  #end
  
end