Slide 107
Slide 107 text
1 module Spree
2 class User < ActiveRecord::Base
3 devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable,
4 :rememberable, :trackable, :validatable, :encryptable, :encryptor => 'authlogic_sha512'
5
6 has_many :orders
7 has_and_belongs_to_many :roles, :join_table => 'spree_roles_users'
8 belongs_to :ship_address, :foreign_key => 'ship_address_id', :class_name => 'Spree::Address'
9 belongs_to :bill_address, :foreign_key => 'bill_address_id', :class_name => 'Spree::Address'
10
11 before_save :check_admin
12 before_validation :set_login
13
14 # Setup accessible (or protected) attributes for your model
15 attr_accessible :email, :password, :password_confirmation, :remember_me, :persistence_token
16
17 users_table_name = User.table_name
18 roles_table_name = Role.table_name
19
20 scope :admin, lambda { includes(:roles).where("#{roles_table_name}.name" => "admin") }
21 scope :registered, where("#{users_table_name}.email NOT LIKE ?", "%@example.net")
22
23 # Creates an anonymous user. An anonymous user is basically an auto-generated +User+ account that is created for the customer
24 # behind the scenes and its completely transparently to the customer. All +Orders+ must have a +User+ so this is necessary
25 # when adding to the "cart" (which is really an order) and before the customer has a chance to provide an email or to register.
26 def self.anonymous!
27 token = User.generate_token(:persistence_token)
28 User.create(:email => "#{token}@example.net", :password => token,
:password_confirmation => token, :persistence_token => token)
29 end
30
31 def anonymous?
32 email =~ /@example.net$/
33 end
spree/auth/app/models/spree/user.rb
spree_auth ‘user’
90
Saturday, February 18, 12