Skip to content

Instantly share code, notes, and snippets.

@kirs
Forked from Olefine/user.rb
Last active December 19, 2015 12:39
Show Gist options
  • Save kirs/5956578 to your computer and use it in GitHub Desktop.
Save kirs/5956578 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
#default_scope where(:banned => false)
scope :unbanned, where(:banned => false)
include ChooseCity
rolify
acts_as_voter
has_karma(:questions, :as => :submitter, :weight => 0.5)
mount_uploader :avatar, AvatarUploader
devise :invitable, :database_authenticatable, :registerable, #:confirmable,
:recoverable, :rememberable, #, :validatable#, :trackable #
:omniauthable
attr_accessor :read # user's agreement
attr_accessible :email, :password, :remember_me, :vk, :fb, :tw, :mailru,
:type_user, :provider, :uid, :avatar, :profileable_id, :profileable_type, :read
attr_accessible :email, :password, :remember_me, :vk, :fb, :tw, :mailru,
:type_user, :provider, :uid, :avatar, :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 :friendships
has_many :friends, :through => :friendships, :source => :friend, :conditions => "friendships.status = 'accepted'"
has_many :friendships_awaiting_acceptance, :conditions => "friendships.status = 'requested'", :class_name => 'Friendship', :foreign_key => :friend_id
has_and_belongs_to_many :message_rooms
has_many :messages
has_many :group_messages, foreign_key: :creator_id
has_many :sent_messages, :class_name => "Message"
has_many :received_messages, :class_name => "Message", :foreign_key => :recipient_id
has_many :unread_messages, :class_name => "Message", :foreign_key => :recipient_id, :conditions => "messages.read = false"
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
scope :last_users, ->(count) { order("id DESC").limit(count) }
def name
client.try(:firstname) || firm.try(:name)
end
def client
return profileable if profileable_type == 'Client'
end
def firm
return profileable if profileable_type == 'Firm'
end
def can_edit?(user)
true
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