Created
October 31, 2013 19:45
-
-
Save hgsigner/7255659 to your computer and use it in GitHub Desktop.
User model for the socialadega
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Mongoid::MultiParameterAttributes | |
include ActiveModel::SecurePassword | |
has_many :feeds | |
has_many :feed_comments | |
has_many :corks | |
has_many :wine_comments | |
has_many :notifications | |
mount_uploader :avatar, AvatarUploader | |
has_secure_password | |
field :name | |
field :email | |
field :state | |
field :city | |
field :country, :type => BSON::ObjectId | |
field :birth_date, :type => Date | |
field :gender | |
field :cork_page_privacy, :type => String, :default => "opened" | |
field :drink_count, :type => Integer, :default => 0 | |
field :language, :type => String, :default => "pt" | |
field :friendship_invitations, :type => Array | |
field :friends_list, :type => Array | |
field :wine_likes_list, :type => Array | |
field :password_digest | |
field :password_reset_token | |
field :password_reset_sent_at, :type => DateTime, :default => nil | |
field :last_gmail_invitation_sent, :type => DateTime, :default => nil | |
field :last_hotmail_invitation_sent, :type => DateTime, :default => nil | |
field :last_yahoo_invitation_sent, :type => DateTime, :default => nil | |
field :provider | |
field :uid | |
field :oauth_token | |
field :oauth_expires_at, :type => DateTime | |
field :role, type: String, default: "user" | |
#Validation | |
EMAIL_REGEX = /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ | |
validates :name, | |
:presence => true | |
validates :email, | |
:presence => true, | |
:uniqueness => true, | |
:format => {:with => EMAIL_REGEX} | |
validates :password, | |
:presence => true, | |
:confirmation => true, | |
:if => :should_update_password | |
attr_accessible :name, | |
:email, | |
:country, | |
:state, | |
:city, | |
:language, | |
:cork_page_privacy, | |
:password, | |
:password_confirmation, | |
:avatar, | |
:birth_date, | |
:gender, | |
:last_gmail_invitation_sent, | |
:last_hotmail_invitation_sent, | |
:last_yahoo_invitation_sent | |
attr_accessor :updating_password | |
# Functions ---------------------- | |
def should_update_password | |
updating_password || new_record? | |
end | |
def generate_token(column) | |
begin | |
self[column] = SecureRandom.urlsafe_base64 | |
end while User.where(column => self[column]).exists? | |
end | |
def get_user_age | |
((Date.today - self.birth_date) / 365.25).to_i | |
end | |
def get_importer_time(name) | |
case name | |
when "gmail" | |
self.last_gmail_invitation_sent | |
when "hotmail" | |
self.last_hotmail_invitation_sent | |
else | |
self.last_yahoo_invitation_sent | |
end | |
end | |
# Reset Password | |
def send_password_reset | |
generate_token(:password_reset_token) | |
self.password_reset_sent_at = Time.zone.now | |
save(:validate => false) | |
UserMailer.delay.send_password_reset(self) | |
end | |
# User *** | |
def self.from_omniauth(auth) | |
find_or_create_by(:email => auth.info.email).tap do |user| | |
if user.new_record? | |
br = Country.where(:name => "Brazil").first | |
user.provider = auth.provider | |
user.uid = auth.uid | |
user.name = auth.info.name | |
user.email = auth.info.email | |
user.oauth_token = auth.credentials.token | |
user.oauth_expires_at = Time.at(auth.credentials.expires_at) | |
user.country = br.id | |
user.language = "pt" | |
user.save(:validate => false) | |
UserMailer.delay.send_user_welcome(user) | |
else | |
user.oauth_token = auth.credentials.token | |
user.oauth_expires_at = Time.at(auth.credentials.expires_at) | |
user.save(:validate => false) | |
end | |
end | |
end | |
def get_name | |
full_name = self.name.split(" ") | |
if full_name.count > 1 | |
"#{full_name[0]} #{full_name[1]}" | |
else | |
"#{full_name[0]}" | |
end | |
end | |
def get_country | |
Country.find(self.country) | |
end | |
def has_invited_this_user?(user) | |
current_user_invitations_list = User.where(:friendship_invitations.in => [self.id]).to_a | |
if current_user_invitations_list.include?(user) | |
return true | |
else | |
return false | |
end | |
end | |
def has_this_user_invitation?(user_id) | |
invitations_list = self.friendship_invitations.to_a | |
if invitations_list.include?(user_id) | |
return true | |
else | |
return false | |
end | |
end | |
def get_invitation_owners_list | |
invitations_list = self.friendship_invitations.to_a | |
users_list =[] | |
if invitations_list.any? | |
invitations_list.each do |invitation| | |
users_list << User.find(invitation) | |
end | |
end | |
users_list | |
end | |
def get_friends(page) | |
list = User.where(:friends_list.in => [self.id]).order_by([:name, :asc]) | |
get_current_page(list, list.count, page) | |
end | |
def get_friends_total | |
User.where(:friends_list.in => [self.id]).order_by([:name, :asc]).count | |
end | |
def get_user_friends_total_pages | |
total_list = User.where(:friends_list.in => [self.id]).order_by([:name, :asc]).count | |
if total_list > 0 | |
(total_list / 10.0).ceil | |
else | |
0 | |
end | |
end | |
def is_friend_of?(user) | |
friends_of_current_user = User.where(:friends_list.in => [self.id]).to_a | |
friends_of_user = User.where(:friends_list.in => [user.id]).to_a | |
if friends_of_user.include?(self) && friends_of_current_user.include?(user) | |
return true | |
else | |
return false | |
end | |
end | |
# User Wine Finders *** | |
def get_liked_wines | |
Wine.where(:wine_likers_list.in => [self.id]) | |
end | |
# Adega | |
def get_adega(page) | |
list = Wine.where(:users_with_current_wine_in_adega.in => [self.id]).order_by([:name, :asc]) | |
get_current_page(list, list.count, page) | |
end | |
def get_adega_total | |
Wine.where(:users_with_current_wine_in_adega.in => [self.id]).count | |
end | |
def get_adega_total_pages | |
total_list = Wine.where(:users_with_current_wine_in_adega.in => [self.id]).count | |
if total_list > 0 | |
(total_list / 10.0).ceil | |
else | |
0 | |
end | |
end | |
# Wishlist | |
def get_wishlist(page) | |
list = Wine.where(:users_with_current_wine_in_wishlist.in => [self.id]).order_by([:name, :asc]) | |
get_current_page(list, list.count, page) | |
end | |
def get_wishlist_total | |
Wine.where(:users_with_current_wine_in_wishlist.in => [self.id]).count | |
end | |
def get_wishlist_total_pages | |
total_list = Wine.where(:users_with_current_wine_in_wishlist.in => [self.id]).count | |
if total_list > 0 | |
(total_list / 10.0).ceil | |
else | |
0 | |
end | |
end | |
def liked_current_wine?(wine) | |
the_wine = Wine.find(wine.id) | |
liked_current_wine_list = the_wine.wine_likers_list.to_a | |
if liked_current_wine_list.include?(self.id) | |
return true | |
else | |
return false | |
end | |
end | |
def has_current_wine_in_adega?(wine) | |
the_wine = Wine.find(wine.id) | |
has_in_adega_list = the_wine.users_with_current_wine_in_adega.to_a | |
if has_in_adega_list.include?(self.id) | |
return true | |
else | |
return false | |
end | |
end | |
def has_current_wine_in_wishlist?(wine) | |
the_wine = Wine.find(wine.id) | |
has_in_wishlist_list = the_wine.users_with_current_wine_in_wishlist.to_a | |
if has_in_wishlist_list.include?(self.id) | |
return true | |
else | |
return false | |
end | |
end | |
# Corks | |
def get_corks(page) | |
list = self.corks.order_by([:created_at, :desc]) | |
get_current_page(list, list.count, page) | |
end | |
def get_corks_total_pages | |
total_list = self.corks.all.count | |
if total_list > 0 | |
(total_list / 10.0).ceil | |
else | |
0 | |
end | |
end | |
private | |
def get_current_page(collection, total, page) | |
feed_per_page = 10.0 | |
feed_total = total | |
feed_pages_number = (feed_total / feed_per_page).ceil | |
offset = (page.to_i - 1) * feed_per_page.to_i | |
offset_up_to = offset + feed_per_page.to_i - 1 | |
collection[offset..offset_up_to].collect{ |f| f } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment