From 9b68bbde0b616492d88d0fe3f9e30b4eb3fb4780 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Tue, 26 Apr 2016 00:20:14 +0800 Subject: [PATCH 1/4] Subscribe feature refactor --- app/controllers/subscribes_controller.rb | 25 +++++++-------- app/controllers/unsubscribes_controller.rb | 17 ++++++++++ app/views/blogs/index.html.slim | 3 ++ app/views/subscribes/index.html.slim | 14 ++++----- app/views/subscribes/new.html.slim | 7 +++++ app/views/unsubscribes/create.html.slim | 2 ++ app/views/unsubscribes/index.html.slim | 5 +++ app/views/unsubscribes/new.html.slim | 9 ++++++ config/initializers/simple_form.rb | 2 +- config/locales/simple_form.zh-CN.yml | 31 +++++++++++++++++++ config/routes.rb | 9 ++---- lib/markdown.rb | 0 .../controllers/subscribes_controller_spec.rb | 6 ++-- 13 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 app/controllers/unsubscribes_controller.rb create mode 100644 app/views/subscribes/new.html.slim create mode 100644 app/views/unsubscribes/create.html.slim create mode 100644 app/views/unsubscribes/index.html.slim create mode 100644 app/views/unsubscribes/new.html.slim create mode 100644 config/locales/simple_form.zh-CN.yml mode change 100755 => 100644 lib/markdown.rb diff --git a/app/controllers/subscribes_controller.rb b/app/controllers/subscribes_controller.rb index 3d9042e..5974695 100644 --- a/app/controllers/subscribes_controller.rb +++ b/app/controllers/subscribes_controller.rb @@ -3,24 +3,23 @@ class SubscribesController < ApplicationController def index end - def create - subscribe = Subscribe.find_or_initialize_by(email: params[:email]) - subscribe.enable = true + def new + @subscribe = Subscribe.new + end - if subscribe.save - render :json => { success: true } + def create + @subscribe = Subscribe.find_or_initialize_by(email: subscribe_params[:email]) + @subscribe.enable = true + + if @subscribe.save + redirect_to subscribes_path, notice: '订阅成功' else - render :json => { success: false, message: subscribe.errors.full_messages.join(", ")} + render :new end end - def cancel - subscribe = Subscribe.find_or_initialize_by(email: params[:email]) - subscribe.enable = false - subscribe.save - - flash[:notice] = "退订成功: #{params[:email]}" - render :json => { success: true } + def subscribe_params + params.require(:subscribe).permit(:email) end end diff --git a/app/controllers/unsubscribes_controller.rb b/app/controllers/unsubscribes_controller.rb new file mode 100644 index 0000000..1bf268e --- /dev/null +++ b/app/controllers/unsubscribes_controller.rb @@ -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 diff --git a/app/views/blogs/index.html.slim b/app/views/blogs/index.html.slim index c21e900..81ecfc6 100644 --- a/app/views/blogs/index.html.slim +++ b/app/views/blogs/index.html.slim @@ -28,6 +28,9 @@ .row .small-12.medium-6.large-12.columns ul.subscribe-ul + - if ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'].include?('@') + li + = link_to t('subscribes.email'), new_subscribe_path li a data-toggle="qrcode-home" #{t('subscribes.wechat') } #qrcode-home.weixin-subscribe.hide data-toggler='hide' data-url=root_url diff --git a/app/views/subscribes/index.html.slim b/app/views/subscribes/index.html.slim index 1578e35..ed366c9 100644 --- a/app/views/subscribes/index.html.slim +++ b/app/views/subscribes/index.html.slim @@ -1,9 +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()" 退订 + h2 订阅成功, 我们将提供: + + ul + li 第一时间新博客邮件通知 + li 有相关评论 @ 你的时候邮件通知 + + p 你可以随时取消订阅 diff --git a/app/views/subscribes/new.html.slim b/app/views/subscribes/new.html.slim new file mode 100644 index 0000000..c49cf85 --- /dev/null +++ b/app/views/subscribes/new.html.slim @@ -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 diff --git a/app/views/unsubscribes/create.html.slim b/app/views/unsubscribes/create.html.slim new file mode 100644 index 0000000..8471bcc --- /dev/null +++ b/app/views/unsubscribes/create.html.slim @@ -0,0 +1,2 @@ +h1 Unsubscribes#create +p Find me in app/views/unsubscribes/create.html.slim diff --git a/app/views/unsubscribes/index.html.slim b/app/views/unsubscribes/index.html.slim new file mode 100644 index 0000000..6557964 --- /dev/null +++ b/app/views/unsubscribes/index.html.slim @@ -0,0 +1,5 @@ +.row + .large-9.large-centered.columns + h4 退订成功 + + p 我们将不再向你发送任何邮件 diff --git a/app/views/unsubscribes/new.html.slim b/app/views/unsubscribes/new.html.slim new file mode 100644 index 0000000..5306e74 --- /dev/null +++ b/app/views/unsubscribes/new.html.slim @@ -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 diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index 72f2b8a..bab62ab 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -6,7 +6,7 @@ SimpleForm.setup do |config| b.use :placeholder b.use :label_input 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 diff --git a/config/locales/simple_form.zh-CN.yml b/config/locales/simple_form.zh-CN.yml new file mode 100644 index 0000000..c7f34c3 --- /dev/null +++ b/config/locales/simple_form.zh-CN.yml @@ -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: '*' + 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' diff --git a/config/routes.rb b/config/routes.rb index a4632f1..647b6f3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,12 +14,9 @@ WBlog::Application.routes.draw do resources :archives - resources :subscribes do - collection do - post :cancel - end - end - get '/unsubscribe' => 'subscribes#index' + resources :subscribes, only: [:index, :new, :create] + + resources :unsubscribes, only: [:index, :new, :create] # photos resources :photos, only: [:create] get '/qrcodes' => 'qrcodes#show' diff --git a/lib/markdown.rb b/lib/markdown.rb old mode 100755 new mode 100644 diff --git a/spec/controllers/subscribes_controller_spec.rb b/spec/controllers/subscribes_controller_spec.rb index 15037e6..6528f21 100644 --- a/spec/controllers/subscribes_controller_spec.rb +++ b/spec/controllers/subscribes_controller_spec.rb @@ -4,15 +4,13 @@ describe SubscribesController do describe "POST 'create'" do it "post ok" do - post 'create', email: 'tester@test.com' - expect(JSON.parse(response.body)['success']).to be_truthy + post 'create', { subscribe: { email: 'tester@test.com' } } expect(Subscribe.all.size).to eq(1) end it "post with disabled email" do subscribe = Subscribe.create(email: 'tester@test.com', enable: false) - post 'create', email: 'tester@test.com' - expect(JSON.parse(response.body)['success']).to be_truthy + post 'create', { subscribe: { email: 'tester@test.com' } } expect(subscribe.reload.enable).to be_truthy end end From eb2886e686179564de758f2e8de216934534a4ef Mon Sep 17 00:00:00 2001 From: yafeilee Date: Tue, 26 Apr 2016 17:50:41 +0800 Subject: [PATCH 2/4] Update Gemfile --- Gemfile | 2 +- Gemfile.lock | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 64dbc44..40681e4 100644 --- a/Gemfile +++ b/Gemfile @@ -49,7 +49,7 @@ group :development do gem 'guard-rails' gem 'guard-rspec', require: false gem 'guard-bundler', require: false - gem 'listen', '~> 3.0.5' + gem 'listen', '~> 3.x' gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index c3e57c6..30fd863 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,7 +79,7 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.1) connection_pool (2.2.0) - database_cleaner (1.5.2) + database_cleaner (1.5.3) diff-lcs (1.2.5) docile (1.1.5) domain_name (0.5.20160310) @@ -142,7 +142,7 @@ GEM js_cookie_rails (1.0.1) railties (>= 3.1) json (1.8.3) - listen (3.0.6) + listen (3.1.1) rb-fsevent (>= 0.9.3) rb-inotify (>= 0.9.7) loofah (2.0.3) @@ -354,7 +354,7 @@ DEPENDENCIES jquery-rails js_cookie_rails kaminari! - listen (~> 3.0.5) + listen (~> 3.x) mina mina-multistage mina-puma From 91e9201bea0e3215f37ee9a81756bedd603b315f Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 27 Apr 2016 10:21:26 +0800 Subject: [PATCH 3/4] About page css adjust --- app/assets/stylesheets/aboutme_welcome.scss | 11 +++++-- app/views/home/index.html.slim | 32 ++++++++++----------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/assets/stylesheets/aboutme_welcome.scss b/app/assets/stylesheets/aboutme_welcome.scss index ef3ca0a..654a65f 100644 --- a/app/assets/stylesheets/aboutme_welcome.scss +++ b/app/assets/stylesheets/aboutme_welcome.scss @@ -37,7 +37,14 @@ a { word-wrap: break-word; } - @media only screen and (min-width: 40.063em) { + //@media only screen and (min-width: 40.063em) { + @media screen and (max-width: 39.9375em) { + .top-bar-wrapper { + .top-bar, .top-bar-left, .top-bar-right { + width: auto !important; + } + } + } .top-bar-wrapper { background: 0 0; transition: background .5s ease-in-out,padding .5s ease-in-out; @@ -77,7 +84,7 @@ } } } - } + //} p { font-size: 1.275rem; diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index 51bdfff..62200ec 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -3,23 +3,21 @@ - content_for(:main) do .about-page .top-bar-wrapper.contain-to-grid.fixed - .row - .small-12.columns - nav#about-top-bar.top-bar - .top-bar-left - ul.menu - li - a.name href='/' 回到博客 - .top-bar-right - ul.menu - li - a href='#about' 关于 - li - a href='#skill' 技能 - li - a href='#work' 作品 - li - a href='#contact' 联系 + nav#about-top-bar.top-bar + .top-bar-left + ul.menu + li + a.name href='/' 回到博客 + .top-bar-right + ul.menu + li + a href='#about' 关于 + li + a href='#skill' 技能 + li + a href='#work' 作品 + li + a href='#contact' 联系 #intro header.intro .intro-heading From 8203757d1ef3c35bf1be31e4fc95b1e2006c6059 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 27 Apr 2016 12:22:37 +0800 Subject: [PATCH 4/4] Fix subscribe system for rails5 --- app/assets/stylesheets/blogs.scss | 1 - app/mailers/comment_mailer.rb | 2 +- app/mailers/post_mailer.rb | 2 +- app/models/comment.rb | 4 ++-- app/models/post.rb | 2 +- app/views/comment_mailer/{new.html.slim => born.html.slim} | 0 app/views/comment_mailer/{new.text.erb => born.text.erb} | 0 app/views/post_mailer/{new.html.slim => born.html.slim} | 0 app/views/post_mailer/{new.text.erb => born.text.erb} | 0 app/views/unsubscribes/create.html.slim | 2 -- app/workers/new_comment_worker.rb | 2 +- app/workers/new_post_worker.rb | 2 +- app/workers/new_reply_post_worker.rb | 1 - config/environments/development.rb | 2 +- 14 files changed, 8 insertions(+), 12 deletions(-) rename app/views/comment_mailer/{new.html.slim => born.html.slim} (100%) rename app/views/comment_mailer/{new.text.erb => born.text.erb} (100%) rename app/views/post_mailer/{new.html.slim => born.html.slim} (100%) rename app/views/post_mailer/{new.text.erb => born.text.erb} (100%) delete mode 100644 app/views/unsubscribes/create.html.slim diff --git a/app/assets/stylesheets/blogs.scss b/app/assets/stylesheets/blogs.scss index 7523144..01e1332 100644 --- a/app/assets/stylesheets/blogs.scss +++ b/app/assets/stylesheets/blogs.scss @@ -48,7 +48,6 @@ } .recent-title { - border-bottom: 1px solid #DCDCDC; margin-bottom: 1rem; } diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb index 24874d0..202582a 100644 --- a/app/mailers/comment_mailer.rb +++ b/app/mailers/comment_mailer.rb @@ -3,7 +3,7 @@ class CommentMailer < ActionMailer::Base default from: "no-reply@#{domain}" - def new(comment_id, to) + def born(comment_id, to) @comment = Comment.find(comment_id) mail to: to, subject: '博主, 你的博客有新的评论' end diff --git a/app/mailers/post_mailer.rb b/app/mailers/post_mailer.rb index 390bed4..652802c 100644 --- a/app/mailers/post_mailer.rb +++ b/app/mailers/post_mailer.rb @@ -3,7 +3,7 @@ class PostMailer < ActionMailer::Base default from: "no-reply@#{domain}" - def new(post_id, to) + def born(post_id, to) @post = Post.find(post_id) mail to: to, subject: '客官, 新博客来了' end diff --git a/app/models/comment.rb b/app/models/comment.rb index 1c5d459..49be5e4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,10 +7,10 @@ class Comment < ApplicationRecord validates :content, presence: true def reply_emails - Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list + Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list - [ ENV['ADMIN_USER'] ] end - after_create do + after_commit on: :create do if ENV['MAIL_SERVER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/ Rails.logger.info 'comment created, comment worker start' NewCommentWorker.perform_async(self.id.to_s, ENV['ADMIN_USER']) diff --git a/app/models/post.rb b/app/models/post.rb index d5fe8c4..dfcc06f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -8,7 +8,7 @@ class Post < ActiveRecord::Base validates :title, :presence=>true, :uniqueness=> true validates :content, :presence=>true, :length => { :minimum=> 30 } - after_create do + after_commit on: :create do if ENV['MAIL_SERVER'].present? NewPostWorker.perform_async(self.id.to_s) end diff --git a/app/views/comment_mailer/new.html.slim b/app/views/comment_mailer/born.html.slim similarity index 100% rename from app/views/comment_mailer/new.html.slim rename to app/views/comment_mailer/born.html.slim diff --git a/app/views/comment_mailer/new.text.erb b/app/views/comment_mailer/born.text.erb similarity index 100% rename from app/views/comment_mailer/new.text.erb rename to app/views/comment_mailer/born.text.erb diff --git a/app/views/post_mailer/new.html.slim b/app/views/post_mailer/born.html.slim similarity index 100% rename from app/views/post_mailer/new.html.slim rename to app/views/post_mailer/born.html.slim diff --git a/app/views/post_mailer/new.text.erb b/app/views/post_mailer/born.text.erb similarity index 100% rename from app/views/post_mailer/new.text.erb rename to app/views/post_mailer/born.text.erb diff --git a/app/views/unsubscribes/create.html.slim b/app/views/unsubscribes/create.html.slim deleted file mode 100644 index 8471bcc..0000000 --- a/app/views/unsubscribes/create.html.slim +++ /dev/null @@ -1,2 +0,0 @@ -h1 Unsubscribes#create -p Find me in app/views/unsubscribes/create.html.slim diff --git a/app/workers/new_comment_worker.rb b/app/workers/new_comment_worker.rb index aeef92f..361d8ff 100644 --- a/app/workers/new_comment_worker.rb +++ b/app/workers/new_comment_worker.rb @@ -7,7 +7,7 @@ class NewCommentWorker def perform(comment_id, to) logger.info "new comment mail: #{comment_id}" - CommentMailer.new(comment_id.to_s, to).deliver + CommentMailer.born(comment_id.to_s, to).deliver end end diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index 6bde5a5..2cc6141 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -8,7 +8,7 @@ class NewPostWorker def perform(post_id) Subscribe.subscribe_list.each do |email| Rails.logger.info "new post #{post_id}, send to #{email}" - PostMailer.new(post_id.to_s, email).deliver + PostMailer.born(post_id.to_s, email).deliver end end end diff --git a/app/workers/new_reply_post_worker.rb b/app/workers/new_reply_post_worker.rb index c27fb6a..9fc7a5b 100644 --- a/app/workers/new_reply_post_worker.rb +++ b/app/workers/new_reply_post_worker.rb @@ -9,7 +9,6 @@ class NewReplyPostWorker logger.info "new reply mail" comment = Comment.find(comment_id.to_s) comment.reply_emails.each do |email| - next if email == ENV['ADMIN_USER'] logger.info "new reply mail to #{email}" CommentMailer.reply(comment_id.to_s, email).deliver end diff --git a/config/environments/development.rb b/config/environments/development.rb index 9817503..5fc0ff7 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -24,7 +24,7 @@ WBlog::Application.configure do # number of complex assets. config.assets.debug = true - config.middleware.insert_before 0, "Rack::Cors" do + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [:get, :post, :options]