From 8203757d1ef3c35bf1be31e4fc95b1e2006c6059 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 27 Apr 2016 12:22:37 +0800 Subject: [PATCH] Fix subscribe system for rails5 --- app/assets/stylesheets/blogs.scss | 1 - app/mailers/comment_mailer.rb | 2 +- app/mailers/post_mailer.rb | 2 +- app/models/comment.rb | 4 ++-- app/models/post.rb | 2 +- app/views/comment_mailer/{new.html.slim => born.html.slim} | 0 app/views/comment_mailer/{new.text.erb => born.text.erb} | 0 app/views/post_mailer/{new.html.slim => born.html.slim} | 0 app/views/post_mailer/{new.text.erb => born.text.erb} | 0 app/views/unsubscribes/create.html.slim | 2 -- app/workers/new_comment_worker.rb | 2 +- app/workers/new_post_worker.rb | 2 +- app/workers/new_reply_post_worker.rb | 1 - config/environments/development.rb | 2 +- 14 files changed, 8 insertions(+), 12 deletions(-) rename app/views/comment_mailer/{new.html.slim => born.html.slim} (100%) rename app/views/comment_mailer/{new.text.erb => born.text.erb} (100%) rename app/views/post_mailer/{new.html.slim => born.html.slim} (100%) rename app/views/post_mailer/{new.text.erb => born.text.erb} (100%) delete mode 100644 app/views/unsubscribes/create.html.slim diff --git a/app/assets/stylesheets/blogs.scss b/app/assets/stylesheets/blogs.scss index 7523144..01e1332 100644 --- a/app/assets/stylesheets/blogs.scss +++ b/app/assets/stylesheets/blogs.scss @@ -48,7 +48,6 @@ } .recent-title { - border-bottom: 1px solid #DCDCDC; margin-bottom: 1rem; } diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb index 24874d0..202582a 100644 --- a/app/mailers/comment_mailer.rb +++ b/app/mailers/comment_mailer.rb @@ -3,7 +3,7 @@ class CommentMailer < ActionMailer::Base default from: "no-reply@#{domain}" - def new(comment_id, to) + def born(comment_id, to) @comment = Comment.find(comment_id) mail to: to, subject: '博主, 你的博客有新的评论' end diff --git a/app/mailers/post_mailer.rb b/app/mailers/post_mailer.rb index 390bed4..652802c 100644 --- a/app/mailers/post_mailer.rb +++ b/app/mailers/post_mailer.rb @@ -3,7 +3,7 @@ class PostMailer < ActionMailer::Base default from: "no-reply@#{domain}" - def new(post_id, to) + def born(post_id, to) @post = Post.find(post_id) mail to: to, subject: '客官, 新博客来了' end diff --git a/app/models/comment.rb b/app/models/comment.rb index 1c5d459..49be5e4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,10 +7,10 @@ class Comment < ApplicationRecord validates :content, presence: true def reply_emails - Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list + Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list - [ ENV['ADMIN_USER'] ] end - after_create do + after_commit on: :create do if ENV['MAIL_SERVER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/ Rails.logger.info 'comment created, comment worker start' NewCommentWorker.perform_async(self.id.to_s, ENV['ADMIN_USER']) diff --git a/app/models/post.rb b/app/models/post.rb index d5fe8c4..dfcc06f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -8,7 +8,7 @@ class Post < ActiveRecord::Base validates :title, :presence=>true, :uniqueness=> true validates :content, :presence=>true, :length => { :minimum=> 30 } - after_create do + after_commit on: :create do if ENV['MAIL_SERVER'].present? NewPostWorker.perform_async(self.id.to_s) end diff --git a/app/views/comment_mailer/new.html.slim b/app/views/comment_mailer/born.html.slim similarity index 100% rename from app/views/comment_mailer/new.html.slim rename to app/views/comment_mailer/born.html.slim diff --git a/app/views/comment_mailer/new.text.erb b/app/views/comment_mailer/born.text.erb similarity index 100% rename from app/views/comment_mailer/new.text.erb rename to app/views/comment_mailer/born.text.erb diff --git a/app/views/post_mailer/new.html.slim b/app/views/post_mailer/born.html.slim similarity index 100% rename from app/views/post_mailer/new.html.slim rename to app/views/post_mailer/born.html.slim diff --git a/app/views/post_mailer/new.text.erb b/app/views/post_mailer/born.text.erb similarity index 100% rename from app/views/post_mailer/new.text.erb rename to app/views/post_mailer/born.text.erb diff --git a/app/views/unsubscribes/create.html.slim b/app/views/unsubscribes/create.html.slim deleted file mode 100644 index 8471bcc..0000000 --- a/app/views/unsubscribes/create.html.slim +++ /dev/null @@ -1,2 +0,0 @@ -h1 Unsubscribes#create -p Find me in app/views/unsubscribes/create.html.slim diff --git a/app/workers/new_comment_worker.rb b/app/workers/new_comment_worker.rb index aeef92f..361d8ff 100644 --- a/app/workers/new_comment_worker.rb +++ b/app/workers/new_comment_worker.rb @@ -7,7 +7,7 @@ class NewCommentWorker def perform(comment_id, to) logger.info "new comment mail: #{comment_id}" - CommentMailer.new(comment_id.to_s, to).deliver + CommentMailer.born(comment_id.to_s, to).deliver end end diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index 6bde5a5..2cc6141 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -8,7 +8,7 @@ class NewPostWorker def perform(post_id) Subscribe.subscribe_list.each do |email| Rails.logger.info "new post #{post_id}, send to #{email}" - PostMailer.new(post_id.to_s, email).deliver + PostMailer.born(post_id.to_s, email).deliver end end end diff --git a/app/workers/new_reply_post_worker.rb b/app/workers/new_reply_post_worker.rb index c27fb6a..9fc7a5b 100644 --- a/app/workers/new_reply_post_worker.rb +++ b/app/workers/new_reply_post_worker.rb @@ -9,7 +9,6 @@ class NewReplyPostWorker logger.info "new reply mail" comment = Comment.find(comment_id.to_s) comment.reply_emails.each do |email| - next if email == ENV['ADMIN_USER'] logger.info "new reply mail to #{email}" CommentMailer.reply(comment_id.to_s, email).deliver end diff --git a/config/environments/development.rb b/config/environments/development.rb index 9817503..5fc0ff7 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -24,7 +24,7 @@ WBlog::Application.configure do # number of complex assets. config.assets.debug = true - config.middleware.insert_before 0, "Rack::Cors" do + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [:get, :post, :options]