loudmouth 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -1,4 +1,5 @@
1
1
  class Loudmouth::CommentsController < ApplicationController
2
+ include ActionView::Helpers::SanitizeHelper
2
3
  layout Loudmouth.use_layout if Loudmouth.use_layout.length > 0
3
4
  before_filter :authenticate_user!, :except => [ :index ]
4
5
 
@@ -25,11 +26,20 @@ class Loudmouth::CommentsController < ApplicationController
25
26
  def create
26
27
  @topic = topic_c.find(params[topic_comment.to_sym][topic.foreign_key.to_sym])
27
28
 
28
- redirect_to :back unless validate_create(@topic)
29
+ # Run create validations
30
+ redirect_to :back and return unless validate_create(@topic)
31
+
32
+ # Sanitize comment content
33
+ params[topic_comment.to_sym][:content] = sanitize params[topic_comment.to_sym][:content]
29
34
 
30
35
  @comment = topic_comment_c.new(params[topic_comment.to_sym])
36
+
37
+ # Run comment validations
38
+ redirect_to :back and return unless validate_comment(@comment)
39
+
31
40
  @user = user_c.find(params[topic_comment.to_sym][('author_' + user.foreign_key).to_sym])
32
41
 
42
+ # Make sure the default comment text isn't being posted
33
43
  if params[topic_comment.to_sym][:content] == new_comment_content()
34
44
  flash[:error] = new_comment_content().
35
45
  redirect_to :back
@@ -48,6 +58,11 @@ class Loudmouth::CommentsController < ApplicationController
48
58
  def update
49
59
  @comment = topic_comment_c.find(params[:id])
50
60
  redirect_to :back unless validate_update(@comment)
61
+
62
+ @topic = topic_c.find(params[topic_comment.to_sym][topic.foreign_key.to_sym])
63
+
64
+ tmp_comment = topic_comment_c.new(params[topic_comment.to_sym])
65
+ redirect_to :back unless validate_comment(tmp_comment)
51
66
 
52
67
  if @comment.update_attributes(params[topic_comment.to_sym])
53
68
  flash[:success] = 'Comment successfully updated.'
@@ -60,6 +75,7 @@ class Loudmouth::CommentsController < ApplicationController
60
75
 
61
76
  def destroy
62
77
  @comment = topic_comment_c.find(params[:id])
78
+ @topic = topic_c.find(params[topic.foreign_key.to_sym])
63
79
 
64
80
  if validate_destroy(@comment)
65
81
  @comment.destroy
@@ -142,6 +158,22 @@ class Loudmouth::CommentsController < ApplicationController
142
158
  ######################
143
159
  # Validation Routines
144
160
  ######################
161
+
162
+ # Used to validate comments
163
+ # Override to provide application specific comment validation
164
+ def validate_comment(comment)
165
+ if comment.content.length < Loudmouth.min_comment_length
166
+ flash[:error] = "Comments must be at least #{Loudmouth.min_comment_length} characters in length."
167
+ return false
168
+ end
169
+
170
+ if Loudmouth.max_comment_length > 0 and
171
+ comment.content.length > Loudmouth.max_comment_length
172
+ flash[:error] = "Comments cannot be more than #{Loudmouth.max_comment_length} characters in length."
173
+ return false
174
+ end
175
+ true
176
+ end
145
177
 
146
178
  # Used to validate that the current user can comment on topic.
147
179
  # Override to provide application specific commenting validation
@@ -1,6 +1,2 @@
1
1
  module CommentsHelper
2
-
3
- def topic_comment_path(topic, comment)
4
- self.send(topic + '_comment_path', topic, comment)
5
- end
6
2
  end
@@ -1,9 +1,7 @@
1
1
  %table{ :style => 'background: #fff; border: 1px solid #ccc;' }
2
2
  %tr{ :style => 'border: 1px solid #ccc; padding: 5px;' }
3
3
  %td{ :style => 'border: 1px solid #ccc; padding: 5px;' }
4
- - cc = sanitize(cc)
5
- - cc = simple_format(comment.content)
6
- = auto_link(cc).html_safe
4
+ = simple_format auto_link sanitize comment.content
7
5
  %tr{ :style => 'border: 1px solid #ccc; padding: 5px;' }
8
6
  %th{ :style => 'background: #eee; text-align: right; font-size: 80%;' }
9
7
  == Posted #{time_ago_in_words(comment.created_at)} ago
@@ -21,8 +21,9 @@ module Loudmouth
21
21
  unless Loudmouth.treasure_map.has_key?(topic.class.name.underscore.to_sym)
22
22
  raise "loudmouth not setup for #{topic.class.name.underscore.downcase.to_sym}"
23
23
  end
24
-
25
- render :partial => 'loudmouth/comments/comment', :collection => topic.comments, :as => :comment
24
+
25
+ render :partial => 'loudmouth/comments/comment', :collection => topic.comments,
26
+ :locals => { :topic => topic }
26
27
  end
27
28
 
28
29
  def topic_comment_path(topic)
@@ -53,6 +53,15 @@ class CommentsController < Loudmouth::CommentsController
53
53
  # Validation Routines
54
54
  ######################
55
55
 
56
+ # Used to validate comments
57
+ # Override to provide application specific comment validation
58
+ # def validate_comment(comment)
59
+ # [ code to determine if the comment is valid ]
60
+ # [ set a flash message to diplay to the user in fail conditions ]
61
+ # flash[:error] = 'Comment cannot be...'
62
+ # return true/false
63
+ # end
64
+
56
65
  # Used to validate that the current user can comment on topic.
57
66
  # Override to provide application specific commenting validation
58
67
  # def validate_create(topic)
@@ -11,5 +11,15 @@ Loudmouth.setup do |config|
11
11
 
12
12
  # Allow anonymous commenting?
13
13
  config.allow_anonymous = false
14
+
15
+ # Smallest comment length allowed
16
+ # 0 for unlimited
17
+ config.min_comment_length = 2
18
+
19
+ # Largest comment length allowed
20
+ # 0 for unlimited
21
+ config.max_comment_length = 0
22
+
23
+
14
24
  end
15
25
 
data/lib/loudmouth.rb CHANGED
@@ -26,6 +26,12 @@ module Loudmouth
26
26
 
27
27
  mattr_accessor :rate_limit_cooldown
28
28
  @@rate_limit_cooldown = 1
29
+
30
+ mattr_accessor :min_comment_length
31
+ @@min_comment_length = 2
32
+
33
+ mattr_accessor :max_comment_length
34
+ @@max_comment_length = 0
29
35
 
30
36
  def self.setup
31
37
  yield self
data/loudmouth.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{loudmouth}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Peter Giacomo Lombardo}]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loudmouth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Giacomo Lombardo