socialforge/app/controllers/blogs_controller.rb

94 lines
2.3 KiB
Ruby
Raw Normal View History

2015-10-24 15:34:43 +08:00
class BlogsController < ApplicationController
before_filter :find_blog,:except => [:index,:create,:new,:set_homepage, :cancel_homepage]
2015-10-24 15:34:43 +08:00
before_filter :find_user
2016-04-22 13:52:33 +08:00
include PraiseTreadHelper
2015-10-24 15:34:43 +08:00
def index
@article = BlogComment.new
2016-04-20 16:12:20 +08:00
2016-04-22 13:52:33 +08:00
@order, @b_sort,@type = params[:order] || 1, params[:sort] || 2, params[:type] || 1
2016-04-21 09:39:21 +08:00
2016-04-22 13:52:33 +08:00
#确定 sort_type
if @order.to_i == @type.to_i
@b_sort = @b_sort.to_i == 1 ? 2 : 1
else
@b_sort = 1
end
2016-04-21 09:39:21 +08:00
2016-04-22 13:52:33 +08:00
sort_name = "updated_on"
2016-04-20 16:12:20 +08:00
2016-04-22 13:52:33 +08:00
sort_type = @b_sort == 1 ? "desc" : "asc"
2016-04-21 09:39:21 +08:00
2016-04-22 13:52:33 +08:00
@topics = @user.blog.articles.reorder("#{BlogComment.table_name}.sticky desc,#{BlogComment.table_name}.#{sort_name} #{sort_type}")
2016-04-20 16:12:20 +08:00
2016-04-22 13:52:33 +08:00
#根据 赞+回复数排序
if @order.to_i == 2
@type = 2
@b_sort == 1 ? @topics = @topics.sort{|x,y| get_praise_num(y) + (y.parent ? y.parent.children.count : y.children.count) <=> get_praise_num(x) + (x.parent ? x.parent.children.count : x.children.count) } : @topics = @topics.sort{|x,y| get_praise_num(x) + (x.parent ? x.parent.children.count : x.children.count) <=> get_praise_num(y) + (y.parent ? y.parent.children.count : y.children.count) }
else
@type = 1
end
2016-04-21 09:39:21 +08:00
2016-04-22 13:52:33 +08:00
#分页
@limit = 10
@is_remote = true
@atta_count = @topics.count
@atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1
@offset ||= @atta_pages.offset
@topics = paginateHelper @topics,@limit
2016-04-21 09:39:21 +08:00
2015-10-24 15:34:43 +08:00
respond_to do |format|
2016-04-22 13:52:33 +08:00
format.js
2015-10-24 15:34:43 +08:00
format.html {render :layout=>'new_base_user'}
end
end
def create
end
def new
end
def show
end
def update
end
def destory
end
def edit
end
def set_homepage
@blog = Blog.find(params[:id])
@blog.update_attribute(:homepage_id, params[:article_id])
redirect_to user_path(params[:user_id])
end
def cancel_homepage
@blog = Blog.find(params[:id])
@blog.update_attribute(:homepage_id, nil)
redirect_to user_blogs_path(params[:user_id])
end
2015-10-24 15:34:43 +08:00
private
def find_blog
if params[:blog_id]
@blog = Blog.find(params[:blog_id])
else
render_404
end
2015-10-24 15:34:43 +08:00
if @blog.nil?
#如果某个user的blog不存在那么就创建一条
@blog = Blog.create(:name=>User.find(params[:id]).realname ,
:description=>'',
:author_id=>params[:id])
end
end
def find_user
@user = User.find(params[:user_id])
end
end