customer.rb

来自「本书源码对于学习RUBY网络编程语言非常有帮助。」· RB 代码 · 共 61 行

RB
61
字号
class Customer < ActiveRecord::Base  has_many :orders,           :dependent => true,           :order     => "created_at ASC"  def open_orders    orders.find(:all, :conditions => "status = 'open'")  end  def editions_on_order    open_orders.map {|order| order.edition }.uniq  end  def edition_history    orders.map {|order| order.edition }.uniq  end  def works_on_order    editions_on_order.map {|edition| edition.works }.flatten.uniq  end  def work_history    edition_history.map {|edition| edition.works }.flatten.uniq  end  def copies_of(edition)    open_orders.select {|order| order.edition == edition } .size  end  def balance    editions_on_order.inject(0) {|acc,edition| acc += edition.price }  end  def check_out    orders.each do |order|      order.update_attributes(:status => "paid")    end  end  def rank(list)    list.uniq.sort_by do |a|      list.select {|b| a == b }.size    end.reverse  end  def composer_rankings    rank(edition_history.map {|ed| ed.composers }.flatten)  end  def instrument_rankings    rank(work_history.map {|work| work.instruments }.flatten)  end  def favorites(thing,options)    limit = options[:count]    rankings = send("#{thing}_rankings")    return rankings[0,limit].compact  endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?