Share
Embed
Transcript
20 slides
-
Active Record Callbacks Ror lab. season 3 - the 5th
round - April 27th, 2013 ChangHoon Jeong(@seapy) 13֙ 4ਘ 27ੌ షਃੌ -
Contents • The Object Life Cycle • Validations Overview •
Validation Helpers • Common Validation Options • Conditional Validation • Custom Validations • Working with Validation Errors • Displaying Validation Errors in the View • Callbacks Overview • Available Callbacks • Running Callbacks • Skipping Callbacks • Halting Execution • Relational Callbacks • Conditional Callbacks • Callback Classes • Observers • Transaction Callbacks Validations Callbacks 13֙ 4ਘ 27ੌ షਃੌ -
Life-cycle of Objects Object Create Save Update Delete Active Record
M odel DB 13֙ 4ਘ 27ੌ షਃੌ -
Life-cycle of Objects Object Create Save Update Delete Validation load
Active Record M odel DB 13֙ 4ਘ 27ੌ షਃੌ -
Callbacks • Methods : protected or private • Before or
after certain moments of an object’s life cycle • 6 events : create, save, update, delete, validate, load • Register using a macro-style class method ★ as an ordinary method or ★ supply as a block 13֙ 4ਘ 27ੌ షਃੌ -
As Ordinary Methods class User < ActiveRecord::Base validates :login, :email,
:presence => true before_validation :ensure_login_has_a_value protected def ensure_login_has_a_value if login.nil? self.login = email unless email.blank? end end end Macro-style class methods : 13֙ 4ਘ 27ੌ షਃੌ -
A Block class User < ActiveRecord::Base validates :login, :email, :presence
=> true before_create do |user| user.name = user.login.capitalize if user.name.blank? end end Macro-style class methods : 13֙ 4ਘ 27ੌ షਃੌ -
Available Callbacks ★Creating an Object • before_validation • after_validation •
before_save • around_save • before_create • around_create • after_create • after_save ★Updating an Object • before_validation • after_validation • before_save • around_save • before_update • around_update • after_update • after_save ★Destroying an Object • before_destroy • around_destroy • after_destroy Save Destroy Create Update Validation DB 13֙ 4ਘ 27ੌ షਃੌ -
After_find > After_initialize class User < ActiveRecord::Base after_initialize do |user|
puts "You have initialized an object!" end after_find do |user| puts "You have found an object!" end end >> User.new You have initialized an object! => #<User id: nil> >> User.first You have found an object! You have initialized an object! => #<User id: 1> 13֙ 4ਘ 27ੌ షਃੌ -
Methods Triggering Callbacks • create • create! • decrement! •
destroy • destroy_all • increment! • save • save! • save(:validate =>false) • toggle! • update • update_attribute • update_attributes • update_attributes! • valid? ‘after_find’ callback • all • first • find • find_all_by_attribute • find_by_attribute • find_by_attribute! • last 13֙ 4ਘ 27ੌ షਃੌ -
Methods Skipping Callbacks • decrement • decrement_counter • delete •
delete_all • find_by_sql • increment • increment_counter • toggle • touch • update_column • update_all • update_counters 13֙ 4ਘ 27ੌ షਃੌ -
Methods Calling Callbacks Methods Skipping Callbacks create create! decrement! decrement
/ ~_counter destroy delete destroy_all delete_all increment! increment / ~_counter save save! save(false) toggle! toggle update update_attribute update_column update_attributes update_all update_attributes! update_counters valid? find_by_sql touch 13֙ 4ਘ 27ੌ షਃੌ -
Halting Execution •Queue ★ validations for model ★ registered callbacks
★ database operations • Wrapped in a Transaction ★ before_ callbacks : false or exception > stopped > ROLLBACK ★ after_ callbacks : exception > stopped > ROLLBACK 13֙ 4ਘ 27ੌ షਃੌ -
Conditional Callbacks • As with validations ★ Using :if and
:unless with a Symbol ★ Using :if and :unless with a String ★ Using :if and :unless with a Proc 13֙ 4ਘ 27ੌ షਃੌ -
Conditional Callbacks as a symbol as a string as a
Proc * multiple conditions possible! 13֙ 4ਘ 27ੌ షਃੌ -
public/protected/private class public protected private class public protected private parent
child private_method http://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails (self.)protected_method 13֙ 4ਘ 27ੌ షਃੌ