From 49366069e4d6919e3dfb36c6eb33f7219743a9b3 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Thu, 24 Dec 2015 12:15:21 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/blogs/_set_sidebar.html.haml | 19 +++++++++++++++++++ app/views/blogs/about.html.haml | 10 ++++++++++ app/views/blogs/index.html.haml | 11 +++++++++++ 3 files changed, 40 insertions(+) create mode 100755 app/views/blogs/_set_sidebar.html.haml create mode 100755 app/views/blogs/about.html.haml create mode 100755 app/views/blogs/index.html.haml diff --git a/app/views/blogs/_set_sidebar.html.haml b/app/views/blogs/_set_sidebar.html.haml new file mode 100755 index 0000000..d675bc3 --- /dev/null +++ b/app/views/blogs/_set_sidebar.html.haml @@ -0,0 +1,19 @@ +- if defined?(obj) && obj.present? && obj.errors.any? + .alert.alert-danger + %a.close{"data-dismiss"=>"alert"} × + %span + = obj.errors.full_messages.first +- if flash[:error].present? + .alert.alert-danger + %a.close{"data-dismiss"=>"alert"} × + %span + = flash[:error] +.col-md-2 + %ul.nav.nav-pills.nav-stacked{role: 'tablist'} + %li{role: 'presentation', class: "#{'active' if params[:action] == 'set'}"} + = link_to '个人信息设置', set_blogs_path + - if current_user_is_admin? + %li{role: 'presentation', class: "#{'active' if params[:action] == 'set_blog'}"} + = link_to '博客设置', set_blog_blogs_path + %li{role: 'presentation'} + = link_to '修改密码', change_password_blogs_path diff --git a/app/views/blogs/about.html.haml b/app/views/blogs/about.html.haml new file mode 100755 index 0000000..f0cb224 --- /dev/null +++ b/app/views/blogs/about.html.haml @@ -0,0 +1,10 @@ +.text-left + %p + %h2 博主 + = @blog ? @blog.name : '未设置' + %p + %h2 email + = @blog ? (link_to @blog.email, "mailto:#{@blog.email}") : '未设置' + %p + %h2 简介 + = @blog ? @blog.description : '未设置' \ No newline at end of file diff --git a/app/views/blogs/index.html.haml b/app/views/blogs/index.html.haml new file mode 100755 index 0000000..368b9e1 --- /dev/null +++ b/app/views/blogs/index.html.haml @@ -0,0 +1,11 @@ +.row.inner.edge + .col-md-9.layout-main + .article-list#article-list + = render partial: 'articles/articles' + .col-md-3 + %h3 最新评论 + - if @new_comments.present? + %ul.list-group.new_comments + - @new_comments.each do |comment| + %li.text-left + = link_to "#{comment.user.nickname}: #{comment.content[0..18]}...", article_path(comment.article_id) + "#comment_#{comment.id}", target: '_blank', class: 'list-group-item' From a95c79cd6c8edefb1d1a961ec9d5164d653782d2 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Sat, 26 Dec 2015 12:25:39 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/blogs_controller.rb | 39 +++++++++++--- app/views/blogs/set.html.haml | 79 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100755 app/views/blogs/set.html.haml diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 9a25810..ef73b44 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -1,7 +1,9 @@ +#encoding: utf-8 class BlogsController < ApplicationController - + before_filter :require_login, only: [:set, :set_userinfo, :upload_img, :set_blog, :update_blog, :change_password, :update_password] def index - + @articles = Article.order('updated_at desc').limit(10) + @new_comments = ArticleComment.order('updated_at desc').limit(10) end def set @@ -9,11 +11,29 @@ class BlogsController < ApplicationController end def set_userinfo - + user = User.find @current_user.id + user.nick_name = params[:user][:nick_name] if params[:user][:nick_name].present? + begin + if params[:user][:avatar].present? + upload_info = upload_picture params[:user][:avatar] + user.avatar = "/images/#{upload_info[:real_file_name]}" + end + rescue UploadException => e + flash.now[:error] = e.message + render 'set' + else + if user.save + redirect_to set_blogs_path + else + flash.now[:error] = user.errors.full_messages.first + render 'set' + end + end end def set_blog - + @blog = BlogInfo.first + @blog = BlogInfo.new unless @blog end def update_blog @@ -21,7 +41,7 @@ class BlogsController < ApplicationController end def about - + @blog = BlogInfo.first end def change_password @@ -37,7 +57,12 @@ class BlogsController < ApplicationController end def preview - + result = {status: false, message: ''} + if params[:content] + result[:status] = true + result[:message] = markdown_parser params[:content] + end + render json: result.to_json end protected @@ -45,4 +70,4 @@ class BlogsController < ApplicationController def upload_picture(file) end -end \ No newline at end of file +end diff --git a/app/views/blogs/set.html.haml b/app/views/blogs/set.html.haml new file mode 100755 index 0000000..ec1790f --- /dev/null +++ b/app/views/blogs/set.html.haml @@ -0,0 +1,79 @@ +.row + %h3.set_title 设置 + = render 'set_sidebar' + .col-md-8 + = form_for :user, url: set_userinfo_blogs_path, role: 'form' do |f| + .form-group + %label{for: 'user_nick_name'} 昵称 + = f.text_field :nick_name, value: @current_user.nickname, placeholder: '昵称', class: 'form-control', size: 10 + .form-group + %label{for: 'user_avatar'} 头像 + .row.text-left + .col-md-2#img_preview + %img#set_user_avatar{src: "#{avatar_url(@current_user)}"} + .col-md-8.avatar-field + = f.file_field :avatar + .form-group + = f.submit '修改', class: 'btn btn-primary set-btn' + + +:javascript + function previewImage(file){ + var MAXWIDTH = 80; + var MAXHEIGHT = 80; + var div = document.getElementById('img_preview'); + if (file.files && file.files[0]) + { + div.innerHTML = ''; + var img = document.getElementById('imghead'); + img.onload = function(){ + var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); + img.height = rect.height; + img.style.marginTop = rect.top+'px'; + } + var reader = new FileReader(); + reader.onload = function(evt){img.src = evt.target.result;} + reader.readAsDataURL(file.files[0]); + } + else + { + var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="'; + file.select(); + var src = document.selection.createRange().text; + div.innerHTML = ''; + var img = document.getElementById('imghead'); + img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src; + var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); + status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height); + div.innerHTML = "
"; + } + } + + function clacImgZoomParam( maxWidth, maxHeight, width, height ){ + var param = {top:0, left:0, width:width, height:height}; + if( width>maxWidth || height>maxHeight ) + { + rateWidth = width / maxWidth; + rateHeight = height / maxHeight; + + if( rateWidth > rateHeight ) + { + param.width = maxWidth; + param.height = Math.round(height / rateWidth); + }else + { + param.width = Math.round(width / rateHeight); + param.height = maxHeight; + } + } + + param.left = Math.round((maxWidth - param.width) / 2); + param.top = Math.round((maxHeight - param.height) / 2); + return param; + } + + $(function(){ + $("#user_avatar").change(function() { + previewImage(this); + }); + }); \ No newline at end of file From 54bdaec347d3197e95a04954074d0889e28c16a6 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Mon, 28 Dec 2015 12:34:18 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=A4=B4=E5=83=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/blogs_controller.rb | 18 ++++++++++++++++-- app/views/blogs/upload_img.js.haml | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 app/views/blogs/upload_img.js.haml diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index ef73b44..991dce2 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -53,7 +53,19 @@ class BlogsController < ApplicationController end def upload_img - + @result = {status: false, message: '', text_id: params[:upload][:text_id] || ''} + begin + if params[:upload].present? && params[:upload][:img].present? && remotipart_submitted? + upload_info = upload_picture params[:upload][:img] + @result[:status] = true + @result[:message] = "![#{upload_info[:file_name]}](/images/#{upload_info[:real_file_name]})" + end + rescue UploadException => e + @result[:message] = e.message + end + respond_to do |format| + format.js + end end def preview @@ -68,6 +80,8 @@ class BlogsController < ApplicationController protected def upload_picture(file) - + upload_path = File.join Rails.root, 'public/images' + upload = SimpleFileupload.new upload_path:upload_path, max_size: 1024*1024*2, type: 'image' + upload_info = upload.upload file end end diff --git a/app/views/blogs/upload_img.js.haml b/app/views/blogs/upload_img.js.haml new file mode 100755 index 0000000..f7a05e3 --- /dev/null +++ b/app/views/blogs/upload_img.js.haml @@ -0,0 +1,8 @@ +- if @result[:status] + :plain + var old_val = $("##{@result[:text_id]}").val(); + $("##{@result[:text_id]}").val(old_val + "#{@result[:message]}"); + $('#upload_modal').modal('hide'); +- else + :plain + $('#error').text("#{@result[:message]}"); \ No newline at end of file From 812c8bc9ff0356f1b602d435e52489a4814ab279 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Thu, 31 Dec 2015 12:40:58 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/blogs_controller.rb | 15 ++++++++++++++- app/views/blogs/change_password.html.haml | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 app/views/blogs/change_password.html.haml diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 991dce2..97a5008 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -49,7 +49,20 @@ class BlogsController < ApplicationController end def update_password - + @user = User.find @current_user.id + if @user.check_password(params[:user][:old_password]) + @user.password = params[:user][:password] || '' + @user.password_confirmation = params[:user][:password_confirmation] || '' + if @user.save + flash[:success] = '修改密码成功,请重新登录' + redirect_to login_path + else + render 'change_password' + end + else + flash.now[:error] = '原密码错误' + render 'change_password' + end end def upload_img diff --git a/app/views/blogs/change_password.html.haml b/app/views/blogs/change_password.html.haml new file mode 100755 index 0000000..d539f92 --- /dev/null +++ b/app/views/blogs/change_password.html.haml @@ -0,0 +1,18 @@ +.row + %h3.set_title 设置 + = render 'set_sidebar', obj: @user + .col-md-8 + = form_for :user, method: 'post', url: update_password_blogs_path, role: 'form' do |f| + .form-group + %label{for: 'user_username'} 用户名 + = f.text_field :username, value: @current_user.username, placeholder: '用户名', class: 'form-control username', disabled: 'disabled' + .form-group + %label{for: 'user_password'} 旧密码 + = f.password_field :old_password, placeholder: '旧密码', class: 'form-control password', required: true + .form-group + %label{for: 'user_password'} 新密码 + = f.password_field :password, placeholder: '密码,最少6位', class: 'form-control password', required: true + .form-group + %label{for: 'user_password_confirmation'} 确认密码 + = f.password_field :password_confirmation, placeholder: '确认密码', class: 'form-control password', required: true + = submit_tag '修改', class: 'btn btn-primary set-btn' \ No newline at end of file From 5e5310bc93dc95ba2144976febb7a4465d683447 Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Sun, 3 Jan 2016 12:46:51 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=8D=9A=E5=AE=A2=E4=BF=A1=E6=81=AF=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/blogs_controller.rb | 15 ++++++++++++++- app/views/blogs/set_blog.html.haml | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 app/views/blogs/set_blog.html.haml diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 97a5008..50dee09 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -1,6 +1,7 @@ #encoding: utf-8 class BlogsController < ApplicationController before_filter :require_login, only: [:set, :set_userinfo, :upload_img, :set_blog, :update_blog, :change_password, :update_password] + def index @articles = Article.order('updated_at desc').limit(10) @new_comments = ArticleComment.order('updated_at desc').limit(10) @@ -37,7 +38,19 @@ class BlogsController < ApplicationController end def update_blog - + param_hash = params.require(:blog).permit(:name, :blog_title, :email, :description) + @blog = BlogInfo.first + if @blog.present? + result = @blog.update_attributes param_hash + else + @blog = BlogInfo.new param_hash + result = @blog.save + end + if result + redirect_to set_blogs_path + else + render 'set_blog' + end end def about diff --git a/app/views/blogs/set_blog.html.haml b/app/views/blogs/set_blog.html.haml new file mode 100755 index 0000000..f6525cd --- /dev/null +++ b/app/views/blogs/set_blog.html.haml @@ -0,0 +1,19 @@ +.row + %h3.set_title 设置 + = render 'set_sidebar', obj: @blog + .col-md-8 + = form_for @blog, as: 'blog', url: update_blog_blogs_path, method: 'post', role: 'form' do |f| + .form-group + %label{for: 'blog_name'} 博主 + = f.text_field :name, placeholder: '博主名称', class: 'form-control' + .form-group + %label{for: 'blog_blog_title'} 博客名字 + = f.text_field :blog_title, placeholder: '博客名称', class: 'form-control' + .form-group + %label{for: 'blog_email'} 联系邮箱 + = f.text_field :email, placeholder: '联系邮箱', class: 'form-control' + .form-group + %label{for: 'blog_description'} 关于博主 + = f.text_area :description, placeholder: '关于博主', class: 'form-control blog-description' + .form-group + = f.submit '修改', class: 'btn btn-primary set-btn' \ No newline at end of file From 79597f235a91a45be60c30d4ace8ec10b2da5d8b Mon Sep 17 00:00:00 2001 From: meganchen15 Date: Mon, 4 Jan 2016 15:01:46 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0blogs=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/controllers/blogs_controller_test.rb | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/controllers/blogs_controller_test.rb diff --git a/test/controllers/blogs_controller_test.rb b/test/controllers/blogs_controller_test.rb new file mode 100644 index 0000000..74037f7 --- /dev/null +++ b/test/controllers/blogs_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' +require 'blogs_controller' +class BlogsControllerTest < ActionController::TestCase + setup do + @controller = BlogsController.new + @blog = BlogInfo.new + @blog.name = 'testblog' + @blog.blog_title = 'New Year around the world' + @blog.email = 'testuser@gmail.com' + @blog.description = 'How do people around the world celebrate their new year?' + @blog.save + + @user = User.new + @user.username = 'testuser' + @user.email = 'testuser@gmail.com' + @user.password = '123456' + @user.password_confirmation = '123456' + @user.admin = 1 + @user.save + end + + test "should get index" do + get :index + assert_response :success + end + + test "should set userinfo" do + patch :set_userinfo, username: @user.username, :user => {:nick_name => 'honey', :avatar => '/users/megan/downloads/1.jpg'} + assert_not_nil assigns(:user) + assert_redirected_to set_blogs_path + end + + test "should update blog" do + post :update_blog, :blog => {:name => 'test', :blog_title => 'test', :email => 'testuser@gmail.com', :description => 'This is a test.'} + assert_redirected_to set_blogs_path + end + + test "should update password" do + patch :update_password, username: @user.username, :user=>{:old_password => @user.password, :password => 'friends', :password_confirmation => 'friends'} + assert_response :success + assert_redirected_to login_path(assigns(:user)) + end + + test "should not update password" do + post :update_password, {:old_password => 'notvalid', :password => 'friends', :password_confirmation => 'friends'} + assert_response :error + end + +end