commontator 1.0.6 → 1.1.0

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.
Files changed (33) hide show
  1. data/app/controllers/commontator/application_controller.rb +5 -2
  2. data/app/controllers/commontator/application_controller.rb~ +5 -3
  3. data/app/controllers/commontator/comments_controller.rb +1 -4
  4. data/app/controllers/commontator/comments_controller.rb~ +4 -5
  5. data/app/controllers/commontator/subscriptions_controller.rb +0 -1
  6. data/app/controllers/commontator/subscriptions_controller.rb~ +1 -1
  7. data/app/controllers/commontator/threads_controller.rb +2 -2
  8. data/app/controllers/commontator/threads_controller.rb~ +3 -4
  9. data/app/mailers/commontator/subscriptions_mailer.rb +9 -13
  10. data/app/mailers/commontator/subscriptions_mailer.rb~ +9 -13
  11. data/app/models/commontator/comment_observer.rb +12 -0
  12. data/app/models/commontator/comment_observer.rb~ +13 -0
  13. data/app/models/commontator/thread.rb +4 -0
  14. data/app/models/commontator/thread.rb~ +5 -1
  15. data/app/views/commontator/subscriptions_mailer/{comment_created_email.html.erb → comment_created.html.erb} +0 -0
  16. data/config/initializers/commontator.rb +15 -12
  17. data/config/initializers/commontator.rb~ +15 -12
  18. data/lib/commontator.rb +3 -2
  19. data/lib/commontator.rb~ +4 -2
  20. data/lib/commontator/engine.rb~ +6 -0
  21. data/lib/commontator/version.rb +1 -1
  22. data/lib/commontator/version.rb~ +1 -1
  23. data/spec/app/mailers/commontator/subscriptions_mailer_spec.rb +9 -17
  24. data/spec/app/mailers/commontator/subscriptions_mailer_spec.rb~ +10 -17
  25. data/spec/app/models/commontator/comment_observer_spec.rb +27 -0
  26. data/spec/app/models/commontator/comment_observer_spec.rb~ +27 -0
  27. data/spec/dummy/config/initializers/commontator.rb +14 -9
  28. data/spec/dummy/config/initializers/commontator.rb~ +15 -9
  29. data/spec/dummy/db/development.sqlite3 +0 -0
  30. data/spec/dummy/db/test.sqlite3 +0 -0
  31. data/spec/dummy/log/development.log +926 -0
  32. data/spec/dummy/log/test.log +32956 -0
  33. metadata +10 -3
@@ -2,6 +2,8 @@ module Commontator
2
2
  class ApplicationController < ActionController::Base
3
3
  before_filter :get_user
4
4
 
5
+ cattr_accessor :commontable_url
6
+
5
7
  rescue_from SecurityTransgression, :with => lambda { head(:forbidden) }
6
8
 
7
9
  protected
@@ -15,10 +17,11 @@ module Commontator
15
17
  @thread = params[:thread_id].blank? ? \
16
18
  Commontator::Thread.find(params[:id]) : \
17
19
  Commontator::Thread.find(params[:thread_id])
20
+ raise SecurityTransgression if @thread.commontable.nil?
18
21
  end
19
22
 
20
- def get_commontable_url
21
- @commontable_url = @thread.config.commontable_url_proc.call(main_app, @thread.commontable)
23
+ def set_commontable_url
24
+ self.commontable_url = @thread.config.commontable_url_proc.call(main_app, @thread.commontable)
22
25
  end
23
26
  end
24
27
  end
@@ -2,7 +2,7 @@ module Commontator
2
2
  class ApplicationController < ActionController::Base
3
3
  before_filter :get_user
4
4
 
5
- class SecurityTransgression < StandardError; end
5
+ cattr_accessor :commontable_url
6
6
 
7
7
  rescue_from SecurityTransgression, :with => lambda { head(:forbidden) }
8
8
 
@@ -17,10 +17,12 @@ module Commontator
17
17
  @thread = params[:thread_id].blank? ? \
18
18
  Commontator::Thread.find(params[:id]) : \
19
19
  Commontator::Thread.find(params[:thread_id])
20
+ raise SecurityTransgression if @thread.commontable.nil?
21
+ set_commontable_url
20
22
  end
21
23
 
22
- def get_commontable_url
23
- @commontable_url = @thread.config.commontable_url_proc.call(main_app, @thread.commontable)
24
+ def set_commontable_url
25
+ self.commontable_url = @thread.config.commontable_url_proc.call(main_app, @thread.commontable)
24
26
  end
25
27
  end
26
28
  end
@@ -2,7 +2,7 @@ module Commontator
2
2
  class CommentsController < ApplicationController
3
3
  before_filter :get_thread, :only => [:new, :create]
4
4
  before_filter :get_comment_and_thread, :except => [:new, :create]
5
- before_filter :get_commontable_url, :only => :create
5
+ before_filter :set_commontable_url, :only => :create
6
6
 
7
7
  # GET /1/comments/new
8
8
  def new
@@ -29,9 +29,6 @@ module Commontator
29
29
 
30
30
  respond_to do |format|
31
31
  if @comment.save
32
- @thread.subscribe(@user) if @thread.config.auto_subscribe_on_comment
33
- @thread.add_unread_except_for(@user)
34
- SubscriptionsMailer.comment_created_email(@comment, @commontable_url)
35
32
  format.html { redirect_to @thread }
36
33
  format.js
37
34
  else
@@ -2,7 +2,7 @@ module Commontator
2
2
  class CommentsController < ApplicationController
3
3
  before_filter :get_thread, :only => [:new, :create]
4
4
  before_filter :get_comment_and_thread, :except => [:new, :create]
5
- before_filter :get_commontable_url, :only => :create
5
+ before_filter :set_commontable_url, :only => :show
6
6
 
7
7
  # GET /1/comments/new
8
8
  def new
@@ -21,6 +21,7 @@ module Commontator
21
21
 
22
22
  # POST /1/comments
23
23
  def create
24
+ set_commontable_url
24
25
  @comment = Comment.new(params[:comment])
25
26
  @comment.thread = @thread
26
27
  @comment.creator = @user
@@ -29,9 +30,6 @@ module Commontator
29
30
 
30
31
  respond_to do |format|
31
32
  if @comment.save
32
- @thread.subscribe(@user) if @thread.config.auto_subscribe_on_comment
33
- @thread.add_unread_except_for(@user)
34
- SubscriptionsMailer.comment_created_email(@comment, @commontable_url)
35
33
  format.html { redirect_to @thread }
36
34
  format.js
37
35
  else
@@ -60,7 +58,8 @@ module Commontator
60
58
  format.html { redirect_to @thread }
61
59
  format.js
62
60
  else
63
- format.html { render action: "edit" }
61
+ format.html { redirect_to @thread }
62
+ format.js { render :edit }
64
63
  end
65
64
  end
66
65
  end
@@ -1,7 +1,6 @@
1
1
  module Commontator
2
2
  class SubscriptionsController < ApplicationController
3
3
  before_filter :get_thread
4
- before_filter :get_commontable_url
5
4
 
6
5
  # POST /1/subscribe
7
6
  def create
@@ -21,7 +21,7 @@ module Commontator
21
21
  def destroy
22
22
  raise SecurityTransgression unless @thread.can_subscribe?(@user)
23
23
 
24
- @thread.errors.add(:base, "You are not subscribed to this thread") \
24
+ @thread.errors.add(:subscription, "You are not subscribed to this thread") \
25
25
  unless @thread.unsubscribe(@user)
26
26
 
27
27
  respond_to do |format|
@@ -1,14 +1,14 @@
1
1
  module Commontator
2
2
  class ThreadsController < ApplicationController
3
3
  before_filter :get_thread
4
- before_filter :get_commontable_url, :only => :show
4
+ before_filter :set_commontable_url, :only => :show
5
5
 
6
6
  # GET /threads/1
7
7
  def show
8
8
  commontator_thread_show(@thread.commontable)
9
9
 
10
10
  respond_to do |format|
11
- format.html { redirect_to @commontable_url }
11
+ format.html { redirect_to commontable_url }
12
12
  format.js
13
13
  end
14
14
  end
@@ -1,14 +1,13 @@
1
1
  module Commontator
2
2
  class ThreadsController < ApplicationController
3
3
  before_filter :get_thread
4
- before_filter :get_commontable_url, :only => :show
5
4
 
6
5
  # GET /threads/1
7
6
  def show
8
7
  commontator_thread_show(@thread.commontable)
9
8
 
10
9
  respond_to do |format|
11
- format.html { redirect_to @commontable_url }
10
+ format.html { redirect_to commontable_url }
12
11
  format.js
13
12
  end
14
13
  end
@@ -17,7 +16,7 @@ module Commontator
17
16
  def close
18
17
  raise SecurityTransgression unless @thread.can_be_edited_by?(@user)
19
18
 
20
- @thread.errors.add(:base, "This thread has already been closed") \
19
+ @thread.errors.add(:base, 'This thread has already been closed.') \
21
20
  unless @thread.close(@user)
22
21
 
23
22
  respond_to do |format|
@@ -30,7 +29,7 @@ module Commontator
30
29
  def reopen
31
30
  raise SecurityTransgression unless @thread.can_be_edited_by?(@user)
32
31
 
33
- @thread.errors.add(:base, "This thread is not closed") \
32
+ @thread.errors.add(:base, 'This thread is not closed.') \
34
33
  unless @thread.reopen
35
34
 
36
35
  respond_to do |format|
@@ -3,27 +3,21 @@ module Commontator
3
3
  include SharedHelper
4
4
  include ThreadsHelper
5
5
 
6
- def comment_created_email(comment, commontable_url)
7
- setup_variables(comment, commontable_url)
6
+ def comment_created(comment, recipients)
7
+ setup_variables(comment, recipients)
8
8
 
9
- mail(:bcc => @bcc, :subject => @subject) \
10
- unless @bcc.empty?
9
+ mail :bcc => @bcc,
10
+ :from => @from,
11
+ :subject => @subject
11
12
  end
12
13
 
13
14
  protected
14
15
 
15
- def setup_variables(comment, commontable_url)
16
-
16
+ def setup_variables(comment, recipients)
17
17
  @comment = comment
18
18
  @thread = @comment.thread
19
19
  @creator = @comment.creator
20
20
 
21
- @bcc = @thread.subscribers.reject{|s| !s.commontator_config.subscription_emails || \
22
- s == @creator} \
23
- .collect{|s| commontator_email(s)}
24
-
25
- return if @bcc.empty?
26
-
27
21
  @commontable = @thread.commontable
28
22
  @config = @thread.config
29
23
 
@@ -33,7 +27,7 @@ protected
33
27
  @commontable_name = commontable_name(@thread)
34
28
  @commontable_id = commontable_id(@thread).to_s
35
29
 
36
- @commontable_url = commontable_url
30
+ @commontable_url = ApplicationController.commontable_url
37
31
 
38
32
  params = Hash.new
39
33
  params[:comment] = @comment
@@ -47,6 +41,8 @@ protected
47
41
  params[:commontable_id] = @commontable_id
48
42
  params[:commontable_url] = @commontable_url
49
43
 
44
+ @bcc = recipients.collect{|s| commontator_email(s)}
45
+ @from = @config.subscription_email_from_proc.call(params)
50
46
  @subject = @config.subscription_email_subject_proc.call(params)
51
47
  end
52
48
  end
@@ -3,27 +3,21 @@ module Commontator
3
3
  include SharedHelper
4
4
  include ThreadsHelper
5
5
 
6
- def comment_created_email(comment, commontable_url)
7
- setup_variables(comment, commontable_url)
6
+ def comment_created(comment, recipients)
7
+ setup_variables(comment, recipients)
8
8
 
9
- mail(:bcc => @bcc, :subject => @subject) \
10
- unless @bcc.empty?
9
+ mail :bcc => @bcc,
10
+ :from => @from,
11
+ :subject => @subject)
11
12
  end
12
13
 
13
14
  protected
14
15
 
15
- def setup_variables(comment, commontable_url)
16
-
16
+ def setup_variables(comment, recipients)
17
17
  @comment = comment
18
18
  @thread = @comment.thread
19
19
  @creator = @comment.creator
20
20
 
21
- @bcc = @thread.subscribers.reject{|s| s == @creator || \
22
- !s.commontator_config.subscription_emails} \
23
- .collect{|s| commontator_email(s)}
24
-
25
- return if @bcc.empty?
26
-
27
21
  @commontable = @thread.commontable
28
22
  @config = @thread.config
29
23
 
@@ -33,7 +27,7 @@ protected
33
27
  @commontable_name = commontable_name(@thread)
34
28
  @commontable_id = commontable_id(@thread).to_s
35
29
 
36
- @commontable_url = commontable_url
30
+ @commontable_url = ApplicationController.commontable_url
37
31
 
38
32
  params = Hash.new
39
33
  params[:comment] = @comment
@@ -47,6 +41,8 @@ protected
47
41
  params[:commontable_id] = @commontable_id
48
42
  params[:commontable_url] = @commontable_url
49
43
 
44
+ @bcc = recipients.collect{|s| commontator_email(s)}
45
+ @from = @config.subscription_email_from_proc.call(params)
50
46
  @subject = @config.subscription_email_subject_proc.call(params)
51
47
  end
52
48
  end
@@ -0,0 +1,12 @@
1
+ module Commontator
2
+ class CommentObserver < ActiveRecord::Observer
3
+ def after_create(comment)
4
+ thread = comment.thread
5
+ thread.subscribe(comment.creator) if thread.config.auto_subscribe_on_comment
6
+ thread.add_unread_except_for(comment.creator)
7
+ recipients = thread.active_subscribers.reject{|s| s == comment.creator}
8
+ SubscriptionsMailer.comment_created(comment, recipients).deliver \
9
+ unless recipients.empty?
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Commontator
2
+ class CommentObserver < ActiveRecord::Observer
3
+ def after_create(comment)
4
+ thread = comment.thread
5
+ thread.subscribe(comment.creator) if thread.config.auto_subscribe_on_comment
6
+ thread.add_unread_except_for(comment.creator)
7
+ recipients = thread.active_subscribers.reject{|s| s == comment.creator}
8
+ pp recipients
9
+ SubscriptionsMailer.comment_created(comment, recipients).deliver \
10
+ unless recipients.empty?
11
+ end
12
+ end
13
+ end
@@ -23,6 +23,10 @@ module Commontator
23
23
  subscriptions.collect{|s| s.subscriber}
24
24
  end
25
25
 
26
+ def active_subscribers
27
+ subscribers.select{|s| s.commontator_config.subscription_email_enable_proc.call(s)}
28
+ end
29
+
26
30
  def subscription_for(subscriber)
27
31
  return nil if subscriber.nil?
28
32
  Subscription.find_by_thread_id_and_subscriber_id_and_subscriber_type(self.id, subscriber.id, subscriber.class.name)
@@ -15,7 +15,7 @@ module Commontator
15
15
  end
16
16
 
17
17
  def ordered_comments
18
- (!commontable.blank? && config.comments_can_be_voted_on && config.comments_ordered_by_votes) ? \
18
+ (!commontable.blank? && config.can_vote_on_comments && config.comments_ordered_by_votes) ? \
19
19
  comments.order("cached_votes_down - cached_votes_up") : comments
20
20
  end
21
21
 
@@ -23,6 +23,10 @@ module Commontator
23
23
  subscriptions.collect{|s| s.subscriber}
24
24
  end
25
25
 
26
+ def active_subscribers
27
+ subscribers.select{|s| s.commontator_config.subscription_emails}
28
+ end
29
+
26
30
  def subscription_for(subscriber)
27
31
  return nil if subscriber.nil?
28
32
  Subscription.find_by_thread_id_and_subscriber_id_and_subscriber_type(self.id, subscriber.id, subscriber.class.name)
@@ -8,14 +8,12 @@ Commontator.configure do |config|
8
8
  # Default: 'current_user'
9
9
  config.current_user_method = 'current_user'
10
10
 
11
- # Proc that is called after any javascript runs (e.g. to clear flash)
12
- # It is passed the 'self' object from the view template, so you should be able to
11
+ # Proc that is called after any javascript runs (e.g. to display/clear flash messages)
12
+ # It is passed the view_context object (self from the view template), so you should be able to
13
13
  # access anything you normally could in a view template (by using, e.g. view.flash)
14
14
  # Should return a string containing JS to be appended to all Commontator JS responses
15
- # Default: lambda { |view| '$("#error_explanation").remove();' }
16
- config.javascript_proc = lambda { |view| '$("#attention").html("' + \
17
- escape_javascript(render(:partial => 'shared/attention')) + \
18
- '");' }
15
+ # Default: lambda { |view| '$("#error_explanation").hide();' }
16
+ config.javascript_proc = lambda { |view| '$("#error_explanation").hide();' }
19
17
 
20
18
 
21
19
  # User (acts_as_commontator) Configuration
@@ -28,11 +26,6 @@ Commontator.configure do |config|
28
26
  # If enabled, the link will point to the comment creator's show page
29
27
  # Default: false
30
28
  config.user_name_clickable = false
31
-
32
- # Whether automated emails are sent to the user whenever
33
- # a comment is posted on a thread they subscribe to
34
- # Default: true
35
- config.subscription_emails = true
36
29
 
37
30
  # The method that returns the user's email address
38
31
  # Default: 'email'
@@ -47,6 +40,11 @@ Commontator.configure do |config|
47
40
  # Note: user can be nil
48
41
  # Default: lambda { |user| false } (no admins)
49
42
  config.user_admin_proc = lambda { |user| false }
43
+
44
+ # Proc called with user as argument that returns true
45
+ # if the user should receive subscription emails
46
+ # Default: lambda { |user| true } (always receive subscription emails)
47
+ config.subscription_email_enable_proc = lambda { |user| true }
50
48
 
51
49
 
52
50
  # Commontable (acts_as_commontable) Configuration
@@ -146,8 +144,13 @@ Commontator.configure do |config|
146
144
  # Main application's routes can be accessed using main_app object
147
145
  # Default: lambda { |main_app, commontable| main_app.polymorphic_url(commontable) }
148
146
  config.commontable_url_proc = lambda { |main_app, commontable| main_app.polymorphic_url(commontable) }
147
+
148
+ # Proc that returns the subscription email 'from' address
149
+ # Default:
150
+ # lambda { |params| 'no-reply@example.com' }
151
+ config.subscription_email_from_proc = lambda { |params| 'no-reply@example.com' }
149
152
 
150
- # Proc that returns the subscription email subject string
153
+ # Proc that returns the subscription email 'subject' string
151
154
  # Default:
152
155
  # lambda do |params|
153
156
  # "#{params[:creator_name]} #{params[:config].comment_create_verb_past} a " + \
@@ -8,14 +8,12 @@ Commontator.configure do |config|
8
8
  # Default: 'current_user'
9
9
  config.current_user_method = 'current_user'
10
10
 
11
- # Proc that is called after any javascript runs (e.g. to clear flash)
12
- # It is passed the 'self' object from the view template, so you should be able to
11
+ # Proc that is called after any javascript runs (e.g. to display/clear flash messages)
12
+ # It is passed the view_context object (self from the view template), so you should be able to
13
13
  # access anything you normally could in a view template (by using, e.g. view.flash)
14
14
  # Should return a string containing JS to be appended to all Commontator JS responses
15
15
  # Default: lambda { |view| '$("#error_explanation").remove();' }
16
- config.javascript_proc = lambda { |view| '$("#attention").html("' + \
17
- escape_javascript(render(:partial => 'shared/attention')) + \
18
- '");' }
16
+ config.javascript_proc = lambda { |view| '$("#error_explanation").hide();' }
19
17
 
20
18
 
21
19
  # User (acts_as_commontator) Configuration
@@ -24,15 +22,10 @@ Commontator.configure do |config|
24
22
  # Default: 'Anonymous'
25
23
  config.user_missing_name = 'Anonymous'
26
24
 
27
- # Whether the user's name is clickable in the comment view
25
+ # Whether the comment creator's name is clickable in the comment view
28
26
  # If enabled, the link will point to the comment creator's show page
29
27
  # Default: false
30
28
  config.user_name_clickable = false
31
-
32
- # Whether automated emails are sent to the user whenever
33
- # a comment is posted on a thread they subscribe to
34
- # Default: true
35
- config.subscription_emails = true
36
29
 
37
30
  # The method that returns the user's email address
38
31
  # Default: 'email'
@@ -47,6 +40,11 @@ Commontator.configure do |config|
47
40
  # Note: user can be nil
48
41
  # Default: lambda { |user| false } (no admins)
49
42
  config.user_admin_proc = lambda { |user| false }
43
+
44
+ # Proc called with user as argument that returns true
45
+ # if the user should receive subscription emails
46
+ # Default: lambda { |user| true } (always receive subscription emails)
47
+ config.subscription_email_enable_proc = lambda { |user| true }
50
48
 
51
49
 
52
50
  # Commontable (acts_as_commontable) Configuration
@@ -146,8 +144,13 @@ Commontator.configure do |config|
146
144
  # Main application's routes can be accessed using main_app object
147
145
  # Default: lambda { |main_app, commontable| main_app.polymorphic_url(commontable) }
148
146
  config.commontable_url_proc = lambda { |main_app, commontable| main_app.polymorphic_url(commontable) }
147
+
148
+ # Proc that returns the subscription email 'from' address
149
+ # Default:
150
+ # lambda { |params| 'no-reply@example.com' }
151
+ config.subscription_email_from_proc = lambda { |params| 'no-reply@example.com' }
149
152
 
150
- # Proc that returns the subscription email subject string
153
+ # Proc that returns the subscription email 'subject' string
151
154
  # Default:
152
155
  # lambda do |params|
153
156
  # "#{params[:creator_name]} #{params[:config].comment_create_verb_past} a " + \