Skip to content

Instantly share code, notes, and snippets.

@zacksiri
Created June 27, 2011 15:13
Show Gist options
  • Save zacksiri/1049056 to your computer and use it in GitHub Desktop.
Save zacksiri/1049056 to your computer and use it in GitHub Desktop.
User Model
class User
include Mongoid::Document
devise :database_authenticatable,:registerable,:omniauthable,:recoverable, :rememberable, :trackable , :timeoutable , :confirmable #, :validatable
devise :encryptable, :encryptor => 'sha512'
validates_presence_of :email, :if => :sign_up
validates_presence_of :email, :first_name, :last_name, :add1, :add2, :id_number, :if => :edit_page
validates_uniqueness_of :email , :case_sensitive => true
attr_accessor :edit_page, :sign_up
references_many :transactions, :dependent => :delete, :inverse_of => :user
field :customer_id, type: Integer, default: 0
field :title, type: String
field :first_name, type: String
field :last_name, type: String
field :gender, type: String
field :company, type: String
field :add1, type: String
field :add2, type: String
field :city, type: String
field :country, type: String
field :zip_code, type: String
field :telephone, type: String
field :mobile, type: String
field :fax, type: String
field :email, type: String
field :id_type, type: String
field :id_number, type: String
field :birth_date, type: Date
field :encrypted_password, type: String
field :fb_id, type: String
field :current_sign_in_at, type: Date
field :current_sign_in_ip, type: String
field :last_sign_in_at, type: Date
field :last_sign_in_ip, type: String
field :sign_in_count, type: Integer
field :fb_points , :type => Float, :default => 0.00
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
user = User.find(:first, :conditions=>{ :email => data["email"]})
unless user.nil?
user.fb_id = data["id"]
user.first_name = data["first_name"]
user.last_name = data["last_name"]
user.birth_date = data["birthday"]
user.save
user
else
user = User.new( :fb_id=>data["id"], :email => data["email"], :password => Devise.friendly_token[0,20] ,
:first_name => data["first_name"], :last_name => data['last_name'], :birth_date => data['birthday'],
:gender => data['gender'], :current_sign_in_at =>'', :current_sign_in_ip => '', :last_sign_in_at => '',
:last_sign_in_ip => '', :sign_in_count => 1 )
user.skip_confirmation!
user.save
user
end
check_customer(user)
user = User.find(:first, :conditions=>{:email=>data["email"]})
facebook = Facebook.new
#facebook.my_points(data,user)
user
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
user.email = data["email"]
user.fname = data["first_name"]
user.lname = data["last_name"]
user.bday = data["birthday"]
end
end
end
def full_name
self.first_name.concat(' ').concat(self.last_name)
end
def create_customer(hotel_id)
customer = Cheqin::Customer.create(hotel_id, customer: { email: self.email, add1: self.add1, add2: self.add2, first_name: self.first_name,
last_name: self.last_name, city: self.city, country: self.country, id_number: self.id_number,
id_type: self.id_type, zip: self.zip_code, birth_date: self.birth_date, gender: self.gender,
telephone: self.telephone, mobile: self.mobile, fax: self.fax, company: self.company,
title: self.title }
)
if customer
self.update_attribute(:customer_id, customer.id)
else
return false
end
end
def sync_customer(hotel_id)
customer = Cheqin::Customer.sync(hotel_id, :email => self.email)
unless customer.nil?
self.update_attributes( customer_id: customer.id, title: customer.title, email: customer.email, first_name: customer.first_name,
last_name: customer.last_name, gender: customer.gender, add1: customer.add1, add2: customer.add2,
city: customer.city, country: customer.country, zip_code: customer.zip_code, telephone: customer.telephone,
mobile: customer.mobile, fax: customer.fax, id_type: customer.id_type, id_number: customer.id_number,
birth_date: customer.birth_date
)
return true
else
self.update_attributes(customer_id: 0, first_name: "Guest", last_name: "")
return false
end
end
def self.cart_total(current_user)
current_user.carts.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment