activerecord拾贝
>> Operator.first.tenants.send :construct_sql
=> "`tenants`.operator_id = 0"
>> Operator.first.tenants.send :construct_scope
=> {:create=>{"operator_id"=>0}, :find=>{:readonly=>false, :include=>nil, :conditions=>"`tenants`.operator_id = 0", :limit=>10, :order=>nil}}
>> Operator.read_inheritable_attribute(:after_add_for_tenants)
=> [:do_after_add]
# Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
# as keys, this will fail.
#
# ==== Examples
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
unknown_keys = keys - [valid_keys].flatten
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end
>> pp Operator.reflections.first
[:bills,
#<ActiveRecord::Reflection::AssociationReflection:0x9927490
@active_record=
Operator(id: integer, host: string, title: string, logo_url: string, biglogo_url: string, footer: text, descr: text, telphone: string, contact: string, email: string, remember_token: string, crypted_password: string, salt: string, activated_at: date, activation_code: string, company: string, is_support_bank: boolean, bank: string, bank_account_name: string, bank_account: string, bank_tax_number: string, bank_payment_number: string, is_support_alipay: boolean, alipay_email: string, alipay_partner: string, alipay_key: string, created_at: datetime, updated_at: datetime, remember_token_expires_at: datetime, page_contact: text, page_about: text, amount: decimal),
@macro=:has_many,
@name=:bills,
@options={:extend=>[]}>]
定义dependent 回调
>> pp Operator.new.methods.grep /has_many/
["has_many_dependent_destroy_for_tenants"]
>> Operator.methods.grep /after_add/
=> ["after_add_for_tenants"]
>> Operator.after_add_for_tenants
=> [:aaa]
调用after_add
def callback(method, record)
callbacks_for(method).each do |callback|
ActiveSupport::Callbacks::Callback.new(method, callback, record).call(@owner, record)
end
end
def callbacks_for(callback_name)
full_callback_name = "#{callback_name}_for_#{@reflection.name}"
@owner.class.read_inheritable_attribute(full_callback_name.to_sym) || []
end
base.define_callbacks *CALLBACKS