ROR Model层代码约定

2 篇文章 0 订阅
2 篇文章 0 订阅

  随着业务逻辑复杂度和team人数的增长,日渐膨胀的Model层代码需要一个统一的代码约定。

  以下是我参考了别人的建议后给出的约定:

  1. require/extend/include (they are dependencies)
  2. associations, acts_as_*(they are relationships with either other models or other instances of same model)
  3. constants, scopes, class methods(they are all 'class methods')
  4. accessors, nested_attributes_for, validates, callbacks, instance methods, delegations(they are all 'instance methods')
  5. method mark: private, protected

例子:

classArticle<ActiveRecord::Base

   require 'json'

   include Redis::Search

   index :name

 

  has_many :comments
  belongs_to :author

   acts_as_commentable

   acts_as_list

  default_scope order("id desc")
  scope :published,where(:published =>true)
  scope :created_after,lambda{|time|["created_at >= ?", time]}

  class<<self
    def batch_create(data)
      # ...
    end
  end

  validates :title,:presence =>true

  before_create :init_score
  def init_score
    self.score =10
  end

  def any_instance_method
    # ...
  end

  begin"score related functions"# Group functions by begin .. end
    def add_score(score)
      # ...
    end
  end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值