重构项目和课程排序部分,修复分页数量错误

This commit is contained in:
yanxd 2014-03-21 16:46:30 +08:00
parent 20cd5941a8
commit 9c54c9c1b6
3 changed files with 57 additions and 59 deletions

View File

@ -49,7 +49,7 @@ class ProjectsController < ApplicationController
# before_filter :authorize, :except => [:new_join, :new_homework, :homework, :statistics, :search, :watcherlist, :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy, :member, :focus, :file, # before_filter :authorize, :except => [:new_join, :new_homework, :homework, :statistics, :search, :watcherlist, :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy, :member, :focus, :file,
# :statistics, :feedback, :course, :enterprise_course, :course_enterprise, :project_respond, :share, # :statistics, :feedback, :course, :enterprise_course, :course_enterprise, :project_respond, :share,
# :show_projects_score, :issue_score_index, :news_score_index, :file_score_index, :code_submit_score_index, :projects_topic_score_index] # :show_projects_score, :issue_score_index, :news_score_index, :file_score_index, :code_submit_score_index, :projects_topic_score_index]
before_filter :authorize, :only => [:sort_project_demo, :show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen] before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen]
before_filter :authorize_global, :only => [:new, :create] before_filter :authorize_global, :only => [:new, :create]
before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar] before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar]
#by young #by young
@ -127,32 +127,39 @@ class ProjectsController < ApplicationController
def index def index
#Modified by nie #Modified by nie
@project_type = params[:project_type] @project_type = params[:project_type]
@limit = 10 per_page_option = 10
@project_count = Project.visible.where("project_type = ?", 0).count
@project_pages = Paginator.new @project_count, @limit, params['page'] @projects_all = Project.active.visible.
if params[:project_sort_type].present? joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
case params[:project_sort_type] where("#{Project.table_name}.project_type = ? ", Project::ProjectType_project)
when '0'
@projects = @projects = Project.visible.where("project_type = ?", 0).order("created_on desc").offset(@project_pages.offset).limit(@limit).all @project_count = @projects_all.count
@s_type = 0 @project_pages = Paginator.new @project_count, per_page_option, params['page']
when '1'
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit
@s_type = 1 case params[:project_sort_type]
when '2' when '0'
@projects = sort_project_demo project_type = 0, order_by="watchers_count desc", @project_pages.offset, @limit @projects = @projects_all.order("created_on desc")
@s_type = 2 @s_type = 0
end when '1'
@projects = @projects_all.order("grade desc")
@s_type = 1
when '2'
@projects = @projects_all.order("watchers_count desc")
@s_type = 2
else else
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit @projects = @projects = @projects_all.order("grade desc")
@s_type = 1 @s_type = 1
end end
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
respond_to do |format| respond_to do |format|
format.html { format.html {
render :layout => 'base' render :layout => 'base'
scope = Project # scope = Project
unless params[:closed] # unless params[:closed]
scope = scope.active # scope = scope.active
end # end
} }
format.api { format.api {
# @offset, @limit = api_offset_and_limit # @offset, @limit = api_offset_and_limit
@ -168,34 +175,34 @@ class ProjectsController < ApplicationController
def course def course
@project_type = params[:project_type] @project_type = params[:project_type]
@limit = 10; per_page_option = 10
@project_count = Project.visible.where("project_type = 1").count
@project_pages = Paginator.new @project_count, @limit, params['page'] @projects_all = Project.active.visible.
if params[:project_sort_type].present? joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
case params[:project_sort_type] where("#{Project.table_name}.project_type = ? ", Project::ProjectType_course)
when '0'
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all @project_count = @projects_all.count
@s_type = 0 @project_pages = Paginator.new @project_count, per_page_option, params['page']
when '1'
@projects = sort_project_demo 1, "course_ac_para desc", @project_pages.offset, @limit case params[:project_sort_type]
@s_type = 1 when '0'
when '2' @projects = @projects_all.order("created_on desc")
@projects = sort_project_demo 1, "watchers_count desc", @project_pages.offset, @limit @s_type = 0
@s_type = 2 when '1'
end @projects = @projects_all.order("course_ac_para desc")
@s_type = 1
when '2'
@projects = @projects_all.order("watchers_count desc")
@s_type = 2
else else
# @projects = sort_project_demo 1, "course_ac_para desc", @project_pages.offset, @limit @s_type = 0
@s_type = 0 @projects = @projects_all.order("created_on desc")
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
# @s_type = 0
end end
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
respond_to do |format| respond_to do |format|
format.html { format.html {
render :layout => 'base' render :layout => 'base'
scope = Project
unless params[:closed]
scope = scope.active
end
} }
format.api { format.api {
# @offset, @limit = api_offset_and_limit # @offset, @limit = api_offset_and_limit
@ -208,17 +215,8 @@ class ProjectsController < ApplicationController
} }
end end
end end
def sort_project_demo project_type, order_by, offset, limit def search
#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 #modified by nie
@projects = Project.visible @projects = Project.visible
@projects = @projects.visible.where('project_type = ?', params[:project_type]).like(params[:name]) if params[:name].present? @projects = @projects.visible.where('project_type = ?', params[:project_type]).like(params[:name]) if params[:name].present?
@ -461,7 +459,7 @@ class ProjectsController < ApplicationController
project = ProjectInfo.new(:user_id => User.current.id, :project_id => @project.id) project = ProjectInfo.new(:user_id => User.current.id, :project_id => @project.id)
user_grades = UserGrade.create(:user_id => User.current.id, :project_id => @project.id) user_grades = UserGrade.create(:user_id => User.current.id, :project_id => @project.id)
if params[:project][:is_public] == '1' || @course_tag=="1" if params[:project][:is_public] == '1' || @course_tag=="1"
project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0) project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type)
end end
@project.members << m @project.members << m
@project.project_infos << project @project.project_infos << project

View File

@ -38,7 +38,7 @@
<div class="pagination"> <div class="pagination">
<ul> <ul>
<%= pagination_links_full @project_pages %> <%= pagination_links_full @project_pages, @project_count %>
</ul> </ul>
</div> </div>

View File

@ -34,7 +34,7 @@
<div class="pagination"> <div class="pagination">
<ul> <ul>
<%= pagination_links_full @project_pages %> <%= pagination_links_full @project_pages, @project_count %>
</ul> </ul>
</div> </div>