添加订阅取消功能

This commit is contained in:
yafeilee 2014-04-03 16:49:59 +08:00
parent 51ad5412cf
commit fed92dd16b
9 changed files with 40 additions and 9 deletions

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

@ -1,4 +1,6 @@
class SubscribesController < ApplicationController class SubscribesController < ApplicationController
def index
end
def create def create
subscribe = Subscribe.find_or_initialize_by(email: params[:email]) subscribe = Subscribe.find_or_initialize_by(email: params[:email])
subscribe.enable = true subscribe.enable = true
@ -10,12 +12,14 @@ class SubscribesController < ApplicationController
end end
end end
def destroy def cancel
if subscribe = Subscribe.where(email: params[:email]).first if subscribe = Subscribe.where(email: params[:email]).first
subscribe.enable = false subscribe.enable = false
subscribe.save subscribe.save
end end
flash[:notice] = "退订成功: #{params[:email]}"
render :json => { success: true } render :json => { success: true }
end end
end end

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

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

View File

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

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

@ -28,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,7 +15,12 @@ WBlog::Application.routes.draw do
resources :archives resources :archives
resources :subscribes 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

@ -4,14 +4,14 @@ describe SubscribesController do
describe "POST 'create'" do describe "POST 'create'" do
it "post ok" 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(JSON.parse(response.body)['success']).to be_true
expect(Subscribe.all.size).to eq(1) expect(Subscribe.all.size).to eq(1)
end end
it "post with disabled email" do it "post with disabled email" do
subscribe = Subscribe.create(email: 'a@b', enable: false) subscribe = Subscribe.create(email: 'tester@test.com', enable: false)
post 'create', email: 'a@b' post 'create', email: 'tester@test.com'
expect(JSON.parse(response.body)['success']).to be_true expect(JSON.parse(response.body)['success']).to be_true
expect(subscribe.reload.enable).to be_true expect(subscribe.reload.enable).to be_true
end end