diff --git a/app/assets/javascripts/angularjs/about.js.coffee b/app/assets/javascripts/angularjs/about.js.coffee index 1a19898..58ef59c 100644 --- a/app/assets/javascripts/angularjs/about.js.coffee +++ b/app/assets/javascripts/angularjs/about.js.coffee @@ -1,4 +1,4 @@ -@app.controller 'AboutController', [ '$scope', ($scope)-> +@app.controller 'AboutController', [ '$scope', '$timeout', '$http', ($scope, $timeout, $http)-> $scope.type = null $scope.click = (v)-> @@ -8,4 +8,29 @@ $scope.type = v $scope.weixin_click = -> $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 ] diff --git a/app/assets/javascripts/angularjs/comments.js.coffee b/app/assets/javascripts/angularjs/comments.js.coffee index c347577..ac90e03 100644 --- a/app/assets/javascripts/angularjs/comments.js.coffee +++ b/app/assets/javascripts/angularjs/comments.js.coffee @@ -17,6 +17,7 @@ $scope.comments.unshift(res.data) else $scope.publish_success = false + $scope.publish_fail_msg = res.message $timeout -> $scope.publish_success = null , 3*1000 diff --git a/app/assets/stylesheets/blogs.css.scss b/app/assets/stylesheets/blogs.css.scss index b3a0a16..7b52622 100644 --- a/app/assets/stylesheets/blogs.css.scss +++ b/app/assets/stylesheets/blogs.css.scss @@ -102,4 +102,14 @@ .rss-subscribe { margin-top: 0.5rem; } + + .subscribe-success { + margin-left: 1rem; + color: green; + } + + .subscribe-fail { + margin-left: 1rem; + color: red; + } } diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 840f8d5..a41ca44 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -14,7 +14,7 @@ class CommentsController < ApplicationController if comment.save render :json=> { success: true, data: build_json(comment) } else - render :json=> { success: false } + render :json=> { success: false, message: comment.errors.full_messages.join(", ") } end end diff --git a/app/controllers/subscribes_controller.rb b/app/controllers/subscribes_controller.rb new file mode 100644 index 0000000..d715002 --- /dev/null +++ b/app/controllers/subscribes_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 3d8377c..1ef6522 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,7 @@ class Comment belongs_to :post 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_presence_of :post_id diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 643dda0..7ce6ade 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -3,7 +3,7 @@ class Subscribe field :email, type: String 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 Subscribe.all.where(enable: true).map(&:email).join(";") diff --git a/app/views/blogs/_comment.html.slim b/app/views/blogs/_comment.html.slim index 4953189..42c33fd 100644 --- a/app/views/blogs/_comment.html.slim +++ b/app/views/blogs/_comment.html.slim @@ -10,7 +10,7 @@ = text_field_tag(:email, nil, placeholder: '你的邮箱', 'ng-model'=> 'email', 'ng-pattern'=>"/^.+@.+$/", 'ng-required'=>"true") button ng-click="submit()" ng-disabled="form.$invalid" 发表 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-wrapper ng-repeat=" comment in comments " p.name diff --git a/app/views/blogs/index.html.slim b/app/views/blogs/index.html.slim index 71c1a77..58a2037 100644 --- a/app/views/blogs/index.html.slim +++ b/app/views/blogs/index.html.slim @@ -45,7 +45,10 @@ li = link_to '邮件订阅 ', '', "ng-click"=>"click('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 = link_to '微信扫一扫', '', "ng-click"=>"click('weixin')" .weixin-subscribe ng-show="type == 'weixin'" diff --git a/app/views/subscribes/create.html.slim b/app/views/subscribes/create.html.slim new file mode 100644 index 0000000..49d3678 --- /dev/null +++ b/app/views/subscribes/create.html.slim @@ -0,0 +1,2 @@ +h1 Subscribes#create +p Find me in app/views/subscribes/create.html.slim diff --git a/app/views/subscribes/destroy.html.slim b/app/views/subscribes/destroy.html.slim new file mode 100644 index 0000000..1e2d50c --- /dev/null +++ b/app/views/subscribes/destroy.html.slim @@ -0,0 +1,2 @@ +h1 Subscribes#destroy +p Find me in app/views/subscribes/destroy.html.slim diff --git a/app/workers/new_comment_worker.rb b/app/workers/new_comment_worker.rb index f7d68e8..43baddf 100644 --- a/app/workers/new_comment_worker.rb +++ b/app/workers/new_comment_worker.rb @@ -1,6 +1,10 @@ class NewCommentWorker include Sidekiq::Worker + sidekiq_retry_in do |count| + 3 + end + def perform(name, content, title, to) logger.info "[mail] new comment mail: name=#{name}, content=#{content}, title=#{title}, to=#{to}" response = send_mail(name, content, title, to) diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index 1abb5e7..1384723 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -1,6 +1,10 @@ class NewPostWorker include Sidekiq::Worker + sidekiq_retry_in do |count| + 3 + end + def perform(title, to) logger.info "[mail] new post mail: title=#{title}, to=#{to}" response = send_mail(title, to) diff --git a/config/routes.rb b/config/routes.rb index 46fc2ad..31f80f8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,7 @@ WBlog::Application.routes.draw do resources :archives + resources :subscribes # photos resources :photos, only: [:create] get '/qrcodes' => 'qrcodes#show' diff --git a/spec/controllers/subscribes_controller_spec.rb b/spec/controllers/subscribes_controller_spec.rb new file mode 100644 index 0000000..6774e60 --- /dev/null +++ b/spec/controllers/subscribes_controller_spec.rb @@ -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 diff --git a/spec/models/subscribe_spec.rb b/spec/models/subscribe_spec.rb index 84ac295..ef71aa8 100644 --- a/spec/models/subscribe_spec.rb +++ b/spec/models/subscribe_spec.rb @@ -11,13 +11,13 @@ describe Subscribe do end it "default is true" do - subscribe = Subscribe.create(email: 'a@b') + subscribe = Subscribe.create(email: 'tester@test.com') expect(subscribe.enable).to be_true end it "subscribe_list" do - subscribe = Subscribe.create(email: 'a@b') - subscribe = Subscribe.create(email: 'a1@b') + subscribe = Subscribe.create(email: 'tester@test.com') + subscribe = Subscribe.create(email: 'tester1@test.com') expect(Subscribe.subscribe_list.split(';').size).to eq(2) end end