订阅功能添加

This commit is contained in:
yafeilee 2014-04-02 23:40:05 +08:00
parent 0991210023
commit 51ad5412cf
16 changed files with 102 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

@ -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,21 @@
class SubscribesController < ApplicationController
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 destroy
if subscribe = Subscribe.where(email: params[:email]).first
subscribe.enable = false
subscribe.save
end
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

@ -0,0 +1,2 @@
h1 Subscribes#create
p Find me in app/views/subscribes/create.html.slim

View File

@ -0,0 +1,2 @@
h1 Subscribes#destroy
p Find me in app/views/subscribes/destroy.html.slim

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)

View File

@ -15,6 +15,7 @@ WBlog::Application.routes.draw do
resources :archives resources :archives
resources :subscribes
# 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: 'a@b'
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: 'a@b', enable: false)
post 'create', email: 'a@b'
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