activerecord callbacks - rorlab season 3-5

20
Active Record Callbacks Ror lab. season 3 - the 5th round - April 27th, 2013 ChangHoon Jeong(@seapy) 13427토요일

Upload: -

Post on 25-May-2015

308 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: ActiveRecord Callbacks - RORLab Season 3-5

Active Record Callbacks

Ror lab. season 3- the 5th round -

April 27th, 2013

ChangHoon Jeong(@seapy)

13년 4월 27일 토요일

Page 2: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 3: ActiveRecord Callbacks - RORLab Season 3-5

Life-cycle of Objects

DB

13년 4월 27일 토요일

Page 4: ActiveRecord Callbacks - RORLab Season 3-5

Life-cycle of Objects

ActiveRecord

Model

DB

13년 4월 27일 토요일

Page 5: ActiveRecord Callbacks - RORLab Season 3-5

Life-cycle of Objects

Object

Create Save

UpdateDelete

ActiveRecord

Model

DB

13년 4월 27일 토요일

Page 6: ActiveRecord Callbacks - RORLab Season 3-5

Life-cycle of Objects

Object

Create Save

UpdateDelete Validat

ion

load

ActiveRecord

Model

DB

13년 4월 27일 토요일

Page 7: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 8: ActiveRecord Callbacks - RORLab Season 3-5

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  endend

Macro-style class methods :

13년 4월 27일 토요일

Page 9: ActiveRecord Callbacks - RORLab Season 3-5

A Block

class User < ActiveRecord::Base  validates :login, :email, :presence => true   before_create do |user|    user.name = user.login.capitalize if user.name.blank?  endend

Macro-style class methods :

13년 4월 27일 토요일

Page 10: ActiveRecord Callbacks - RORLab Season 3-5

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

UpdateValid

atio

n DB

13년 4월 27일 토요일

Page 11: ActiveRecord Callbacks - RORLab Season 3-5

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!"  endend >> User.newYou have initialized an object!=> #<User id: nil> >> User.firstYou have found an object!You have initialized an object!=> #<User id: 1>

13년 4월 27일 토요일

Page 12: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 13: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 14: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 15: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 16: ActiveRecord Callbacks - RORLab Season 3-5

Relational Callbacks

13년 4월 27일 토요일

Page 17: ActiveRecord Callbacks - RORLab Season 3-5

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일 토요일

Page 18: ActiveRecord Callbacks - RORLab Season 3-5

Conditional Callbacks

as a symbol

as a string

as a Proc

* multiple conditions possible!

13년 4월 27일 토요일

Page 19: ActiveRecord Callbacks - RORLab Season 3-5

감사합니다.����������� ������������������  

13년 4월 27일 토요일