rosa-build/app/models/user.rb

140 lines
5.3 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
2011-03-09 16:16:48 +00:00
class User < ActiveRecord::Base
2012-03-21 17:45:51 +00:00
ROLES = ['', 'admin', 'banned']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
MAX_AVATAR_SIZE = 5.megabyte
2011-10-23 22:39:44 +01:00
2011-10-26 10:31:58 +01:00
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
:recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable
has_attached_file :avatar, :styles =>
2012-03-13 18:53:37 +00:00
{ :micro => { :geometry => "16x16#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:small => { :geometry => "30x30#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:medium => { :geometry => "40x40#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'},
:big => { :geometry => "81x81#", :format => :jpg, :convert_options => '-strip -background white -flatten -quality 70'}
}
validates_inclusion_of :avatar_file_size, :in => (0..MAX_AVATAR_SIZE), :allow_nil => true
has_one :notifier, :class_name => 'Settings::Notifier', :dependent => :destroy #:notifier
2012-01-11 13:58:13 +00:00
has_many :activity_feeds, :dependent => :destroy
has_many :authentications, :dependent => :destroy
has_many :build_lists, :dependent => :destroy
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :relations, :as => :object, :dependent => :destroy
has_many :targets, :as => :object, :class_name => 'Relation', :dependent => :destroy
2011-10-17 15:23:51 +01:00
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
has_many :groups, :through => :targets, :source => :target, :source_type => 'Group', :autosave => true
2011-10-17 15:23:51 +01:00
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group', :dependent => :destroy
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
include Modules::Models::PersonalRepository
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }
validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? }
2012-03-22 14:50:15 +00:00
validates :role, :inclusion => {:in => ROLES}, :allow_blank => true
validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true
2011-03-10 21:48:15 +00:00
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :login, :name, :uname, :language,
:site, :company, :professional_experience, :location, :avatar
attr_readonly :uname, :own_projects_count
attr_accessor :login
2012-03-08 00:32:12 +00:00
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :without, lambda {|l| where("users.id NOT IN (?)", Array(l))}
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
scope :opened, where('1=1')
2012-03-22 14:50:15 +00:00
scope :banned, where(:role => 'banned')
scope :admin, where(:role => 'admin')
scope :real, where(:role => ['', nil])
after_create lambda { self.create_notifier }
2012-01-11 13:58:13 +00:00
def admin?
role == 'admin'
end
2012-01-26 08:53:40 +00:00
def user?
persisted?
end
def guest?
new_record?
end
2012-03-22 14:50:15 +00:00
def access_locked?
role == 'banned'
end
def fullname
return "#{uname} (#{name})"
end
class << self
def find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(uname) = :value OR lower(email) = :value", { :value => login.downcase }]).first
end
def new_with_session(params, session)
super.tap do |user|
if data = session["devise.omniauth_data"]
2011-12-06 19:57:03 +00:00
if info = data['info'] and info.present?
user.email = info['email'].presence if user.email.blank?
user.uname ||= info['nickname'].presence || info['username'].presence
user.name ||= info['name'].presence || [info['first_name'], info['last_name']].join(' ').strip
end
user.password = Devise.friendly_token[0,20] # stub password
user.authentications.build :uid => data['uid'], :provider => data['provider']
end
2011-03-29 23:16:04 +01:00
end
2011-03-10 21:48:15 +00:00
end
end
2011-03-10 21:48:15 +00:00
# def update_with_password(params={})
# params.delete(:current_password)
# # self.update_without_password(params) # Don't allow password update
# if params[:password].blank?
# params.delete(:password)
# params.delete(:password_confirmation) if params[:password_confirmation].blank?
# end
# result = update_attributes(params)
# clean_up_passwords
# result
# end
2012-01-26 08:53:40 +00:00
2012-01-29 20:18:14 +00:00
def commentor?(commentable)
comments.exists?(:commentable_type => commentable.class.name, :commentable_id => commentable.id.hex)
2012-01-29 20:18:14 +00:00
end
def committer?(commit)
2012-01-30 06:38:23 +00:00
email.downcase == commit.committer.email.downcase
2012-01-29 20:18:14 +00:00
end
def owner_of? object
if object.respond_to? :owner
object.owner_id == self.id or self.group_ids.include? object.owner_id
else
false
end
end
2012-03-19 14:33:56 +00:00
def user_appeal
name.blank? ? uname : name
end
2012-01-11 13:58:13 +00:00
private
def create_settings_notifier
self.create_notifier
2012-02-19 17:27:47 +00:00
end
2011-03-09 16:16:48 +00:00
end