public
Description: Conditional checks on Rails filters
Clone URL: git://github.com/thoughtbot/when.git
updated the README

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@330 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jcarroll (author)
Wed Feb 13 16:25:45 -0800 2008
commit  b35c5eebfc4732d984101dd411b2545f15026063
tree    9ab01e1208b86ad5b643707d9d559aa605bf267d
parent  ac8f84c4bed35bdf6b4bc383d4f94f5842c5393c
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-Copyright (c) 2008 [name of plugin creator]
0
+Copyright (c) 2008 Jared Carroll and Dan Croak
0
 
0
 Permission is hereby granted, free of charge, to any person obtaining
0
 a copy of this software and associated documentation files (the
0
...
3
4
5
6
 
7
8
 
9
10
11
...
19
20
21
22
23
24
25
26
...
28
29
30
31
 
32
33
34
35
36
37
 
38
39
40
41
42
43
44
 
45
46
47
48
 
49
50
51
...
53
54
55
 
56
57
58
...
66
67
68
69
 
 
 
70
71
72
...
75
76
77
 
78
79
80
81
82
 
83
84
85
86
87
88
89
90
91
 
92
93
94
95
96
97
98
99
100
101
102
103
104
 
 
 
105
106
...
3
4
5
 
6
7
 
8
9
10
11
...
19
20
21
 
 
22
23
24
...
26
27
28
 
29
30
31
 
 
32
 
33
34
35
 
36
37
38
39
40
41
42
43
 
44
45
46
47
...
49
50
51
52
53
54
55
...
63
64
65
 
66
67
68
69
70
71
...
74
75
76
77
78
79
80
 
 
81
82
83
84
85
 
 
 
 
 
86
87
88
 
 
 
 
 
 
 
 
 
 
 
89
90
91
92
93
0
@@ -3,9 +3,9 @@ When
0
 
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
 
0
-It works on all 14 callbacks:
0
+It works on the following callbacks:
0
 
0
 before_validation
0
 before_validation_on_create
0
@@ -19,8 +19,6 @@ after_update
0
 after_save
0
 before_destroy
0
 after_destroy
0
-after_initialize
0
-after_find
0
 
0
 3 validations:
0
 
0
@@ -28,24 +26,22 @@ validate
0
 validate_on_create
0
 validate_on_update
0
 
0
-and 3 filters:
0
+and 1 filter:
0
 
0
 before_filter
0
-after_filter
0
-around_filter
0
 
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
 
0
-
0
 Example
0
 =======
0
 
0
 class Address < ActiveRecord::Base
0
+
0
   before_save :geolocate
0
 
0
   def geolocate
0
- if self.complete?
0
+ if complete?
0
       ...
0
     end
0
   end
0
@@ -53,6 +49,7 @@ class Address < ActiveRecord::Base
0
   def complete?
0
     street? && city? && state? && zip?
0
   end
0
+
0
 end
0
 
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
 
0
 class Address < ActiveRecord::Base
0
- before_save :geolocate, :if => :complete?
0
+
0
+ before_save :geolocate,
0
+ :if => :complete?
0
 
0
   def geolocate
0
     ...
0
@@ -75,32 +74,20 @@ class Address < ActiveRecord::Base
0
   def complete?
0
     street? && city? && state? && zip?
0
   end
0
+
0
 end
0
 
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
-
0
+The #geolocate method's single responsibility is to ... geolocate.
0
 
0
 More Examples
0
 =============
0
 
0
-after_update :send_notifications, :unless => lambda {|record| record.minor_change?}
0
-
0
-after_initialize :capitalize_title, :if => :new_record?
0
-
0
-before_create %{self.password = password.to_sha1},
0
+before_create :encrypt_password,
0
   :unless => lambda {|user| user.password_confirmation.blank?}
0
 
0
-before_create :unless => lambda {|user| user.password_confirmation.blank?} do |record|
0
- record.password = record.password.to_sha1
0
-end
0
-
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
- end
0
-end
0
-
0
+before_filter :log_in!,
0
+ :only => [:new, :create],
0
+ :unless => :logged_in?
0
 
0
 Copyright (c) 2008 Jared Carroll and Dan Croak, released under the MIT license

Comments

    No one has commented yet.