Merge branch 'subscribe_feature'

This commit is contained in:
yafeilee 2014-04-03 16:50:11 +08:00
commit 6564bf967a
17 changed files with 133 additions and 9 deletions

View File

@ -1,4 +1,4 @@
@app.controller 'AboutController', [ '$scope', ($scope)-> @app.controller 'AboutController', [ '$scope', '$timeout', '$http', ($scope, $timeout, $http)->
$scope.type = null $scope.type = null
$scope.click = (v)-> $scope.click = (v)->
@ -8,4 +8,29 @@
$scope.type = v $scope.type = v
$scope.weixin_click = -> $scope.weixin_click = ->
$scope.weixin = !$scope.weixin $scope.weixin = !$scope.weixin
#
$scope.email = ''
$scope.subscribe_success = null
$scope.email_validate = ->
$scope.email.match(/@/)
$scope.subscribe = ()->
$http
url: '/subscribes'
method: 'POST'
params:
email: $scope.email
.success (res)->
if res.success
$scope.email = ''
$scope.subscribe_success = true
else
$scope.subscribe_success = false
$scope.subscribe_fail_msg = res.message
$timeout ->
$scope.subscribe_success = null
, 3000
] ]

View File

@ -17,6 +17,7 @@
$scope.comments.unshift(res.data) $scope.comments.unshift(res.data)
else else
$scope.publish_success = false $scope.publish_success = false
$scope.publish_fail_msg = res.message
$timeout -> $timeout ->
$scope.publish_success = null $scope.publish_success = null
, 3*1000 , 3*1000

View File

@ -0,0 +1,10 @@
@app.controller 'SubscribesController', [ '$scope', '$http', ($scope, $http)->
$scope.cancel = ()->
$http
url: '/subscribes/cancel'
method: 'POST'
params:
email: $scope.email
.success (res)->
window.location = '/'
]

View File

@ -102,4 +102,14 @@
.rss-subscribe { .rss-subscribe {
margin-top: 0.5rem; margin-top: 0.5rem;
} }
.subscribe-success {
margin-left: 1rem;
color: green;
}
.subscribe-fail {
margin-left: 1rem;
color: red;
}
} }

View File

@ -14,7 +14,7 @@ class CommentsController < ApplicationController
if comment.save if comment.save
render :json=> { success: true, data: build_json(comment) } render :json=> { success: true, data: build_json(comment) }
else else
render :json=> { success: false } render :json=> { success: false, message: comment.errors.full_messages.join(", ") }
end end
end end

View File

@ -0,0 +1,25 @@
class SubscribesController < ApplicationController
def index
end
def create
subscribe = Subscribe.find_or_initialize_by(email: params[:email])
subscribe.enable = true
if subscribe.save
render :json => { success: true }
else
render :json => { success: false, message: subscribe.errors.full_messages.join(", ")}
end
end
def cancel
if subscribe = Subscribe.where(email: params[:email]).first
subscribe.enable = false
subscribe.save
end
flash[:notice] = "退订成功: #{params[:email]}"
render :json => { success: true }
end
end

View File

@ -9,7 +9,7 @@ class Comment
belongs_to :post belongs_to :post
validates :name, presence: true validates :name, presence: true
validates :email, presence: true,:format => /@/ validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: '地址无效' }
validates :content, presence: true validates :content, presence: true
validates_presence_of :post_id validates_presence_of :post_id

View File

@ -3,7 +3,7 @@ class Subscribe
field :email, type: String field :email, type: String
field :enable, type: Mongoid::Boolean, default: true field :enable, type: Mongoid::Boolean, default: true
validates :email, presence: true, uniqueness: true, format: /@/ validates :email, presence: true, uniqueness: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: '地址无效' }
def self.subscribe_list def self.subscribe_list
Subscribe.all.where(enable: true).map(&:email).join(";") Subscribe.all.where(enable: true).map(&:email).join(";")

View File

@ -10,7 +10,7 @@
= text_field_tag(:email, nil, placeholder: '你的邮箱', 'ng-model'=> 'email', 'ng-pattern'=>"/^.+@.+$/", 'ng-required'=>"true") = text_field_tag(:email, nil, placeholder: '你的邮箱', 'ng-model'=> 'email', 'ng-pattern'=>"/^.+@.+$/", 'ng-required'=>"true")
button ng-click="submit()" ng-disabled="form.$invalid" 发表 button ng-click="submit()" ng-disabled="form.$invalid" 发表
p.comment-success ng-show="publish_success" 发布成功 p.comment-success ng-show="publish_success" 发布成功
p.comment-fail ng-show="publish_success == false" 发布失败 p.comment-fail ng-show="publish_success == false" 发布失败: {{ publish_fail_msg }}
.comment-diag ng-cloak="" .comment-diag ng-cloak=""
.comment-wrapper ng-repeat=" comment in comments " .comment-wrapper ng-repeat=" comment in comments "
p.name p.name

View File

@ -45,7 +45,10 @@
li li
= link_to '邮件订阅 ', '', "ng-click"=>"click('email')" = link_to '邮件订阅 ', '', "ng-click"=>"click('email')"
.email-subscribe ng-show="type == 'email'" .email-subscribe ng-show="type == 'email'"
= text_field_tag 'email', nil, placeholder: 'your@email.com' = text_field_tag 'email', nil, placeholder: 'your@email.com', 'ng-model'=>'email'
button.small ng-click="subscribe()" ng-disabled="! email_validate()" 订阅
span.subscribe-success ng-show="subscribe_success" 订阅成功
span.subscribe-fail ng-show="subscribe_success == false" {{subscribe_fail_msg}}
li li
= link_to '微信扫一扫', '', "ng-click"=>"click('weixin')" = link_to '微信扫一扫', '', "ng-click"=>"click('weixin')"
.weixin-subscribe ng-show="type == 'weixin'" .weixin-subscribe ng-show="type == 'weixin'"

View File

@ -53,6 +53,11 @@ html
i.fi-torso i.fi-torso
| 关于我 | 关于我
section.main-section ng-app="app" section.main-section ng-app="app"
- flash.each do |name, msg|
- if msg.is_a?(String)
div class=("alert-box #{name == :notice ? "success" : "alert"}") data-alert=""
= content_tag :div, msg
a.close href="#" &times;
= yield = yield
= render "layouts/footer" = render "layouts/footer"
a.exit-off-canvas a.exit-off-canvas

View File

@ -0,0 +1,9 @@
.row
.large-9.large-centered.columns
h2 退订服务
hr
form action='' ng-controller='SubscribesController'
.row
.small-12.large-6.columns
= text_field_tag :email, nil, placeholder: 'your@email.com', 'ng-model' => 'email'
button ng-click="cancel()" 退订

View File

@ -1,6 +1,10 @@
class NewCommentWorker class NewCommentWorker
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_retry_in do |count|
3
end
def perform(name, content, title, to) def perform(name, content, title, to)
logger.info "[mail] new comment mail: name=#{name}, content=#{content}, title=#{title}, to=#{to}" logger.info "[mail] new comment mail: name=#{name}, content=#{content}, title=#{title}, to=#{to}"
response = send_mail(name, content, title, to) response = send_mail(name, content, title, to)

View File

@ -1,6 +1,10 @@
class NewPostWorker class NewPostWorker
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_retry_in do |count|
3
end
def perform(title, to) def perform(title, to)
logger.info "[mail] new post mail: title=#{title}, to=#{to}" logger.info "[mail] new post mail: title=#{title}, to=#{to}"
response = send_mail(title, to) response = send_mail(title, to)
@ -24,6 +28,8 @@ class NewPostWorker
<p>#{ENV['SITE_NAME']} 新博客到了: #{title}</p> <p>#{ENV['SITE_NAME']} 新博客到了: #{title}</p>
<p>访: http://yafeilee.me</p> <p>访: http://yafeilee.me</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF EOF
} }
return response return response

View File

@ -15,6 +15,12 @@ WBlog::Application.routes.draw do
resources :archives resources :archives
resources :subscribes do
collection do
post :cancel
end
end
get '/unsubscribe' => 'subscribes#index'
# photos # photos
resources :photos, only: [:create] resources :photos, only: [:create]
get '/qrcodes' => 'qrcodes#show' get '/qrcodes' => 'qrcodes#show'

View File

@ -0,0 +1,20 @@
require 'spec_helper'
describe SubscribesController do
describe "POST 'create'" do
it "post ok" do
post 'create', email: 'tester@test.com'
expect(JSON.parse(response.body)['success']).to be_true
expect(Subscribe.all.size).to eq(1)
end
it "post with disabled email" do
subscribe = Subscribe.create(email: 'tester@test.com', enable: false)
post 'create', email: 'tester@test.com'
expect(JSON.parse(response.body)['success']).to be_true
expect(subscribe.reload.enable).to be_true
end
end
end

View File

@ -11,13 +11,13 @@ describe Subscribe do
end end
it "default is true" do it "default is true" do
subscribe = Subscribe.create(email: 'a@b') subscribe = Subscribe.create(email: 'tester@test.com')
expect(subscribe.enable).to be_true expect(subscribe.enable).to be_true
end end
it "subscribe_list" do it "subscribe_list" do
subscribe = Subscribe.create(email: 'a@b') subscribe = Subscribe.create(email: 'tester@test.com')
subscribe = Subscribe.create(email: 'a1@b') subscribe = Subscribe.create(email: 'tester1@test.com')
expect(Subscribe.subscribe_list.split(';').size).to eq(2) expect(Subscribe.subscribe_list.split(';').size).to eq(2)
end end
end end