Merge pull request #2 from E2S2015/blogs

Blogs
This commit is contained in:
thedreamwork 2016-01-06 19:13:16 +08:00
commit 21fe6b788b
9 changed files with 289 additions and 11 deletions

View File

@ -1,7 +1,10 @@
#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,19 +12,49 @@ 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
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
@blog = BlogInfo.first
end
def change_password
@ -29,20 +62,52 @@ 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
@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
result = {status: false, message: ''}
if params[:content]
result[:status] = true
result[:message] = markdown_parser params[:content]
end
render json: result.to_json
end
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
end

View File

@ -0,0 +1,19 @@
- if defined?(obj) && obj.present? && obj.errors.any?
.alert.alert-danger
%a.close{"data-dismiss"=>"alert"} &times;
%span
= obj.errors.full_messages.first
- if flash[:error].present?
.alert.alert-danger
%a.close{"data-dismiss"=>"alert"} &times;
%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

10
app/views/blogs/about.html.haml Executable file
View File

@ -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 : '未设置'

View File

@ -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'

11
app/views/blogs/index.html.haml Executable file
View File

@ -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'

79
app/views/blogs/set.html.haml Executable file
View File

@ -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 = '<img id=imghead>';
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 = '<img id=imghead>';
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 = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";
}
}
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);
});
});

View File

@ -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'

View File

@ -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]}");

View File

@ -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