public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Search Repo:
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
better comment validation and error reporting [Geoff Davis]

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2101 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Fri Sep 08 12:31:30 -0700 2006
commit  db4fc3932aaee7ee96464cf41c8ff828b7e2ed98
tree    e9e1f16aca87e4e06fb3c2b45162aff4dc4c4875
parent  7abcff71856b510c32c6f6ae9a2e82fdbefa2b2f
...
36
37
38
39
 
40
41
42
...
36
37
38
 
39
40
41
42
0
@@ -36,7 +36,7 @@ class CommentsController < ApplicationController
0
     @comment.save!
0
     redirect_to comment_preview_url(@article.hash_for_permalink(:comment => @comment, :anchor => @comment.dom_id))
0
   rescue ActiveRecord::RecordInvalid
0
- show_article_with 'errors' => @comment.errors.full_messages
0
+ show_article_with 'errors' => @comment.errors.full_messages, 'submitted' => params[:comment]
0
   rescue Article::CommentNotAllowed
0
     show_article_with 'errors' => ["Commenting has been disabled on this article"]
0
   end
...
 
 
1
2
 
 
 
 
3
4
5
...
17
18
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
21
22
...
1
2
3
 
4
5
6
7
8
9
10
...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -1,5 +1,10 @@
0
+require 'uri'
0
+
0
 class Comment < Content
0
- validates_presence_of :author, :author_ip, :article_id
0
+ validates_presence_of :author, :author_ip, :article_id, :body
0
+ validates_format_of :author_email, :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
0
+ before_validation :clean_up_author_email
0
+ before_validation :clean_up_author_url
0
   after_validation_on_create :snag_article_filter_and_site
0
   before_create :check_comment_expiration
0
   before_save :update_counter_cache
0
@@ -17,6 +22,22 @@ class Comment < Content
0
     write_attribute :approved, value
0
   end
0
 
0
+ def clean_up_author_email
0
+ if value = read_attribute(:author_email) then
0
+ write_attribute :author_email, value.strip
0
+ end
0
+ end
0
+
0
+ def clean_up_author_url
0
+ if value = read_attribute(:author_url) then
0
+ value.strip!
0
+ if not value.blank?
0
+ value = 'http://' + value unless (URI::parse(value).scheme or value[0..0] == '/')
0
+ end
0
+ write_attribute :author_url, value
0
+ end
0
+ end
0
+
0
   protected
0
     def snag_article_filter_and_site
0
       self.attributes = { :site_id => article.site_id, :filter => article.filter }
...
8
9
10
11
 
 
 
 
12
13
14
15
16
17
 
 
 
 
18
19
20
21
 
22
23
24
...
8
9
10
 
11
12
13
14
15
16
 
 
 
 
17
18
19
20
21
22
23
 
24
25
26
27
0
@@ -8,17 +8,20 @@ module Mephisto
0
         result = []
0
         context.stack do
0
           if context['message'].blank?
0
- errors = context['errors'].blank? ? '' : %Q{<ul id="comment_errors"><li>#{context['errors'].join('</li><li>')}</li></ul>}
0
+ errors = context['errors'].blank? ? '' : %Q{<ul id="comment-errors"><li>#{context['errors'].join('</li><li>')}</li></ul>}
0
+
0
+ submitted = context['submitted'] || {}
0
+ submitted.each{ |k, v| submitted[k] = CGI::escapeHTML(v) }
0
             
0
             context['form'] = {
0
- 'body' => %(<textarea id="comment_body" name="comment[body]"></textarea>),
0
- 'name' => %(<input type="text" id="comment_author" name="comment[author]" />),
0
- 'email' => %(<input type="text" id="comment_author_email" name="comment[author_email]" />),
0
- 'url' => %(<input type="text" id="comment_author_url" name="comment[author_url]" />),
0
+ 'body' => %(<textarea id="comment_body" name="comment[body]">#{submitted['body']}</textarea>),
0
+ 'name' => %(<input type="text" id="comment_author" name="comment[author]" value="#{submitted['author']}" />),
0
+ 'email' => %(<input type="text" id="comment_author_email" name="comment[author_email]" value="#{submitted['author_email']}" />),
0
+ 'url' => %(<input type="text" id="comment_author_url" name="comment[author_url]" value="#{submitted['author_url']}" />),
0
               'submit' => %(<input type="submit" value="Send" />)
0
             }
0
             
0
- result << %(<form method="post" action="#{context['article']['url']}/comment">#{[errors]+render_all(@nodelist, context)}</form>)
0
+ result << %(<form id="comment-form" method="post" action="#{context['article']['url']}/comment">#{[errors]+render_all(@nodelist, context)}</form>)
0
           else
0
             result << %(<p id="comment-message">#{context['message']}</p>)
0
           end
...
125
126
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
0
@@ -125,4 +125,38 @@ class CommentsControllerTest < Test::Unit::TestCase
0
     post :create, contents(:welcome).hash_for_permalink
0
     assert_redirected_to contents(:welcome).hash_for_permalink(:controller => 'mephisto', :action => 'show')
0
   end
0
+
0
+ def test_should_show_comments_form_with_error_messages
0
+ date = 3.days.ago
0
+ post :create, :year => date.year, :month => date.month, :day => date.day, :permalink => 'welcome-to-mephisto', :comment => {
0
+ :author_url => 'http://foo', :author_email => 'foo@example.com', :body => 'test'
0
+ }
0
+
0
+ assert_response :success
0
+
0
+ assert_tag 'ul', :attributes => { :id => 'comment-errors' }
0
+ assert_tag 'form', :attributes => { :id => 'comment-form' }
0
+ assert_no_tag 'p', :attributes => { :id => 'comment-message' }
0
+ assert_tag :tag => 'form', :descendant => {
0
+ :tag => 'input', :attributes => { :type => 'text', :id => 'comment_author', :name => 'comment[author]' } }
0
+ assert_tag :tag => 'form', :descendant => {
0
+ :tag => 'input', :attributes => { :type => 'text', :id => 'comment_author_url', :name => 'comment[author_url]', :value => 'http://foo' } }
0
+ assert_tag :tag => 'form', :descendant => {
0
+ :tag => 'input', :attributes => { :type => 'text', :id => 'comment_author_email', :name => 'comment[author_email]', :value => 'foo@example.com' } }
0
+ assert_tag :tag => 'form', :descendant => {
0
+ :tag => 'textarea', :attributes => { :id => 'comment_body', :name => 'comment[body]' }, :content => 'test' }
0
+ end
0
+
0
+ def test_should_show_comments_message_on_article_not_accepting_comments
0
+ date = 3.days.ago
0
+ post :create, :year => contents(:about).published_at.year, :month => contents(:about).published_at.month, :day => contents(:about).published_at.day, :permalink => contents(:about).permalink, :comment => {
0
+ :author_url => 'http://foo', :author_email => 'foo@example.com', :body => 'test'
0
+ }
0
+
0
+ assert_response :success
0
+
0
+ assert_no_tag 'ul', :attributes => { :id => 'comment-errors' }
0
+ assert_no_tag 'form', :attributes => { :id => 'comment-form' }
0
+ assert_no_tag 'p', :attributes => { :id => 'comment-message' }
0
+ end
0
 end
...
63
64
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
0
@@ -63,4 +63,35 @@ class CommentTest < Test::Unit::TestCase
0
       contents(:welcome).reload
0
     end
0
   end
0
+
0
+ def test_should_clean_up_email_and_url
0
+ comments = contents(:welcome).comments
0
+ options = {:body => 'test', :author => 'bob', :author_ip => '127.0.0.1', :filter => 'textile_filter'}
0
+ comment = comments.build options.merge(:author_email => ' bob@example.com ')
0
+ assert_valid comment
0
+ assert_equal 'bob@example.com', comment.author_email
0
+ comment = comments.build options.merge(:author_url => ' ')
0
+ assert_valid comment
0
+ assert_equal '', comment.author_url
0
+ comment = comments.build options.merge(:author_url => ' /foo ')
0
+ assert_valid comment
0
+ assert_equal '/foo', comment.author_url
0
+ comment = comments.build options.merge(:author_url => ' http://example.com ')
0
+ assert_valid comment
0
+ assert_equal 'http://example.com', comment.author_url
0
+ comment = comments.build options.merge(:author_url => ' example.com ')
0
+ assert_valid comment
0
+ assert_equal 'http://example.com', comment.author_url
0
+ end
0
+
0
+ def test_should_validate_emails
0
+ comments = contents(:welcome).comments
0
+ options = {:body => 'test', :author => 'bob', :author_ip => '127.0.0.1', :filter => 'textile_filter'}
0
+ comment = comments.build options.merge(:author_email => 'bob@example.com')
0
+ assert_valid comment
0
+ comment = comments.build options.merge(:author_email => 'bobexample.com')
0
+ assert !comment.valid?
0
+ comment = comments.build options.merge(:author_email => 'bob@example')
0
+ assert !comment.valid?
0
+ end
0
 end
...
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
...
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
0
@@ -4,25 +4,25 @@ class MembershipTest < Test::Unit::TestCase
0
   fixtures :memberships, :users, :sites
0
 
0
   def test_should_find_user_sites
0
- assert_models_equal [sites(:hostess), sites(:first)], users(:arthur).sites
0
+ assert_models_equal [sites(:hostess), sites(:first)].collect(&:id).sort, users(:arthur).sites.collect(&:id).sort
0
   end
0
   
0
   def test_should_find_site_members
0
- assert_models_equal [users(:arthur), users(:quentin)], sites(:first).members
0
+ assert_models_equal [users(:arthur), users(:quentin)].collect(&:id).sort, sites(:first).members.collect(&:id).sort
0
   end
0
   
0
   def test_should_find_site_admins
0
- assert_models_equal [users(:arthur), users(:quentin)], sites(:first).admins
0
+ assert_models_equal [users(:arthur), users(:quentin)].collect(&:id).sort, sites(:first).admins.collect(&:id).sort
0
     assert_models_equal [users(:quentin)], sites(:hostess).admins
0
   end
0
   
0
   def test_should_find_all_site_users
0
- assert_models_equal [users(:arthur), users(:quentin)], User.find_all_by_site(sites(:first))
0
- assert_models_equal [users(:arthur), users(:quentin)], sites(:first).users
0
+ assert_models_equal [users(:arthur), users(:quentin)].collect(&:id).sort, User.find_all_by_site(sites(:first)).collect(&:id).sort
0
+ assert_models_equal [users(:arthur), users(:quentin)].collect(&:id).sort, sites(:first).users.collect(&:id).sort
0
   end
0
   
0
   def test_should_find_all_site_users_with_deleted
0
- assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)], User.find_all_by_site_with_deleted(sites(:first))
0
- assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)], sites(:first).users_with_deleted
0
+ assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)].collect(&:id).sort, User.find_all_by_site_with_deleted(sites(:first)).collect(&:id).sort
0
+ assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)].collect(&:id).sort, sites(:first).users_with_deleted.collect(&:id).sort
0
   end
0
 end
...
66
67
68
69
 
70
71
72
...
66
67
68
 
69
70
71
72
0
@@ -66,7 +66,7 @@
0
       {{ form.name }}<label class="text" for="comment_author">Name (Required)</label><br />
0
       {{ form.email }}<label class="text" for="comment_author_email">Email (Required)</label><br />
0
       {{ form.url }}<label class="text" for="comment_author_url">Website</label><br />
0
- <p><textarea name="comment[body]" class="commentbox"></textarea></p>
0
+ <p>{{ form.body }}</p>
0
       <div class="formactions">
0
         <input type="submit" value="Post comment" class="submit" />
0
       </div>

Comments

    No one has commented yet.