Permalink
Browse files

updated the README

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@330 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent ac8f84c commit b35c5eebfc4732d984101dd411b2545f15026063 jcarroll committed Feb 14, 2008
Showing with 17 additions and 30 deletions.
  1. +1 −1 MIT-LICENSE
  2. +16 −29 README
View
2 MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008 [name of plugin creator]
+Copyright (c) 2008 Jared Carroll and Dan Croak
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
45 README
@@ -3,9 +3,9 @@ When
When adds :if and :unless conditions to ActiveRecord callbacks
and validations and ActionController filters. It works exactly
-the way as the current implementation of validates_acceptance_of.
+the way as the current implementation of #validates_acceptance_of.
-It works on all 14 callbacks:
+It works on the following callbacks:
before_validation
before_validation_on_create
@@ -19,40 +19,37 @@ after_update
after_save
before_destroy
after_destroy
-after_initialize
-after_find
3 validations:
validate
validate_on_create
validate_on_update
-and 3 filters:
+and 1 filter:
before_filter
-after_filter
-around_filter
-It works when :if or :unless is passed symbols, lambdas, and strings.
+It works when :if or :unless is passed a Symbol, a lambdas or a String.
They should return or evaluate to a true or false value.
-
Example
=======
class Address < ActiveRecord::Base
+
before_save :geolocate
def geolocate
- if self.complete?
+ if complete?
...
end
end
def complete?
street? && city? && state? && zip?
end
+
end
In this case, we want to find the latitude and longitude of an address only if
@@ -66,7 +63,9 @@ With When, the WHEN responsibility is moved to where it belongs:
as part of the callback.
class Address < ActiveRecord::Base
- before_save :geolocate, :if => :complete?
+
+ before_save :geolocate,
+ :if => :complete?
def geolocate
...
@@ -75,32 +74,20 @@ class Address < ActiveRecord::Base
def complete?
street? && city? && state? && zip?
end
+
end
The callback's single responsibility is to execute code WHEN certain conditions are met.
-The geolocate method's single responsibility is to ... geolocate.
-
+The #geolocate method's single responsibility is to ... geolocate.
More Examples
=============
-after_update :send_notifications, :unless => lambda {|record| record.minor_change?}
-
-after_initialize :capitalize_title, :if => :new_record?
-
-before_create %{self.password = password.to_sha1},
+before_create :encrypt_password,
:unless => lambda {|user| user.password_confirmation.blank?}
-before_create :unless => lambda {|user| user.password_confirmation.blank?} do |record|
- record.password = record.password.to_sha1
-end
-
-before_create PasswordEncryptor, :unless => lambda {|user| user.password_confirmation.blank?}
-class PasswordEncryptor
- def self.before_create(record)
- record.password = record.password.to_sha1
- end
-end
-
+before_filter :log_in!,
+ :only => [:new, :create],
+ :unless => :logged_in?
Copyright (c) 2008 Jared Carroll and Dan Croak, released under the MIT license

0 comments on commit b35c5ee

Please sign in to comment.