Skip to content

Instantly share code, notes, and snippets.

@smooki
Created January 11, 2012 15:00
Show Gist options
  • Save smooki/1595063 to your computer and use it in GitHub Desktop.
Save smooki/1595063 to your computer and use it in GitHub Desktop.
user model
class User < ActiveRecord::Base
include StaticList::Validate
# static_list [[:male, 0], [:female, 1], [:no_specified, 2]
validates_static_list_value :sex, Sex, :allow_blank => true
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, , :lockable, :timeoutable and
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable,
# from devise_invitable
#:confirmable,
:invitable, :invite_for => 2.weeks
attr_accessible :email, :password, :password_confirmation, :avatar,
:birthdate, :name, :surname, :bio, :sex, :town, :favorite_drink,
:favorite_music, :favorite_appetizer, :favorite_aperitif,
:share_sex, :share_town, :share_birthdate, :share_email,
:share_name , :share_surname, :nickname
has_many :events
has_many :comments
has_many :invitations, :class_name => "EventInvitation"
has_many :friends, :source => :friend
has_many :communications
has_many :messages, :foreign_key => "sender_id"
has_many :event_transports
# paperclip attachment
has_attached_file :avatar,
:styles => { :medium => "200x200",
:thumb => "32x32",
:in_glass => ["145x145", :png] },
:default_url => '/images/blank200.png',
:convert_options => { :in_glass => Proc.new{self.convert_options} }
# in glass avatar process by paperclip
def self.convert_options
trans = ""
px = 70
trans << " \\( +clone -threshold -1 "
trans << "-draw 'fill black polygon 0,0 0,#{px} #{px},0 fill white circle #{px},#{px} #{px},0' "
trans << "\\( +clone -flip \\) -compose Multiply -composite "
trans << "\\( +clone -flop \\) -compose Multiply -composite "
trans << "\\) +matte -compose CopyOpacity -composite "
end
def display_name
nickname || email
end
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
# user allreay exists, return it
user
else # create user and confirm it, no need to confirm via email etc.
user = User.create!(:email => data["email"], :password => Devise.friendly_token[0,20], :agreement_status => true)
# confirm it to avoid email confirmation need
user.skip_confirmation!
user.confirm!
user.save!
user
end
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"]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment