订阅功能, 由于 sendcloud 的问题未测试

This commit is contained in:
yafeilee 2014-04-01 17:09:10 +08:00
parent 23f07094bf
commit 2ec5f8e82a
7 changed files with 62 additions and 1 deletions

View File

@ -26,6 +26,8 @@ gem 'nokogiri'
gem 'angularjs-rails'
gem 'figaro'
gem 'rqrcode-with-patches', require: 'rqrcode'
gem 'sidekiq'
gem 'rest-client'
group :development do
gem 'quiet_assets'
@ -39,6 +41,7 @@ group :test do
gem 'capybara'
gem 'mongoid-rspec', :require => false
gem 'database_cleaner'
gem 'rspec-sidekiq'
end
group :test, :development do

View File

@ -9,7 +9,7 @@ class Comment
belongs_to :post
validates :name, presence: true
validates :email, confirmation: true,:format => /@/
validates :email, presence: true,:format => /@/
validates :content, presence: true
validates_presence_of :post_id
end

View File

@ -22,6 +22,12 @@ class Post
validates :content, :presence=>true, :length => { :minimum=> 30 }
validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] }
after_create do
unless ENV['SENDCLOUD_USER']
NewPostWorker.perform_async(self.title, Subscribe.subscribe_list)
end
end
def content_html
self.class.render_html(self.content)
end

11
app/models/subscribe.rb Normal file
View File

@ -0,0 +1,11 @@
class Subscribe
include Mongoid::Document
field :email, type: String
field :enable, type: Mongoid::Boolean, default: true
validates :email, presence: true, format: /@/
def self.subscribe_list
Subscribe.all.map(&:email).join(";")
end
end

View File

@ -0,0 +1,28 @@
class NewPostWorker
include Sidekiq::Worker
def perform(title, to)
send_mail(title, to)
end
def send_mail(title, to)
response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
{
:api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org",
:api_key => ENV['SENDCLOUD_PASSWORD'],
:from => ENV['SENDCLOUD_FROM'],
:fromname => ENV['SENDCLOUD_FROMNAME'],
:to => to,
:subject => "#{ENV['SITE_NAME']} 又写了新博客"
:html => <<-EOF
<p>hi, , :</p>
<p>#{ENV['SITE_NAME']} 新博客到了: #{title}</p>
<p>访: http://yafeilee.me</p>
EOF
}
return response
end
end

View File

@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :subscribe do
email "MyString"
enable false
end
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Subscribe do
pending "add some examples to (or delete) #{__FILE__}"
end