Skip to content

Instantly share code, notes, and snippets.

@Olefine
Created August 31, 2013 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Olefine/6400559 to your computer and use it in GitHub Desktop.
Save Olefine/6400559 to your computer and use it in GitHub Desktop.
# encoding: utf-8
class User < ActiveRecord::Base
attr_accessor :read # user's agreement
scope :unbanned, where(:banned => false)
scope :last_users, ->(count) { order("id DESC").limit(count) }
include Models::User::City
include Models::User::MessageStuff
include Models::User::Friendship
include Models::User::SocialAuth
rolify
acts_as_voter
has_karma(:questions, :as => :submitter, :weight => 0.5)
mount_uploader :avatar, AvatarUploader
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :omniauthable
attr_accessible :email, :password, :password_confirmation, :remember_me, :vk, :fb, :tw, :mailru, :login,
:type_user, :provider, :uid, :avatar, :profileable_id, :profileable_type, :read,
:banned, :read, :admin_role_ids, :as => :admin
validates :read, :presence => true, :on => :create
validate :password_complexity
validates :email, :presence => true, :uniqueness => true
validates :profileable_id, :presence => true
acts_as_commentable
DEPTH = 2
belongs_to :profileable, :polymorphic => true, :dependent => :destroy
has_many :videos, :class_name => "MediaContent", :as => :multimedia, :conditions => ["media_contents.video = ?", true]
has_many :audios, :class_name => "MediaContent", :as => :multimedia, :conditions => ["media_contents.video = ?", false]
has_many :likes, :as => :likeable
has_many :complaints, :as => :pretension, :dependent => :destroy
has_many :invitations, :class_name => 'User', :as => :invited_by
has_many :albums, :as => :resource
has_many :photos, :through => :albums
has_many :posts, :as => :journal, :dependent => :destroy
has_and_belongs_to_many :communities
has_many :admin_roles, :source => :role, :through => :users_roles,
:conditions => {:roles => {:resource_type => nil, :resource_id => nil, :name => [:admin, :moderator]}}
has_many :users_roles
delegate :city_id, to: :client, prefix: true, allow_nil: true
delegate :city_id, to: :firm, prefix: true, allow_nil: true
def custom_remove_role name, resource
delete_role = roles.where(resource_type: resource.class.to_s,
resource_id: resource.id,
name: name).first
delete_users_roles = delete_role.users_roles.where(user_id: id).first
delete_users_roles.destroy
end
def name
self.client.try(:firstname)||self.firm.try(:name)
end
def client
return self.profileable if self.profileable_type == 'Client'
end
def firm
return self.profileable if self.profileable_type == 'Firm'
end
def is?(user)
self.id == user.id
end
def owner?(user)
self.id == user.id
end
def comment_threads
Comment.find_comments_for_commentable(profileable_type, profileable_id)
end
def root_object
self
end
def can_edit?(user)
true
end
#Пользователь который пригласил
def invited_couple
User.find(self.couple_id)
end
def self.search_couple(exclude_obj, search_string, limit = 10)
q = search_string.to_s.gsub(/\\/, '\\\\\\\\').gsub(/%/, '\%').gsub(/_/, '\_')
self.no_couple.where("id <> ? and (name like '%#{q}%' or surname like '%#{q}%') and gender <> ?", exclude_obj.id, exclude_obj.gender).limit(limit)
end
#for polls votes
def voted? votable
votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name)
return nil if votes.size == 0
return votes.first.vote_flag
end
private
def password_complexity
if password.present? and not password.match(/^[0-9A-Za-z#\$-%]{6,}$/)
errors.add :password, "Должен содержать не менее 6 смиволов, может содержать A-Za-z,0-9,(#$-%)"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment