Subscribe feature refactor

This commit is contained in:
yafeilee 2016-04-26 00:20:14 +08:00
parent deaa69f89e
commit 9b68bbde0b
13 changed files with 99 additions and 31 deletions

View File

@ -3,24 +3,23 @@ class SubscribesController < ApplicationController
def index def index
end end
def create def new
subscribe = Subscribe.find_or_initialize_by(email: params[:email]) @subscribe = Subscribe.new
subscribe.enable = true end
if subscribe.save def create
render :json => { success: true } @subscribe = Subscribe.find_or_initialize_by(email: subscribe_params[:email])
@subscribe.enable = true
if @subscribe.save
redirect_to subscribes_path, notice: '订阅成功'
else else
render :json => { success: false, message: subscribe.errors.full_messages.join(", ")} render :new
end end
end end
def cancel def subscribe_params
subscribe = Subscribe.find_or_initialize_by(email: params[:email]) params.require(:subscribe).permit(:email)
subscribe.enable = false
subscribe.save
flash[:notice] = "退订成功: #{params[:email]}"
render :json => { success: true }
end end
end end

View File

@ -0,0 +1,17 @@
class UnsubscribesController < ApplicationController
def index
end
def new
@subscribe = Subscribe.new
end
def create
subscribe = Subscribe.find_or_initialize_by(email: params[:email])
subscribe.enable = false
subscribe.save
flash[:notice] = "退订成功: #{params[:email]}"
redirect_to unsubscribes_path
end
end

View File

@ -28,6 +28,9 @@
.row .row
.small-12.medium-6.large-12.columns .small-12.medium-6.large-12.columns
ul.subscribe-ul ul.subscribe-ul
- if ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'].include?('@')
li
= link_to t('subscribes.email'), new_subscribe_path
li li
a data-toggle="qrcode-home" #{t('subscribes.wechat') } a data-toggle="qrcode-home" #{t('subscribes.wechat') }
#qrcode-home.weixin-subscribe.hide data-toggler='hide' data-url=root_url #qrcode-home.weixin-subscribe.hide data-toggler='hide' data-url=root_url

View File

@ -1,9 +1,9 @@
.row .row
.large-9.large-centered.columns .large-9.large-centered.columns
h2 退订服务 h2 订阅成功, 我们将提供:
hr
form action='' ng-controller='SubscribesController' ul
.row li 第一时间新博客邮件通知
.small-12.large-6.columns li 有相关评论 @ 你的时候邮件通知
= text_field_tag :email, nil, placeholder: 'your@email.com', 'ng-model' => 'email'
button ng-click="cancel()" 退订 p 你可以随时取消订阅

View File

@ -0,0 +1,7 @@
.row
.small-12.medium-12.large-9.columns
h2 订阅
= simple_form_for @subscribe do |f|
= f.input :email
= f.button :submit

View File

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

View File

@ -0,0 +1,5 @@
.row
.large-9.large-centered.columns
h4 退订成功
p 我们将不再向你发送任何邮件

View File

@ -0,0 +1,9 @@
.row
.large-9.large-centered.columns
h2 退订服务
hr
= simple_form_for @subscribe, url: unsubscribes_path do |f|
.row
.small-12.large-6.columns
= f.input :email, placeholder: 'your@email.com'
= f.submit

View File

@ -6,7 +6,7 @@ SimpleForm.setup do |config|
b.use :placeholder b.use :placeholder
b.use :label_input b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint } b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error } b.use :error, wrap_with: { tag: :span, class: 'form-error is-visible' }
end end

View File

@ -0,0 +1,31 @@
en:
simple_form:
"yes": '是'
"no": '否'
required:
text: '必须的'
mark: '*'
# You can uncomment the line below if you need to overwrite the whole required html.
# When using html, text and mark won't be used.
# html: '<abbr title="required">*</abbr>'
error_notification:
default_message: "请检查以下问题:"
# Examples
# labels:
# defaults:
# password: 'Password'
# user:
# new:
# email: 'E-mail to sign in.'
# edit:
# email: 'E-mail.'
# hints:
# defaults:
# username: 'User name to sign in.'
# password: 'No special characters, please.'
# include_blanks:
# defaults:
# age: 'Rather not say'
# prompts:
# defaults:
# age: 'Select your age'

View File

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

0
lib/markdown.rb Executable file → Normal file
View File

View File

@ -4,15 +4,13 @@ describe SubscribesController do
describe "POST 'create'" do describe "POST 'create'" do
it "post ok" do it "post ok" do
post 'create', email: 'tester@test.com' post 'create', { subscribe: { email: 'tester@test.com' } }
expect(JSON.parse(response.body)['success']).to be_truthy
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: 'tester@test.com', enable: false) subscribe = Subscribe.create(email: 'tester@test.com', enable: false)
post 'create', email: 'tester@test.com' post 'create', { subscribe: { email: 'tester@test.com' } }
expect(JSON.parse(response.body)['success']).to be_truthy
expect(subscribe.reload.enable).to be_truthy expect(subscribe.reload.enable).to be_truthy
end end
end end