# encoding: utf-8 require 'digest/sha2' class User < ActiveRecord::Base validates :email, format: {with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, message: '请填写有效电子邮箱'}, :on => :create validates :password, format: {with: /^[a-zA-Z0-9~@#%&_\.\-\^\$\*\!\+\=]{6,16}$/,message: '必须由6-16个字母、数字或特殊字符组成'}, :on => :create validates :password, :confirmation => true , :on => :create validates :name ,format: {with: /^[\u4e00-\u9fa5]{2,4}$/,message: '中文姓名必须由2到4个汉字组成'} validates :city ,format: {with: /^[\u4e00-\u9fa5]{2,10}$/,message: '地名必须由2到10个汉字组成'} validates :agree, inclusion: { in: %w(1), message: "%{value} is invalid." } validates :status, inclusion: { in: %w(0 1 2), message: "%{value} is invalid." } validates :gender, inclusion: { in: %w(0 1), message: "%{value} is invalid." } attr_accessor :password_confirmation attr_reader :password validate :password_must_be_present validate :email_unique, :on=> :create before_create :make_activation_code, :set_pinyin has_many :posts has_many :comments has_many :groups has_many :requestee_relationships,:class_name=>"Relationship",:foreign_key=>"requester_id" has_many :requestees,:through=>:requestee_relationships,:source=>:requestee has_many :requester_relationships,:class_name=>"Relationship",:foreign_key=>"requestee_id" has_many :requesters,:through=>:requester_relationships,:source=>:requester STATUS = {"0" => "未工作","1"=>"在上学" ,"2"=>"在工作"} UserPath="#{Rails.root.to_s}/public/uploads/users" #UserPath="#{Rails.root.to_s}/public/上传/用户" # 'password' is a virtual attribute def password=(password) @password = password if password.present? generate_salt self.encrypted_password = self.class.encrypt_password(password, salt) end end def active? activation_code.nil? end # Activates the user in the database. def activate! activate save(:validate => false) end def activate @activated = true self.activated_at = Time.now self.activation_code = nil end # Returns true if the user has just been activated. def recently_activated? @activated end =begin select user_id,count(user_id) as same from friendships where friend_id in (select friend_id from friendships where user_id=1 ) and user_id in (6,7,8,9,10,16,17,18,19,20) and friend_id<>1 group by user_id order by same desc =end #有共同好友的,从好友的好友中找,不包含已经发送请求或是好友关系的 def suggested_friends rs1=Relationship.where("requester_id='#{self.id}' and (status='pending' or status='accepted')") ids1= rs1.map{|r| r.requestee_id} rs2=Relationship.where("requestee_id='#{self.id}' and (status='pending' or status='accepted')") ids2 = rs2.map{|r| r.requester_id} rs3 = User.all(:conditions=>["activation_code is NULL and id not in (?)",ids1+ids2+[self.id]]) ids3 =rs3.map{|r| r.id} _ids3=ids3.join(",") rs=ActiveRecord::Base.connection.execute("select user_id,friend_id from friendships where friend_id in (select friend_id from friendships where user_id='#{self.id}' ) and user_id in (#{_ids3}) and friend_id<>'#{self.id}'") hash={} rs.each_with_index do |r,i| hash[r[0]]=[] if hash[r[0]].nil? hash[r[0]] << r[1] end users = rs3.select{|r| hash.keys.include? r.id} users.sort_by! {|u| -hash[u.id].count} return users,hash end def search(keyword) rs1=Relationship.where("requester_id='#{self.id}' and (status='pending' or status='accepted')") ids1= rs1.map{|r| r.requestee_id} rs2=Relationship.where("requestee_id='#{self.id}' and (status='pending' or status='accepted')") ids2 = rs2.map{|r| r.requester_id} User.all(:conditions=>["activation_code is NULL and id not in (?)",ids1+ids2+[self.id]]) end def is_friend_of?(user_id) arr1 = Relationship.where("requester_id='#{self.id}' and requestee_id='#{user_id}'") arr2 = Relationship.where("requestee_id='#{self.id}' and requester_id='#{user_id}'") return true if !arr1.empty? and arr1[0].status=="accepted" return true if !arr2.empty? and arr2[0].status=="accepted" return false end def friends(group) if group.nil? or group=~/^\-\d+$/ or group=~/^\d+$/ _group = group.to_i if group.nil? or _group ==-3 a = self.requesters.where("relationships.status='accepted'") b = self.requestees.where("relationships.status='accepted'") return a+b elsif _group>-3 fs= Friendship.where("user_id='#{self.id}' and group_id='#{group}'") ids = fs.map{|f| f.friend_id } return User.all(:conditions=>["id in (?)",ids]) end elsif ('a'..'z').include?(group.downcase) fs= Friendship.where("user_id='#{self.id}'") ids = fs.map{|f| f.friend_id } return User.all(:conditions=>["id in (?) and pinyin like ?",ids,"#{group.downcase}%"]) else [] end end def self.same_friend(user_id,id) rs=ActiveRecord::Base.connection.execute("select user_id,friend_id from friendships where friend_id in (select friend_id from friendships where user_id='#{user_id}' ) and user_id='#{id}' and friend_id<>'#{user_id}'") rs.count end =begin select distinct relationships.requestee_id,friendships.group_id from relationships left join friendships on friendships.relationship_id=relationships.id where relationships.requester_id=1 and relationships.status='accepted'; select distinct relationships.requester_id,friendships.group_id from relationships left join friendships on friendships.relationship_id=relationships.id where relationships.requestee_id=1 and relationships.status='accepted'; =end class << self def birth(year,month,day) #20.days.ago.to_date.to_s return year + "-" +month+ "-" +day end def authenticate(email, password) if user = find_by_email(email) if user.encrypted_password == encrypt_password(password, user.salt) if user.active? return [true,user] else return [false,user] end end end [false,nil] end def encrypt_password(password, salt) Digest::SHA2.hexdigest(password + "wibble" + salt) end def secure_digest(*args) Digest::SHA2.hexdigest(args.flatten.join('--')) end def make_token secure_digest(Time.now, (1..10).map{ rand.to_s }) end def valid_image?(file) format = file.original_filename.split(".")[-1] =~ /jpg|jpeg|png|gif|bmp/ size = file.size <= 2*1024*1024 #小于2M format&&size end end def User.another_user?(current_user,another) current_user && current_user.id != another.to_i end def self.upload_logo(hash) path= "/#{hash[:id]}/logo" FileUtils.mkdir_p "#{UserPath}#{path}" original_type = hash[:uploaded_logo].original_filename.split(".")[-1] basename = File.basename(hash[:uploaded_logo].original_filename,".*") _now = Time.now.strftime("%Y%m%d%H%M%S") _path = "/#{UserPath.split("/")[-2..-1].join("/")}" logo_name = basename.force_encoding("UTF-8") save_logo_path = "#{UserPath}#{path}/#{logo_name}_#{_now}.#{original_type}" logo_path="#{_path}#{path}/#{logo_name}_#{_now}.#{original_type}" small_logo_name = "#{basename}_small" save_small_logo_path="#{UserPath}#{path}/#{small_logo_name}#{_now}.#{original_type}" small_logo_path="#{_path}#{path}/#{small_logo_name}#{_now}.#{original_type}" medium_logo_name = "#{basename}_medium" save_medium_logo_path="#{UserPath}#{path}/#{medium_logo_name}#{_now}.#{original_type}" medium_logo_path="#{_path}#{path}/#{medium_logo_name}#{_now}.#{original_type}" large_logo_name = "#{basename}_large" save_large_logo_path = "#{UserPath}#{path}/#{large_logo_name}#{_now}.#{original_type}" large_logo_path = "#{_path}#{path}/#{large_logo_name}#{_now}.#{original_type}" File.open(save_logo_path,"wb") do |file| file.write hash[:uploaded_logo].read end img = Magick::Image.from_blob(hash[:uploaded_logo].read)[0] img.write(save_logo_path) thumbnail = img.resize_to_fill(50, 50) thumbnail.write(save_small_logo_path) thumbnail = img.resize_to_fill(120, 120) thumbnail.write(save_medium_logo_path) thumbnail = img.resize_to_fill(180, 180) thumbnail.write(save_large_logo_path) [logo_name,logo_path,small_logo_path,medium_logo_path,large_logo_path] end private def password_must_be_present errors.add(:password, "Missing password") unless encrypted_password.present? end def email_unique if User.find_by_email(self.email) errors.add(:email,"该邮箱已经注册,请您换一个邮箱") end end def birth_valid?(year, month,day) begin @date = Date.new(year.to_i, month.to_i, day.to_i) return @date>=Date.new(1900,1,1)&&@date<=Time.now.to_date rescue ArgumentError return false end end def check_birth unless birth_valid? errors.add(:birth,"日期格式错误或不在范围内") end end def generate_salt self.salt = self.object_id.to_s + rand.to_s end def make_activation_code self.activation_code = self.class.make_token end def set_pinyin self.pinyin = PinYin.of_string(self.name, :ascii).join.gsub!(/\d/,"") end end =begin self.errors.add_to_base(self.class.instance_variable_get(:@message)) self.errors.add(:captcha, self.class.instance_variable_get(:@message)) flash.now[:error] @reply.errors.full_messages.join(',') DUPLICATE_RECORD_ERRORS = [ /^Mysql::Error:\s+Duplicate\s+entry\b/, /^PG::Error:\s+ERROR:\s+duplicate\s+key\b/, /\bConstraintException\b/ ] def self.duplicate_record_error?(error) error.class.name == 'ActiveRecord::RecordNotUnique' or DUPLICATE_RECORD_ERRORS.any? { |re| re =~ error.message } end =end
相关推荐
它在我们的测试中在 Windows 10 上顺利运行,对系统资源消耗的影响最小。由于其直观的界面和众多功能,IDE 应该满足许多程序员的要求。 支持的操作系统:Windows 7,Windows 8,Windows 10,MacOS,* nux 支持的...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
可以利用它为任意样式的接口生成多语言的SDK,代码示例,测试用例,接口编排等。 概念 组件 解析器(当前模块) 生成器(陆续开源中) Swift 镖 Ruby a Kotlin :提供语法高亮,代码提示,代码格式化,语法检查 :...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
交互式Ruby工具-改进的irb和rails控制台,其中包含许多简单而强大的工具。 什么是IRT? IRT是改进的irb / rails控制台(用于2号和3号导轨),为标准irb增加了许多功能。 如果您使用IRT代替irb,则将拥有所有常规的...
:warning: 此主题需要安装ruby和rubygems特征干净的布局一次性布局分页语法高亮社会联系标签列表页面类别列表页面Google Analytics(分析)整合Disqus整合安装和测试下载或克隆git clone git@github....
--自定义语法高亮, 支持 HTML, XML, CSS, JavaScript, VBScript, ASP, PHP, CSS, Perl/CGI, C/C++, C#, Java, VB, Pascal, Assembler, SQL, Python, NSIS, INI, REG, INF, BAT, DIFF, Ruby 等众多文件 --支持 ANSI、...
*支持13种主要编程语言(C / C ++ / Java / Python / C#/ Perl / Ruby / Scala / Clojure / Haskell / Go / Erlang)*所有支持语言的语法高亮显示。 即将推出的功能:*从本地文件系统保存/加载代码。 *干净直观的UI...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
9.3 迭代D3:高亮显示变化 94 9.4 迭代D4:隐藏空购物车 96 9.5 迭代D5:JavaScript被禁用时的对策 99 9.6 我们做了什么 99 第10章 任务E:付账! 101 10.1 迭代E1:收集订单信息 101 第11章 任务F:管理 113 11.1 ...
特征语法高亮通过终端伪造的REPL 通过全局标签文件“〜/ .sctags”进行代码导航(跳转到定义) 通过标签完成自动完成已知单词启动SuperCollider QT帮助系统tmux和screen支持要求Ruby超级对撞机安装scvim插件安装强烈...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...
JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接...