ActiveRecord Callbacks & Observers, Season 2

1,437 views

Published on

This is a Keynote slide about the "ActiveRecord Callbacks & Observers"

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,437
On SlideShare
0
From Embeds
0
Number of Embeds
303
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ActiveRecord Callbacks & Observers, Season 2

    1. 1. Ror lab. season 2 - the 7th round - Active RecordCallbacks & Observers October 6th, 2012 Hyoseong Choi
    2. 2. Contents Validations Callbacks• The Object Life Cycle • Callbacks Overview• Validations Overview • Available Callbacks• Validation Helpers • Running Callbacks• Common Validation • Skipping Callbacks Options • Halting Execution• Conditional Validation • Relational Callbacks• Custom Validations • Conditional Callbacks• Working with Validation • Callback Classes Errors • Observers• Displaying Validation Errors • Transaction Callbacks in the View
    3. 3. Life-cycle of Objects DB
    4. 4. Life-cycle of Objects DB Active Record el od M
    5. 5. Life-cycle ofActiveRecord el Objects od M DB Create Save Object Delete Update
    6. 6. Life-cycle ofActiveRecord el Objects od M DB Create Save Validation Object Delete Update load
    7. 7. Callbacks• Methods : protected or private• Before or after certain moments of an object’s life cycle• 6create, save, update, delete, validate, load : events• Register using a macro-style class method ★ as an ordinary method or ★ supply as a block
    8. 8. Available Callbacks★ Creating an Object ★ Updating an Object ★ Destroying an Object• before_validation • before_validation • before_destroy• after_validation • after_validation • around_destroy• before_save • before_save • after_destroy• before_create • before_update• around_create • around_update• after_create • after_update• after_save • after_save DB Validation Create Save Update Destroy
    9. 9. After_find > After_initializeclass User < ActiveRecord::Base  after_initialize do |user|    puts "You have initialized an object!"  end   after_find do |user|    puts "You have found an object!"  endend >> User.newYou have initialized an object!=> #<User id: nil> >> User.firstYou have found an object!You have initialized an object!=> #<User id: 1>
    10. 10. MethodsTriggering Callbacks•• create create! ‘after_find’ callback• decrement! • all• destroy • first• destroy_all • find• increment! • find_all_by_attribute• save • find_by_attribute• save! • find_by_attribute!• save(:validate =>false)• toggle! • last• update• update_attribute• update_attributes• update_attributes!• valid?
    11. 11. Methods Skipping Callbacks• decrement• decrement_counter• delete• delete_all• find_by_sql• increment• increment_counter• toggle• touch• update_column• update_all• update_counters
    12. 12. Methods Calling Callbacks Methods Skipping Callbackscreatecreate!decrement! decrement / ~_counterdestroy deletedestroy_all delete_allincrement! increment / ~_countersavesave!save(false)toggle! toggleupdateupdate_attribute update_columnupdate_attributes update_allupdate_attributes! update_countersvalid? find_by_sql touch
    13. 13. 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
    14. 14. RelationalCallbacks
    15. 15. 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
    16. 16. Conditional Callbacks as a symbol as a string as a Proc* multiple conditions possible!
    17. 17. Callback Classes • To reuse callback methods for other models as an instance method a model objectinstancemethod
    18. 18. Callback Classesas a class method
    19. 19. Observers• Similar to callbacks• for the callbacks with the code that isn’t directly related the model• without changing the code of the model• ex. sending confirmation email after user registration
    20. 20. Creating Observers
    21. 21. Registering Observers • config/application.rb • or config/environments
    22. 22. Sharing Observers as a model name
    23. 23. Transaction Callbacks• after_commit or after_rollback
    24. 24. 감사합니다.
    25. 25.  
    26. 26. Macro-style class methods : 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
    27. 27. public/protected/private parent pr class iv at child e_ m private et ho class protected public d private (sel f.)p protected rote public ed_ ct met hodhttp://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails
    28. 28. Macro-style class methods : 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

    ×