From a68225423123e7dc8ee6ed1ea8277dff75319fd0 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Thu, 11 Dec 2008 16:58:44 -0300 Subject: [PATCH] ActionItem870: notify article owner about new comments by e-mail --- app/helpers/article_helper.rb | 6 ++++-- app/models/comment.rb | 22 ++++++++++++++++++++++ app/views/comment/notifier/mail.rhtml | 14 ++++++++++++++ db/migrate/057_add_notify_comments_to_articles.rb | 11 +++++++++++ db/schema.rb | 12 +++++++----- test/functional/cms_controller_test.rb | 6 ++++++ test/unit/article_test.rb | 5 +++++ test/unit/comment_notifier_test.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 app/views/comment/notifier/mail.rhtml create mode 100644 db/migrate/057_add_notify_comments_to_articles.rb create mode 100644 test/unit/comment_notifier_test.rb diff --git a/app/helpers/article_helper.rb b/app/helpers/article_helper.rb index 168eefd..da2da4a 100644 --- a/app/helpers/article_helper.rb +++ b/app/helpers/article_helper.rb @@ -7,8 +7,10 @@ module ArticleHelper check_box(:article, :published) + content_tag('label', _('Published'), :for => 'article_published') + check_box(:article, :accept_comments) + - content_tag('label', _('Accept Comments'), :for => 'article_accept_comments') - ) + content_tag('label', _('Accept Comments'), :for => 'article_accept_comments') + + check_box(:article, :notify_comments) + + content_tag('label', _('Notify Comments'), :for => 'article_notify_comments') + ) + observe_field(:article_accept_comments, :function => "$('article_notify_comments').disabled = ! $('article_accept_comments').checked") end def cms_label_for_new_children diff --git a/app/models/comment.rb b/app/models/comment.rb index f68d4a5..23e6349 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -45,4 +45,26 @@ class Comment < ActiveRecord::Base article.comments_updated end + after_create do |comment| + if comment.article.notify_comments? + Comment::Notifier.deliver_mail(comment) + end + end + + class Notifier < ActionMailer::Base + def mail(comment) + profile = comment.article.profile + recipients profile.email + from "#{profile.environment.name} <#{profile.environment.contact_email}>" + subject _("%s - New comment in '%s'") % [profile.environment.name, comment.article.title] + body :name => (comment.author.nil? ? comment.name : comment.author.name), + :email => (comment.author.nil? ? comment.email : comment.author.email), + :title => comment.title, + :body => comment.body, + :article_url => comment.article.url, + :environment => profile.environment.name, + :url => url_for(:host => profile.environment.default_hostname, :controller => 'home') + end + end + end diff --git a/app/views/comment/notifier/mail.rhtml b/app/views/comment/notifier/mail.rhtml new file mode 100644 index 0000000..cef4b55 --- /dev/null +++ b/app/views/comment/notifier/mail.rhtml @@ -0,0 +1,14 @@ +<%= _('Name: %s') % @name %> +<%= _('e-Mail: %s') % @email %> +<%= _('Title') % @title %> +<%= _('Comment:') %> +-- + +<%= @body %> + +-- +<%= @article_url %> + +-- +<%= _('%s environment system') % @environment %> +<%= @url %> diff --git a/db/migrate/057_add_notify_comments_to_articles.rb b/db/migrate/057_add_notify_comments_to_articles.rb new file mode 100644 index 0000000..786da51 --- /dev/null +++ b/db/migrate/057_add_notify_comments_to_articles.rb @@ -0,0 +1,11 @@ +class AddNotifyCommentsToArticles < ActiveRecord::Migration + def self.up + add_column :articles, :notify_comments, :boolean, :default => false + add_column :article_versions, :notify_comments, :boolean, :default => false + end + + def self.down + remove_column :articles, :notify_comments + remove_column :article_versions, :notify_comments + end +end diff --git a/db/schema.rb b/db/schema.rb index 7777ea7..99e6360 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 56) do +ActiveRecord::Schema.define(:version => 57) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -40,6 +40,7 @@ ActiveRecord::Schema.define(:version => 56) do t.boolean "accept_comments", :default => true t.integer "reference_article_id" t.text "setting" + t.boolean "notify_comments", :default => false end create_table "articles", :force => true do |t| @@ -71,6 +72,7 @@ ActiveRecord::Schema.define(:version => 56) do t.boolean "accept_comments", :default => true t.integer "reference_article_id" t.text "setting" + t.boolean "notify_comments", :default => false end create_table "articles_categories", :id => false, :force => true do |t| @@ -79,8 +81,8 @@ ActiveRecord::Schema.define(:version => 56) do t.boolean "virtual", :default => false end - add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id" + add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" create_table "blocks", :force => true do |t| t.string "title" @@ -120,8 +122,8 @@ ActiveRecord::Schema.define(:version => 56) do t.boolean "virtual", :default => false end - add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" + add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" create_table "comments", :force => true do |t| t.string "title" @@ -188,8 +190,8 @@ ActiveRecord::Schema.define(:version => 56) do t.datetime "updated_at" end - add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" + add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" create_table "products", :force => true do |t| t.integer "enterprise_id" @@ -260,8 +262,8 @@ ActiveRecord::Schema.define(:version => 56) do t.datetime "created_at" end - add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type" + add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" create_table "tags", :force => true do |t| t.string "name" diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index a0d4b17..694c328 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -746,4 +746,10 @@ class CmsControllerTest < Test::Unit::TestCase assert_tag :tag => 'input', :attributes => {:type => 'submit', :value => 'Yes, I want.' } end + should 'display notify comments option' do + a = profile.articles.create!(:name => 'test') + get :edit, :profile => profile.identifier, :id => a.id + assert :tag => 'input', :attributes => {:name => 'article[notify_comments]', :value => 1} + end + end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index f4bfd07..3ba24e3 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -562,4 +562,9 @@ class ArticleTest < Test::Unit::TestCase assert !a.belongs_to_blog? end + should 'has comments notifier false by default' do + a = Article.new + assert !a.notify_comments? + end + end diff --git a/test/unit/comment_notifier_test.rb b/test/unit/comment_notifier_test.rb new file mode 100644 index 0000000..4eac274 --- /dev/null +++ b/test/unit/comment_notifier_test.rb @@ -0,0 +1,67 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommentNotifierTest < Test::Unit::TestCase + FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures' + CHARSET = "utf-8" + + def setup + ActionMailer::Base.delivery_method = :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + + @expected = TMail::Mail.new + @expected.set_content_type "text", "plain", { "charset" => CHARSET } + end + + should 'deliver mail after make aarticle commment' do + p = create_user('user_comment_test').person + a = Article.create!(:name => 'Article test', :profile => p, :notify_comments => true) + assert_difference ActionMailer::Base.deliveries, :count do + a.comments << Comment.new(:author => p, :title => 'test comment', :body => 'you suck!') + end + end + + should 'deliver mail to owner of article' do + p = create_user('user_comment_test').person + a = Article.create!(:name => 'Article test', :profile => p, :notify_comments => true) + a.comments << Comment.new(:author => p, :title => 'test comment', :body => 'you suck!') + sent = ActionMailer::Base.deliveries.first + assert_equal [p.email], sent.to + end + + should 'display author name in delivered mail' do + p = create_user('user_comment_test').person + a = Article.create!(:name => 'Article test', :profile => p, :notify_comments => true) + a.comments << Comment.new(:author => p, :title => 'test comment', :body => 'you suck!') + sent = ActionMailer::Base.deliveries.first + assert_match /user_comment_test/, sent.body + end + + should 'display unauthenticated author name and email in delivered mail' do + p = create_user('user_comment_test').person + a = Article.create!(:name => 'Article test', :profile => p, :notify_comments => true) + a.comments << Comment.new(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!') + sent = ActionMailer::Base.deliveries.first + assert_match /flatline/, sent.body + assert_match /flatline@invalid.com/, sent.body + end + + should 'not deliver mail if notify comments is false' do + p = create_user('user_comment_test').person + a = Article.create!(:name => 'Article test', :profile => p, :notify_comments => false) + assert_no_difference ActionMailer::Base.deliveries, :count do + a.comments << Comment.new(:author => p, :title => 'test comment', :body => 'you suck!') + end + end + + private + + def read_fixture(action) + IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") + end + + def encode(subject) + quoted_printable(subject, CHARSET) + end + +end -- libgit2 0.21.2