From f543fad37d1c8547c30da312e24b364b7f50d7aa Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 27 Apr 2016 15:42:44 +0800 Subject: [PATCH] Admin subscribe manage --- app/assets/stylesheets/admin/subscribes.scss | 3 ++ app/controllers/admin/posts_controller.rb | 2 +- .../admin/subscribes_controller.rb | 26 +++++++++++++++ app/views/admin/subscribes/index.html.slim | 25 +++++++++++++++ app/views/layouts/admin.html.slim | 2 ++ config/locales/en.yml | 10 ++++++ config/locales/zh-CN.yml | 10 ++++++ config/routes.rb | 6 ++++ .../admin/subscribes_controller_spec.rb | 32 +++++++++++++++++++ 9 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/admin/subscribes.scss create mode 100644 app/controllers/admin/subscribes_controller.rb create mode 100644 app/views/admin/subscribes/index.html.slim create mode 100644 spec/controllers/admin/subscribes_controller_spec.rb diff --git a/app/assets/stylesheets/admin/subscribes.scss b/app/assets/stylesheets/admin/subscribes.scss new file mode 100644 index 0000000..0d63856 --- /dev/null +++ b/app/assets/stylesheets/admin/subscribes.scss @@ -0,0 +1,3 @@ +.admin-subscribes-field { + +} diff --git a/app/controllers/admin/posts_controller.rb b/app/controllers/admin/posts_controller.rb index d583c48..a2ea0dc 100644 --- a/app/controllers/admin/posts_controller.rb +++ b/app/controllers/admin/posts_controller.rb @@ -22,7 +22,7 @@ class Admin::PostsController < ApplicationController end def index - @posts = Post.order(created_at: :desc).page(params[:page]) + @posts = Post.order(created_at: :desc).page(params[:page]).per(25) end def create diff --git a/app/controllers/admin/subscribes_controller.rb b/app/controllers/admin/subscribes_controller.rb new file mode 100644 index 0000000..43af9de --- /dev/null +++ b/app/controllers/admin/subscribes_controller.rb @@ -0,0 +1,26 @@ +class Admin::SubscribesController < ApplicationController + layout 'layouts/admin' + before_action :authericate_user! + + def index + @subscribes = Subscribe.all.order(created_at: :desc).page(params[:page]).per(25) + end + + def enable + @subscribe = Subscribe.find(params[:id]) + + @subscribe.enable = true + @subscribe.save! + + redirect_to admin_subscribes_path, notice: "#{@subscribe.email} is enabled!" + end + + def disable + @subscribe = Subscribe.find(params[:id]) + + @subscribe.enable = false + @subscribe.save! + + redirect_to admin_subscribes_path, notice: "#{@subscribe.email} is disabled!" + end +end diff --git a/app/views/admin/subscribes/index.html.slim b/app/views/admin/subscribes/index.html.slim new file mode 100644 index 0000000..02b2a99 --- /dev/null +++ b/app/views/admin/subscribes/index.html.slim @@ -0,0 +1,25 @@ +.row.admin-subscribes-field + .small-12.columns + h3.blog-title #{t('admin.subscribes') } + table width="100%" + thead + tr + th #{t('admin.subscribes_head.email')} + th #{t('admin.subscribes_head.enable')} + th #{t('admin.subscribes_head.created_at')} + th #{t('admin.subscribes_head.operation')} + tbody + - @subscribes.each do |subscribe| + tr + td + = mail_to subscribe.email + td + = subscribe.enable + td + = format_time(subscribe.created_at) + td + - if subscribe.enable + = link_to t('admin.subscribes_head.op.disable'), disable_admin_subscribe_path(subscribe), method: 'POST', data: { confirm: 'Confirm?' } + - else + = link_to t('admin.subscribes_head.op.enable'), enable_admin_subscribe_path(subscribe), method: 'POST', data: { confirm: 'Confirm?' } + == paginate @subscribes diff --git a/app/views/layouts/admin.html.slim b/app/views/layouts/admin.html.slim index fe2a386..d6736a6 100644 --- a/app/views/layouts/admin.html.slim +++ b/app/views/layouts/admin.html.slim @@ -20,6 +20,8 @@ html = link_to t('admin.new_post'), new_admin_post_path li = link_to t('admin.posts'), admin_posts_path + li + = link_to t('admin.subscribes'), admin_subscribes_path .top-bar-right ul.menu li diff --git a/config/locales/en.yml b/config/locales/en.yml index 989bac9..c4ee173 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -77,6 +77,16 @@ en: reply: 'Reply' destroy: 'Destroy' + subscribes: 'Manage Subscribes' + subscribes_head: + email: 'Email' + enable: 'Enable Flag' + created_at: 'Created at' + operation: 'Operation' + op: + enable: 'Enable' + disable: 'Disable' + back: 'Back to Home' logout: 'Logout' dashboard: diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 822db90..7a045cd 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -77,6 +77,16 @@ zh-CN: reply: '回复' destroy: '删除' + subscribes: '订阅管理' + subscribes_head: + email: '邮箱' + enable: '是否启用' + created_at: '创建时间' + operation: '操作' + op: + enable: '启用' + disable: '禁用' + back: '返回首页' logout: '退出' dashboard: diff --git a/config/routes.rb b/config/routes.rb index 647b6f3..f340e0e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,12 @@ WBlog::Application.routes.draw do resources :comments end resources :sessions, :only=>[:new, :create, :destroy] + resources :subscribes, only: [:index] do + member do + post :enable + post :disable + end + end root 'dashboard#index' end diff --git a/spec/controllers/admin/subscribes_controller_spec.rb b/spec/controllers/admin/subscribes_controller_spec.rb new file mode 100644 index 0000000..a0e6f7b --- /dev/null +++ b/spec/controllers/admin/subscribes_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +RSpec.describe Admin::SubscribesController, :type => :controller do + + before do + session[:login] = true + end + + describe "GET index" do + it "returns http success" do + get :index + expect(response).to have_http_status(:success) + end + end + + describe "POST enable" do + it "returns http success" do + subscribe = create(:subscribe) + post :enable, id: subscribe.id + expect(subscribe.reload.enable).to eq(true) + end + end + + describe "POST disable" do + it "returns http success" do + subscribe = create(:subscribe, enable: true) + post :disable, id: subscribe.id + expect(subscribe.reload.enable).to eq(false) + end + end + +end