diff --git a/app/assets/javascripts/angularjs/subscribes.js.coffee b/app/assets/javascripts/angularjs/subscribes.js.coffee new file mode 100644 index 0000000..04bf07a --- /dev/null +++ b/app/assets/javascripts/angularjs/subscribes.js.coffee @@ -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 = '/' +] diff --git a/app/controllers/subscribes_controller.rb b/app/controllers/subscribes_controller.rb index d715002..d77cffc 100644 --- a/app/controllers/subscribes_controller.rb +++ b/app/controllers/subscribes_controller.rb @@ -1,4 +1,6 @@ class SubscribesController < ApplicationController + def index + end def create subscribe = Subscribe.find_or_initialize_by(email: params[:email]) subscribe.enable = true @@ -10,12 +12,14 @@ class SubscribesController < ApplicationController end end - def destroy + 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 diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 19a7f6c..cf17021 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -53,6 +53,11 @@ html i.fi-torso | 关于我 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="#" × = yield = render "layouts/footer" a.exit-off-canvas diff --git a/app/views/subscribes/create.html.slim b/app/views/subscribes/create.html.slim deleted file mode 100644 index 49d3678..0000000 --- a/app/views/subscribes/create.html.slim +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 1e2d50c..0000000 --- a/app/views/subscribes/destroy.html.slim +++ /dev/null @@ -1,2 +0,0 @@ -h1 Subscribes#destroy -p Find me in app/views/subscribes/destroy.html.slim diff --git a/app/views/subscribes/index.html.slim b/app/views/subscribes/index.html.slim new file mode 100644 index 0000000..1578e35 --- /dev/null +++ b/app/views/subscribes/index.html.slim @@ -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()" 退订 diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index 1384723..b2148bd 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -28,6 +28,8 @@ class NewPostWorker

#{ENV['SITE_NAME']} 新博客到了: #{title}

具体内容请访问: http://yafeilee.me

+ +

点此退订

EOF } return response diff --git a/config/routes.rb b/config/routes.rb index 31f80f8..57ba389 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,12 @@ WBlog::Application.routes.draw do resources :archives - resources :subscribes + resources :subscribes do + collection do + post :cancel + end + end + get '/unsubscribe' => 'subscribes#index' # 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 index 6774e60..3cc33b6 100644 --- a/spec/controllers/subscribes_controller_spec.rb +++ b/spec/controllers/subscribes_controller_spec.rb @@ -4,14 +4,14 @@ describe SubscribesController do describe "POST 'create'" do it "post ok" do - post 'create', email: 'a@b' + 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: 'a@b', enable: false) - post 'create', email: 'a@b' + 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