Skip to content

Instantly share code, notes, and snippets.

@andruu
Created February 21, 2013 16:13
Show Gist options
  • Save andruu/5005822 to your computer and use it in GitHub Desktop.
Save andruu/5005822 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name
has_one :shopping_cart
has_and_belongs_to_many :addresses
has_and_belongs_to_many :payment_options
validates :email, uniqueness: true, presence: true, format: { with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
has_secure_password
def self.authenticate(email, password)
find_by_email(email).try(:authenticate, password)
end
def self.new_guest
user = User.new
user.guest = true
user.save validate: false
user
end
def name
if guest
"Guest"
else
"#{first_name} #{last_name}"
end
end
end
class ShoppingCart < ActiveRecord::Base
belongs_to :user
has_many :items, class_name: 'ShoppingCartItem'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment