0
When adds :if and :unless conditions to ActiveRecord callbacks
0
and validations and ActionController filters. It works exactly
0
-the way as the current implementation of
validates_acceptance_of.
0
+the way as the current implementation of
#validates_acceptance_of.
0
-It works on
all 14 callbacks:
0
+It works on
the following callbacks:
0
before_validation_on_create
0
@@ -19,8 +19,6 @@ after_update
0
@@ -28,24 +26,22 @@ validate
0
-It works when :if or :unless is passed
symbols, lambdas, and strings.
0
+It works when :if or :unless is passed
a Symbol, a lambdas or a String.
0
They should return or evaluate to a true or false value.
0
class Address < ActiveRecord::Base
0
@@ -53,6 +49,7 @@ class Address < ActiveRecord::Base
0
street? && city? && state? && zip?
0
In this case, we want to find the latitude and longitude of an address only if
0
@@ -66,7 +63,9 @@ With When, the WHEN responsibility is moved to where it belongs:
0
as part of the callback.
0
class Address < ActiveRecord::Base
0
- before_save :geolocate, :if => :complete?
0
+ before_save :geolocate,
0
@@ -75,32 +74,20 @@ class Address < ActiveRecord::Base
0
street? && city? && state? && zip?
0
The callback's single responsibility is to execute code WHEN certain conditions are met.
0
-The geolocate method's single responsibility is to ... geolocate.
0
+The #geolocate method's single responsibility is to ... geolocate.
0
-after_update :send_notifications, :unless => lambda {|record| record.minor_change?}
0
-after_initialize :capitalize_title, :if => :new_record?
0
-before_create %{self.password = password.to_sha1},
0
+before_create :encrypt_password,
0
:unless => lambda {|user| user.password_confirmation.blank?}
0
-before_create :unless => lambda {|user| user.password_confirmation.blank?} do |record|
0
- record.password = record.password.to_sha1
0
-before_create PasswordEncryptor, :unless => lambda {|user| user.password_confirmation.blank?}
0
-class PasswordEncryptor
0
- def self.before_create(record)
0
- record.password = record.password.to_sha1
0
+before_filter :log_in!,
0
+ :only => [:new, :create],
0
+ :unless => :logged_in?
0
Copyright (c) 2008 Jared Carroll and Dan Croak, released under the MIT license
Comments
No one has commented yet.