socialforge/app/controllers/blogs_controller.rb

105 lines
2.5 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-25 10:44:46 +08:00
@order, @b_sort,@type = params[:order] || 1, params[:sort] || 1, params[:type] || 1
2016-04-21 09:39:21 +08:00
2016-04-25 10:44:46 +08:00
#确定 sort_type 1升序 2 降序
2016-04-22 13:52:33 +08:00
if @order.to_i == @type.to_i
@b_sort = @b_sort.to_i == 1 ? 2 : 1
else
2016-04-25 10:44:46 +08:00
@b_sort = 2
2016-04-22 13:52:33 +08:00
end
2016-04-21 09:39:21 +08:00
2016-04-24 10:49:33 +08:00
sort_name = "updated_at"
2016-04-20 16:12:20 +08:00
2016-04-25 10:44:46 +08:00
sort_type = @b_sort == 1 ? "asc" : "desc"
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
2016-04-29 15:52:44 +08:00
@topics.each do |topic|
topic[:infocount] = get_praise_num(topic) + (topic.parent ? topic.parent.children.count : topic.children.count)
2016-04-29 19:24:20 +08:00
if topic[:infocount] < 0
topic[:infocount] = 0
end
2016-04-29 15:52:44 +08:00
end
@b_sort == 1 ? @topics = @topics.sort{|x,y| x[:infocount] <=> y[:infocount] } : @topics = @topics.sort{|x,y| y[:infocount] <=> x[:infocount] }
2016-04-29 13:42:58 +08:00
@topics = sort_by_sticky @topics
2016-04-29 19:24:20 +08:00
@topics = sortby_time_countcommon_hassticky @topics,sort_name
2016-04-22 13:52:33 +08:00
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