ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年3月1日 星期一
Advanced Active Record Techniques: Best Practice Refactoring
Advanced Active Record Techniques: Best Practice Refactoring
#p54
class ArticleVersion < ActiveRecord::Base
before_validation_on_create :set_version_number
before_create :mark_related_links_not_current, :if => :current_version
private
def current_version
article.current_version
end
#下面兩個method 都會call 到 article.current_version
#這樣就可以抽出來
#然後mark_related_linkers_not_current 會需要判斷 article.current_version 的存在與否這也可以利用 Conditional Callback 來判斷
def set_version_number
self.version = (current_version ? current_version.version : 0) + 1
end
def mark_related_links_not_current
current_version.relateds.each do |rel|
rel.update_attribute(:current, false)
end
end
end
#================================================================
#p74
class ArticleVersion < ActiveRecord::Base
STATES = { :edit => 'Edit',
:edit_check => 'Edit Check',
:sub_edit => 'Sub Edit',
:publish_ready => 'Publish Ready',
:published => 'Published' }
STATES.each do |key, value|
define_method "#{key}?", {
self.state == "#{value}"
}
end
end
#=========================================================
# p84
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
validates_presence_of :name
validates_uniqueness_of :name
def name=(value)
write_attribute("name", value.downcase)
end
# 想用symbol 拿值
def self.[](name) # Get a role quickly by using: Role[:admin]
self.find(:first, :conditions => ["name = ?", name.id2name])
end
def add_user(user)
self.users << user
end
def delete_user(user)
self.users.delete(user)
end
end
#====================================================
# p91
class User < ActiveRecord::Base
has_many :roles
end
class Role < ActiveRecord::Base
TYPES = %w(admin editor writer guest)
validates_inclusion_of :name, :in => TYPES
# 生成 class method
class << self
TYPES.each do |role_type|
define_method "#{role_type}?" do
# Exists? http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002268
roles.exists?(:name => role_type)
end
end
end
end
# =====================================================
# p96
# Step 1
class User < ActiveRecord::Base
include Addressable
end
class Order < ActiveRecord::Base
include Addressable
end
module Addressable
def self.included(base)
base.validates_presence_of :billing_address_one, :billing_city, :billing_state,
:billing_zip, :billing_country
base.validates_presence_of :shipping_address_one, :shipping_city, :shipping_state,
:shipping_zip, :shipping_country
base.validates_format_of :billing_zip,
:with => /\d{5}(-\d{4})?/,
:if => Proc.new { |klass| klass.billing_country == "US" }
base.validates_format_of :shipping_zip,
:with => /\d{5}(-\d{4})?/,
:if => Proc.new { |klass| klass.shipping_country == "US" }
base.validates_format_of :billing_zip,
:with => /[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d/,
:if => Proc.new { |klass| klass.billing_country == "CA" }
base.validates_format_of :shipping_zip,
:with => /[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d/,
:if => Proc.new { |klass| klass.shipping_country == "CA" }
end
end
# Step 2
class User < ActiveRecord::Base
has_address :shipping
has_address :billing
has_many :orders
end
module Addressable
class << self
def included(base)
base.extend ClassMethods
end
end
module ClassMethods
def has_address(name = "")
name = "#{name}_" unless name.blank?
validates_presence_of "#{name}address_one", "#{name}city",
"#{name}state_province", "#{name}postal_code", "#{name}country"
validates_format_of "#{name}postal_code", :with => /\d{5}(-\d{4})?/,
:if => Proc.new { |user| user["#{name}country"] == "US" }
validates_format_of "#{name}postal_code", :with => /[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d/,
:if => Proc.new { |user| user["#{name}country"] == "CA" }
end
end
end
ActiveRecord::Base.send :include, Addressable
# Step 3
class User < ActiveRecord::Base
has_address :shipping
has_address :billing
has_many :orders
end
module Addressable
class << self
def included(base)
base.extend ClassMethods
end
end
module ClassMethods
def has_address(name)
name << “_” unless name.blank?
validates_presence_of "#{name}address_one", "#{name}city",
"#{name}state_province", "#{name}postal_code", "#{name}country"
validates_format_of "#{name}postal_code", :with => /\d{5}(-\d{4})?/,
:if => Proc.new { |user| user["#{name}country"] == "US" }
validates_format_of "#{name}postal_code", :with => /[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d/,
:if => Proc.new { |user| user["#{name}country"] == "CA" }
composed_of "#{name}address", :class_name => “Address”,
:mapping => [ ["#{name}address_one", "address_one"],
["#{name}address_two", "address_two"],
["#{name}city", "city"],
["#{name}state_province", "state_province"],
["#{name}postal_code", "postal_code"]
["#{name}country", "country"] ]
end
end
end
ActiveRecord::Base.send :include, Addressable
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言