public
Description: Easy Akismet integration for your Rails app
Clone URL: git://github.com/jfrench/rakismet.git
Allow only/except in controller method
jfrench (author)
Sat Aug 30 10:34:51 -0700 2008
commit  7ac0c0e5ab9e50d78b8795f091c2be681bc67e1f
tree    fee236047c81761d97f0eb1b151fffc79f338270
parent  a4157d66f33081f46657bd7f91d6eb7c297ba17b
0
...
30
31
32
33
 
34
35
36
37
38
 
 
 
 
 
 
39
40
41
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
 
 
 
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
 
84
85
86
...
102
103
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
...
30
31
32
 
33
34
35
36
 
 
37
38
39
40
41
42
43
44
45
...
56
57
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
60
61
62
63
 
64
 
 
 
 
 
 
 
 
 
 
 
65
66
67
68
69
...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
0
@@ -30,12 +30,16 @@ model and a CommentsController:
0
     has_rakismet
0
   end
0
 
0
- class CommentController < ActionController::Base
0
+ class CommentsController < ActionController::Base
0
     has_rakismet
0
   end
0
 
0
-Rakismet sends the following information to the spam-hungry robots at Akismet,
0
-which means these attributes should be stored in your Comment model or
0
+
0
+Model Requirements
0
+==================
0
+
0
+Rakismet sends the following information to the spam-hungry robots at Akismet.
0
+This means these attributes should be stored in your Comment model or
0
 accessible through that class's associations.
0
 
0
   author : name submitted with the comment
0
@@ -52,35 +56,14 @@ accessible through that class's associations.
0
 
0
 user_ip, user_agent, and referer are optional; you don't have to store them,
0
 but it's a good idea. If you omit them from the Rakismet definition (see
0
-below), the +spam?+ method will look around in your request environment and
0
-find them there. This means that Rakismet is meant to be used in a synchronous
0
-fashion -- validating comments as they are submitted by the user, not placed
0
-in a queue for later spam-checking.
0
-
0
-Customizing Attributes
0
-======================
0
-
0
-If your attributes don't match those listed above, or if some of them live on
0
-other objects, you pass has_rakismet a hash mapping the default attributes to
0
-your own. You can change the names, if your comment attributes don't match the
0
-Rakismet defaults:
0
-
0
- has_rakismet :author => :commenter_name,
0
- :author_email => :commenter_email
0
+"Customizing Attributes" below), the +spam?+ method will look around in your
0
+request environment and find them there. This means that Rakismet is meant to
0
+be used in a synchronous fashion -- validating comments as they are submitted
0
+by the user, not placed in a queue for later spam-checking.
0
 
0
-You can also pass in a proc, to access associations:
0
 
0
- belongs_to :author
0
- has_rakismet :author => proc { author.name },
0
- :author_email => proc { author.email }
0
-
0
-For any attribute you don't explicitly define, Rakismet will try to find an
0
-attribute or method matching the default name. As mentioned above, you can
0
-either map user_ip, user_agent, and referer here or omit them and Rakismet
0
-will look for them in the request environment when you call +spam?+.
0
-
0
-Usage
0
-=====
0
+Basic Usage
0
+===========
0
 
0
 Rakismet provides three methods for interacting with Akismet:
0
 
0
@@ -102,4 +85,45 @@ a false positive. Likewise if they missed a spammy comment, @comment.spam!
0
 will resubmit marked as spam.
0
 
0
 
0
+Customizing Attributes
0
+======================
0
+
0
+If your attribute names don't match those listed above, or if some of them
0
+live on other objects, you pass has_rakismet a hash mapping the default
0
+attributes to your own. You can change the names, if your comment attributes
0
+don't match the defaults:
0
+
0
+ class Comment < ActiveRecord::Base
0
+ has_rakismet :author => :commenter_name,
0
+ :author_email => :commenter_email
0
+ end
0
+
0
+Or you can pass in a proc, to access associations:
0
+
0
+ class Comment < ActiveRecord::Base
0
+ belongs_to :author
0
+ has_rakismet :author => proc { author.name },
0
+ :author_email => proc { author.email }
0
+ end
0
+
0
+For any attribute you don't specify, Rakismet will try to find an attribute or
0
+method matching the default name. As mentioned above, you can explicitly map
0
+user_ip, user_agent, and referer here or omit them. If you leave them out,
0
+Rakismet will look for them in the request environment when you call +spam?+.
0
+
0
+
0
+Controller Behavior
0
+===================
0
+
0
+Most of the time you won't be checking for spam on every action defined in
0
+your controller. If you only call +spam?+ within CommentsController#create and
0
+you'd like to reduce filter overhead, has_rakismet takes :only and :except
0
+parameters that work like the standard before/around/after filter options.
0
+
0
+ class CommentsController < ActionController::Base
0
+ has_rakismet :only => :create
0
+ end
0
+
0
+
0
+==============================================================
0
 Copyright (c) 2008 Josh French, released under the MIT license
...
7
8
9
 
 
 
 
 
 
 
10
11
12
13
14
15
16
 
 
 
 
17
18
19
...
7
8
9
10
11
12
13
14
15
16
17
 
 
 
 
 
 
18
19
20
21
22
23
24
0
@@ -7,13 +7,18 @@ module Rakismet
0
       end
0
     end
0
     
0
+ def rakismet(&block)
0
+ Rakismet::Base.rakismet_binding = binding
0
+ yield
0
+ Rakismet::Base.rakismet_binding = nil
0
+ end
0
+ private :rakismet
0
+
0
     module ClassMethods
0
- def has_rakismet
0
- self.around_filter do |controller,action|
0
- Rakismet::Base.rakismet_binding = action.send(:binding)
0
- action.call
0
- Rakismet::Base.rakismet_binding = nil
0
- end
0
+ def has_rakismet(opts={})
0
+ skip_filter :rakismet # in case we're inheriting from another Rakismeted controller
0
+ opts.assert_valid_keys(:only, :except)
0
+ self.around_filter :rakismet, opts
0
       end
0
     end
0
     
...
1
2
3
 
 
 
 
 
 
 
 
4
5
6
 
7
8
 
9
10
 
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
...
1
2
 
3
4
5
6
7
8
9
10
11
12
 
13
14
 
15
16
 
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
0
@@ -1,13 +1,50 @@
0
 require File.dirname(__FILE__) + '/../spec_helper'
0
 
0
-describe stub_controller = ActionController::Base.subclass('StubController') { has_rakismet; define_method(:index, proc{}) } do
0
+class StubController < ActionController::Base
0
+ has_rakismet
0
+ def one ; end
0
+ def two ; end
0
+end
0
+
0
+describe StubController do
0
+
0
   it "should set Rakismet::Base.rakismet_binding" do
0
     Rakismet::Base.should_receive(:rakismet_binding=).twice
0
- get :index
0
+ get :one
0
   end
0
-
0
+
0
   it "should return Rakismet::Base.rakismet_binding to nil after request" do
0
- get :index
0
+ get :one
0
     Rakismet::Base.rakismet_binding.should be_nil
0
   end
0
+
0
+ it "should add around_filter" do
0
+ StubController.filter_chain.map(&:class).should include(ActionController::Filters::AroundFilter)
0
+ end
0
+end
0
+
0
+describe StubController.subclass('OnlyActions') { has_rakismet(:only => :one) } do
0
+
0
+ it "should add around filter to specified actions" do
0
+ Rakismet::Base.should_receive(:rakismet_binding=).twice
0
+ get :one
0
+ end
0
+
0
+ it "should not add around filter to unspecified actions" do
0
+ Rakismet::Base.should_not_receive(:rakismet_binding=)
0
+ get :two
0
+ end
0
+end
0
+
0
+describe StubController.subclass('ExceptActions') { has_rakismet(:except => :one) } do
0
+
0
+ it "should not add around filter to specified actions" do
0
+ Rakismet::Base.should_not_receive(:rakismet_binding=)
0
+ get :one
0
+ end
0
+
0
+ it "should add around filter to other actions" do
0
+ Rakismet::Base.should_receive(:rakismet_binding=).twice
0
+ get :two
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.