beast1.0 解读(二)

简单的分析一下,发觉有了文档什么问题都不是问题!
model代码中我发觉user.rb代码比较丰富多彩一些,那就拿user.rb拿分析一下.

ruby 代码
 
  1. require 'digest/sha1'  #分析:引入Hash加密算法  
  2.   
  3. class User < ActiveRecord::Base  
  4.   has_many :moderatorships:dependent => :destroy  
  5.   has_many :forums:through => :moderatorships:order => 'forums.name'  
  6.   
  7.   has_many :posts  
  8.   has_many :topics  
  9.   has_many :monitorships  
  10.   has_many :monitored_topics:through => :monitorships:conditions => ['monitorships.active = ?', true], :order => 'topics.replied_at desc', :source => :topic  
  11.   #分析:  
  12.   #has_many就是表与表之间一对多的关系,就如:users表与forums表  
  13.   #这里专门介绍一个has_many各种Options的用法.  
  14.   #:class_name - 仅当model名不能直接由关联名推断出时,就必须指定这个选项值,  
  15.   #如:has_many :products 默认情况下,他的model的名字应为product;但是这个关联的model的名为SpecialProduct,这时我们就得指定这个选项值.  
  16.   #:conditions - 这个应该比较容易理解,就是取关联表数据时,必须加上:conditions所指定的条件.  
  17.   #:order - 就是取关联表数据时,返回一个"order by"的SQL片段.  
  18.   #:group - 关联表数据返回一个"group by"的SQL片段,如:"category"  
  19.   #:foreign_key - 默认的情况下外键为单数的表名+"_id";当外键与默认的约定不同时,那就得在这里选项里设值了.  
  20.   #:dependent - 从字面上看的意思是依赖,相关的意思.比如: has_many :moderatorships, :dependent => :destroy这句代码的意思就是当User  
  21.   #执行destroy方法时,那相应也会相应的删除moderatorships表里该user相关联的数据.  
  22.   #:exclusively_dependent - 排外依赖.就是关联表的相关记录一条SQL删除,不会因为其他表的关联产生回滚现象.  
  23.   #:through: 指定一个Join的方式执行一个跨表查询.   
  24.     
  25.   validates_presence_of     :login:email #分析:这两个字段必填  
  26.   validates_length_of       :login:minimum => 2  #分析:login的最小长度为2  
  27.     
  28.   with_options :if => :password_requireddo |u|  #分析:如果需要密码则:增加如下的验证条件.  
  29.     u.validates_presence_of     :password_hash  
  30.     u.validates_length_of       :password:minimum => 5, :allow_nil => true  
  31.     u.validates_confirmation_of :password:on => :create  #分析:一致性检查 仅在user.create的时候  
  32.     u.validates_confirmation_of :password:on => :update:allow_nil => true  
  33.   end  
  34.     
  35.   
  36.   # names that start with #s really upset me for some reason  
  37.   validates_format_of       :login:with => /^[a-z]{2}(?:\w+)?$/i  #分析:正则表达式  
  38.   validates_format_of       :identity_url:with => /^https?:\/\//i, :allow_nil => true  
  39.   
  40.   # names that start with #s really upset me for some reason  
  41.   validates_format_of     :display_name:with => /^[a-z]{2}(?:[.'\-\w ]+)?$/i, :allow_nil => true  
  42.   
  43.   validates_format_of :email:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Please check the e-mail address"[:check_email_message]  
  44.   
  45.   validates_uniqueness_of   :login:email:case_sensitive => false  
  46.   validates_uniqueness_of   :display_name:identity_url:case_sensitive => false:allow_nil => true  
  47.   before_validation { |u| u.identity_url = nil if u.identity_url.blank? }  
  48.   before_validation { |u| u.display_name = u.login if u.display_name.blank? }  
  49.   # first user becomes admin automatically  
  50.   before_create { |u| u.admin = u.activated = true if User.count == 0 }  
  51.   format_attribute :bio  
  52.   
  53.   attr_reader :password  
  54.   attr_protected :admin:posts_count:login:created_at:updated_at:last_login_at:topics_count:activated  
  55.   
  56.   def self.currently_online  
  57.     User.find(:all:conditions => ["last_seen_at > ?"Time.now.utc-5.minutes])  
  58.   end  
  59.   
  60.   # we allow false to be passed in so a failed login can check  
  61.   # for an inactive account to show a different error  
  62.   def self.authenticate(login, password, activated=true)  
  63.     find_by_login_and_password_hash_and_activated(login, Digest::SHA1.hexdigest(password + PASSWORD_SALT), activated)  
  64.   end  
  65.   
  66.   def self.search(query, options = {})  
  67.     with_scope :find => { :conditions => build_search_conditions(query) } do  
  68.       find :all, options  
  69.     end  
  70.   end  
  71.   
  72.   def self.build_search_conditions(query)  
  73.     query && ['LOWER(display_name) LIKE :q OR LOWER(login) LIKE :q', {:q => "%#{query}%"}]  
  74.   end  
  75.   
  76.   def password=(value)  
  77.     return if value.blank?  
  78.     write_attribute :password_hash, Digest::SHA1.hexdigest(value + PASSWORD_SALT)  
  79.     @password = value  
  80.   end  
  81.     
  82.   def reset_login_key!  
  83.     self.login_key = Digest::SHA1.hexdigest(Time.now.to_s + password_hash.to_s + rand(123456789).to_s).to_s  
  84.     # this is not currently honored  
  85.     self.login_key_expires_at = Time.now.utc+1.year  
  86.     save!  
  87.     login_key  
  88.   end  
  89.   
  90.   def moderator_of?(forum)  
  91.     moderatorships.count(:all:conditions => ['forum_id = ?', (forum.is_a?(Forum) ? forum.id : forum)]) == 1  
  92.   end  
  93.   
  94.   def to_xml(options = {})  
  95.     options[:except] ||= []  
  96.     options[:except] << :email << :login_key << :login_key_expires_at << :password_hash << :identity_url  
  97.     super  
  98.   end  
  99.     
  100.   def password_required?  
  101.     identity_url.nil?  
  102.   end  
  103. end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值