重构排序代码

This commit is contained in:
nieguanghui 2013-12-17 16:59:07 +08:00
parent f8f5ed3f9a
commit c6f657ed8c
3 changed files with 31 additions and 183 deletions

View File

@ -123,118 +123,26 @@ class ProjectsController < ApplicationController
def index
#Modified by nie
# @offset, @limit = api_offset_and_limit({:limit => 10})
# @project_count = Project.visible.count
# @project_pages = Paginator.new @project_count, @limit, params['page']
# @offset ||= @project_pages.offset
# @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
@project_type = params[:project_type]
@offset, @limit = api_offset_and_limit({:limit => 10})
@projects_status = ProjectStatus.visible.where("project_statuses.project_type <> ? or project_statuses.project_type is null", 1)
# @projects_status = ProjectStatus.visible
# @projects_status.each do |project|
# if Project.visible.find_by_id("#{project.project_id}")
# project.project_type = Project.visible.find_by_id("#{project.project_id}").project_type
# project.save
# end
# end
# @projects.each do |project|
# @admin = project.users_by_role[Role.find(3)]
# unless @admin.nil?
# @admin.each do |user|
# ProjectInfo.create(:user_id => user.id, :project_id => project.id)
# end
#
# end
# end
# @projects = Project.where("project_type <> ? or project_type is null", 1)
# @projects.each do |project|
# @id = project.id
# @users = Member.find_by_sql("SELECT
# members.user_id
# FROM members
# INNER JOIN users
# ON members.user_id = users.id
# WHERE members.project_id = #{@id}
# AND (users.type = 'User' AND users.status = 1);")
# @users.each do |user|
# UserGrade.create(:project_id => project.id, :user_id => user.user_id)
# end
# #ProjectStatus.create(:changesets_count => project.changesets.count, :project_id => project.id, :watchers_count => project.watcher_users.count)
# end
# @projects_status = Project.visible.like(params[:name]) if params[:name].present?
@project_count = @projects_status.count
@limit = 10
@project_count = Project.visible.where("project_type = ?", 0).count
@project_pages = Paginator.new @project_count, @limit, params['page']
@offset ||= @project_pages.reverse_offset
# @projects = @projects.offset(@offset).limit(@limit).order('created_on DESC').all
if params[:project_sort_type].present?
case params[:project_sort_type]
when '0'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.joins(:project).reorder("projects.created_on").offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.joins(:project).reorder("projects.created_on").offset(@offset).limit(@limit).all.reverse
end
@projects = @projects = Project.visible.where("project_type = ?", 0).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
@s_type = 0
# @projects = @projects.sort {|x,y| y.created_on <=> x.created_on }
# @projects = @projects[@offset, @limit]
when '1'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.reorder('grade').offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.reorder('grade').offset(@offset).limit(limit).all.reverse
end
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit
@s_type = 1
#@projects = @projects[@offset, @limit]
when '2'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.reorder('watchers_count').offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.reorder('watchers_count').offset(@offset).limit(limit).all.reverse
end
@projects = sort_project_demo project_type = 0, order_by="watchers_count desc", @project_pages.offset, @limit
@s_type = 2
end
else
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.reorder('grade').offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.reorder('grade').offset(@offset).limit(limit).all.reverse
end
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit
@s_type = 1
# @projects = @projects.sort {|x,y| y.created_on <=> x.created_on }
# @projects = @projects[@offset, @limit]
end
@projects = []
@projects_status.each do |obj|
@projects << Project.visible.find_by_id("#{obj.project_id}") unless Project.visible.find_by_id("#{obj.project_id}").nil?
end
#end
respond_to do |format|
format.html {
render :layout => 'base'
@ -256,99 +164,28 @@ class ProjectsController < ApplicationController
end
def course
#Modified by nie
# @offset, @limit = api_offset_and_limit({:limit => 10})
# @project_count = Project.visible.count
# @project_pages = Paginator.new @project_count, @limit, params['page']
# @offset ||= @project_pages.offset
# @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
@project_type = params[:project_type]
@offset, @limit = api_offset_and_limit({:limit => 10})
@projects_status = ProjectStatus.visible.where("project_type = ?", 1)
# @projects.each do |project|
# @admin = project.users_by_role[Role.find(3)]
# unless @admin.nil?
# @admin.each do |user|
# ProjectInfo.create(:user_id => user.id, :project_id => project.id)
# end
#
# end
# end
# @projects.each do |project|
# ProjectStatus.create(:changesets_count => project.changesets.count, :project_id => project.id, :watchers_count => project.watcher_users.count)
# end
#@projects_status = Project.visible.like(params[:name]) if params[:name].present?
@project_count = @projects_status.count
@limit = 10;
@project_count = Project.visible.where("project_type = 1").count
@project_pages = Paginator.new @project_count, @limit, params['page']
@offset ||= @project_pages.reverse_offset
#@projects = @projects.offset(@offset).limit(@limit).order('created_on DESC').all
if params[:project_sort_type].present?
case params[:project_sort_type]
when '0'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.offset(@offset).limit(limit).all.reverse
end
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
@s_type = 0
# @projects = @projects.sort {|x,y| y.created_on <=> x.created_on }
# @projects = @projects[@offset, @limit]
when '1'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.reorder('course_ac_para').offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.reorder('course_ac_para').offset(@offset).limit(limit).all.reverse
end
@projects = sort_project_demo 1, "course_ac_para desc", @project_pages.offset, @limit
@s_type = 1
#@projects = @projects[@offset, @limit]
when '2'
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.reorder('watchers_count').offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.reorder('watchers_count').offset(@offset).limit(limit).all.reverse
end
@projects = sort_project_demo 1, "watchers_count desc", @project_pages.offset, @limit
@s_type = 2
end
else
@offset ||= @project_pages.reverse_offset
unless @offset == 0
@projects_status = @projects_status.offset(@offset).limit(@limit).all.reverse
else
limit = @project_count % @limit
if limit == 0
limit = @limit
end
@projects_status = @projects_status.offset(@offset).limit(limit).all.reverse
end
@s_type = 0
# @projects = @projects.sort {|x,y| y.created_on <=> x.created_on }
# @projects = @projects[@offset, @limit]
logger.debug("-------------------------------------------------")
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
logger.debug("-------------------------------------------------")
@s_type = 0
end
@projects = []
@projects_status.each do |obj|
@projects << Project.visible.find_by_id("#{obj.project_id}") unless Project.visible.find_by_id("#{obj.project_id}").nil?
end
#end
respond_to do |format|
format.html {
render :layout => 'base'
@ -369,6 +206,15 @@ class ProjectsController < ApplicationController
end
end
def sort_project_demo project_type, order_by, offset, limit
#Project.visible.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).offset(offset).limit(limit).all
Project.visible.find_by_sql("
SELECT *
FROM projects AS p RIGHT OUTER JOIN (
SELECT project_id,grade FROM project_statuses
WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} OFFSET #{offset}) AS t ON p.id = t.project_id ")
end
def search
#modified by nie
@projects = Project.visible

View File

@ -159,12 +159,12 @@ module WelcomeHelper
end
def sort_project_by_hot_rails project_type=0, order_by='grade DESC', limit=15
# Project.find_by_sql("
# Project.find_by_sql("
# SELECT p.id, p.name, p.description, p.identifier, t.project_id
# FROM projects AS p RIGHT OUTER JOIN (
# SELECT project_id,grade FROM project_statuses
# WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} ) AS t ON p.id = t.project_id ")
Project.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).limit(limit).all
# FROM projects AS p LEFT OUTER JOIN (
# SELECT project_id,grade FROM project_statuses
# WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} ) AS t ON p.id = t.project_id ")
Project.visible.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).limit(limit).all
end
def sort_bid_by_hot_rails reward_type, limit = 10

View File

@ -1105,4 +1105,6 @@ class Project < ActiveRecord::Base
logger.error "[Project Model] ===> Auto create board when project saved, because #{@board.full_messages}"
end
end
end