From 0c22d409fc89c29eff884b4c0bc5d7577cf1ec07 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 27 Aug 2013 11:27:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=99project=5Fstatus?= =?UTF-8?q?=E5=92=8Cuser=5Fstatus=E4=B8=A4=E8=A1=A8=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=20=E5=9C=A8=E5=85=B3=E6=B3=A8=E4=B8=AD?= =?UTF-8?q?=E5=86=99=E8=BF=992=E4=B8=AA=E8=A1=A8=EF=BC=8C=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BA=86tag=E7=9A=84=E8=BF=87=E6=BB=A4=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=20=E6=8B=BF=E6=8E=89=E4=BA=86user=5Fextensio?= =?UTF-8?q?n.rb.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/tags_controller.rb | 205 +++++++++++--------- app/controllers/watchers_controller.rb | 26 ++- app/helpers/bids_helper.rb | 2 +- app/helpers/groups_helper.rb | 2 +- app/helpers/issues_helper.rb | 2 +- app/helpers/members_helper.rb | 2 +- app/helpers/projects_helper.rb | 2 +- app/helpers/users_helper.rb | 2 +- app/models/project.rb | 8 +- app/models/project_status.rb | 21 ++ app/models/user.rb | 7 +- app/models/user_extension.rb | 11 -- app/models/user_status.rb | 20 ++ app/views/my/account.html.erb | 2 +- app/views/tags/_related_tags.html.erb | 5 +- app/views/tags/_selected_tags.html.erb | 8 +- app/views/tags/_show_bids.html.erb | 1 + app/views/tags/_tag_name.html.erb | 4 +- app/views/tags/_tag_search_results.html.erb | 8 +- app/views/tags/add_tag.js.erb | 7 +- app/views/tags/delete_tag.js.erb | 4 +- app/views/tags/index.html.erb | 31 +-- app/views/tags/show_all.html.erb | 2 +- app/views/watchers/_set_watcher.js.erb | 3 - 24 files changed, 223 insertions(+), 162 deletions(-) create mode 100644 app/models/project_status.rb delete mode 100644 app/models/user_extension.rb create mode 100644 app/models/user_status.rb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index a7c787746..cac287653 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -14,71 +14,28 @@ class TagsController < ApplicationController $selected_tags = Array.new $related_tags = Array.new + @@numbers = Setting.tags_show_search_results + + def index $selected_tags = [] $related_tags = [] $selected_tags << params[:q] - @issues_tags_num = Issue.tag_counts.size - @projects_tags_num = Project.tag_counts.size - @users_tags_num = User.tag_counts.size - @bids_tags_num = Bid.tag_counts.size - - # 这里为了提高系统的响应速度 把搜索结果放到case中去了 - @users_results = nil - @projects_results = nil - @issues_results = nil - @bids_results = nil - @obj_pages = nil - + @users_tags_num,@projects_tags_num,@issues_tags_num,@bids_tags_num = get_tags_size + @obj_id = params[:obj_id] @obj_flag = params[:object_flag] - @numbers = Setting.tags_show_search_results - - case @obj_flag - when '1' then - @users_results = get_users_by_tag($selected_tags) - @obj = User.find_by_id(@obj_id) - - @offset, @limit = api_offset_and_limit({:limit => @numbers}) - @project_count = @users_results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - @users_results = @users_results.offset(@offset).limit(@limit).all - when '2' then - @projects_results = get_projects_by_tag($selected_tags) - @obj = Project.find_by_id(@obj_id) - - @offset, @limit = api_offset_and_limit({:limit => @numbers}) - @project_count = @projects_results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - @projects_results = @projects_results.offset(@offset).limit(@limit).order('lft').all - - when '3' then - @issues_results = get_issues_by_tag($selected_tags) - @obj = Issue.find_by_id(@obj_id) - - @offset, @limit = api_offset_and_limit({:limit => @numbers}) - @project_count = @issues_results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - @issues_results = @issues_results.offset(@offset).limit(@limit).all - when '4' - @bids_results = get_bids_by_tag($selected_tags) - @obj = Bid.find_by_id(@obj_id) - - @offset, @limit = api_offset_and_limit({:limit => @numbers}) - @project_count = @bids_results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - @bids_results = @bids_results.offset(@offset).limit(@limit).all - - else - @obj = nil - end - + + # 获取搜索结果 + @obj,@obj_pages,@users_results, + @projects_results, + @issues_results, + @bids_results, + @obj_pages = refresh_results(@obj_id,@obj_flag) + + # 这里是做tag推荐用的, 用来生产推荐的tag unless @obj.nil? @tags = @obj.tag_list $selected_tags.each do |i| @@ -91,35 +48,44 @@ class TagsController < ApplicationController end + + # 增加已选的tag def add_tag @tag = params[:tag] @show_flag = params[:show_flag] + $selected_tags << @tag $related_tags.delete(@tag) - @obj_pages = nil - @numbers = Setting.tags_show_search_results + # # 获取搜索结果 + # @obj,@obj_pages,@users_results, + # @projects_results, + # @issues_results, + # @bids_results, + # @obj_pages = refresh_results(@obj_id,@show_flag) - # @issues_results = get_issues_by_tag($selected_tags) - # @projects_results = get_projects_by_tag($selected_tags) - # @users_results = get_users_by_tag($selected_tags) case @show_flag when '1' then @users_results = get_users_by_tag($selected_tags) @obj = User.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @users_results.count + @obj_pages = Paginator.new @project_count, @limit, params['page'] + @offset ||= @obj_pages.offset @users_results = @users_results.offset(@offset).limit(@limit).all + when '2' then @projects_results = get_projects_by_tag($selected_tags) @obj = Project.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @projects_results.count + @obj_pages = Paginator.new @project_count, @limit, params['page'] + @offset ||= @obj_pages.offset @projects_results = @projects_results.offset(@offset).limit(@limit).order('lft').all @@ -127,44 +93,49 @@ class TagsController < ApplicationController @issues_results = get_issues_by_tag($selected_tags) @obj = Issue.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @issues_results.count + @obj_pages = Paginator.new @project_count, @limit, params['page'] + @offset ||= @obj_pages.offset @issues_results = @issues_results.offset(@offset).limit(@limit).all - when '4' + + when '4' then @bids_results = get_bids_by_tag($selected_tags) @obj = Bid.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @bids_results.count + @obj_pages = Paginator.new @project_count, @limit, params['page'] + @offset ||= @obj_pages.offset - @bids_results = @bids_results.offset(@offset).limit(@limit).all - + @bids_results = @bids_results.offset(@offset).limit(@limit) + + else - @obj = nil + @obj = nil end + end + # 删除已选tag def delete_tag @tag = params[:tag] @show_flag = params[:show_flag] + $related_tags << @tag $selected_tags.delete(@tag) @obj_pages = nil - @numbers = Setting.tags_show_search_results - - # @issues_results = get_issues_by_tag($selected_tags) - # @projects_results = get_projects_by_tag($selected_tags) - # @users_results = get_users_by_tag($selected_tags) + case @show_flag when '1' then @users_results = get_users_by_tag($selected_tags) @obj = User.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @users_results.count @obj_pages = Paginator.new @project_count, @limit, params['page'] @offset ||= @obj_pages.offset @@ -173,7 +144,7 @@ class TagsController < ApplicationController @projects_results = get_projects_by_tag($selected_tags) @obj = Project.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @projects_results.count @obj_pages = Paginator.new @project_count, @limit, params['page'] @offset ||= @obj_pages.offset @@ -183,20 +154,26 @@ class TagsController < ApplicationController @issues_results = get_issues_by_tag($selected_tags) @obj = Issue.find_by_id(@obj_id) - @offset, @limit = api_offset_and_limit({:limit => @numbers}) + @offset, @limit = api_offset_and_limit({:limit => @@numbers}) @project_count = @issues_results.count @obj_pages = Paginator.new @project_count, @limit, params['page'] @offset ||= @obj_pages.offset @issues_results = @issues_results.offset(@offset).limit(@limit).all + when '4' - @bids_results = get_bids_by_tag($selected_tags) + @obj_pages,@bids_results = for_pagination(get_bids_by_tag($selected_tags)) + + # @bids_results = get_bids_by_tag($selected_tags) @obj = Bid.find_by_id(@obj_id) +# + # @offset, @limit = api_offset_and_limit({:limit => @@numbers}) + # @project_count = @bids_results.count + # @obj_pages = Paginator.new @project_count, @limit, params['page'] + # @offset ||= @obj_pages.offset + # @bids_results = @bids_results.offset(@offset).limit(@limit).all + + - @offset, @limit = api_offset_and_limit({:limit => @numbers}) - @project_count = @bids_results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - @bids_results = @bids_results.offset(@offset).limit(@limit).all else @obj = nil @@ -209,22 +186,13 @@ class TagsController < ApplicationController def show_all @tags = ActsAsTaggableOn::Tag.find(:all) end - - def for_pagination(results) - @offset, @limit = api_offset_and_limit({:limit => 2}) - @project_count = results.count - @obj_pages = Paginator.new @project_count, @limit, params['page'] - @offset ||= @obj_pages.offset - results = results.offset(@offset).limit(@limit).order('lft').all - end + #完全从数据库删除tag def delete if params[:q] @tag = ActsAsTaggableOn::Tag.find_by_id(params[:q]) - @tag.delete - + @tag.delete @taggings = ActsAsTaggableOn::Tagging.find_all_by_tag_id(@tag.id) - @taggings.each do |tagging| tagging.delete end @@ -238,4 +206,51 @@ private end + # 这里用来刷新搜索结果的区域 + # 函数的返回值 前2字段用来处理获取其他tag和分页 ,另外4个返回值为过滤结果 + def refresh_results(obj_id,obj_flag) + @users_results = nil + @projects_results = nil + @issues_results = nil + @bids_results = nil + @obj_pages = nil + @obj = nil + # 这里为了提高系统的响应速度 把搜索结果放到case中去了 + case obj_flag + when '1' then + @obj = User.find_by_id(obj_id) + @obj_pages,@users_results = for_pagination(get_users_by_tag($selected_tags)) + when '2' then + @obj = Project.find_by_id(obj_id) + @obj_pages,@projects_results = for_pagination(get_projects_by_tag($selected_tags)) + when '3' then + @obj = Issue.find_by_id(obj_id) + @obj_pages,@issues_results = for_pagination(get_issues_by_tag($selected_tags)) + when '4' then + @obj_pages,@bids_results = for_pagination(get_bids_by_tag($selected_tags)) + @obj = Bid.find_by_id(obj_id) + else + @obj = nil + end + return @obj,@obj_pages,@users_results,@projects_results,@issues_results,@bids_results + end + + def for_pagination(results) + @offset, @limit = api_offset_and_limit({:limit => 3}) # 设置每页显示的个数 + @results_count = results.count + @obj_pages = Paginator.new @results_count, @limit, params['page'] + @offset ||= @obj_pages.offset + results = results.offset(@offset).limit(@limit).all # 这里默认设置为按时间排序 + return @obj_pages,results + end + + #获取有某类对象的tag总数 + def get_tags_size + @issues_tags_num = Issue.tag_counts.size + @projects_tags_num = Project.tag_counts.size + @users_tags_num = User.tag_counts.size + @bids_tags_num = Bid.tag_counts.size + return @users_tags_num,@projects_tags_num,@issues_tags_num,@bids_tags_num + end + end diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index fa212a682..c01afebc1 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -14,10 +14,8 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - class WatchersController < ApplicationController before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] - def watch set_watcher(@watchables, User.current, true) end @@ -86,7 +84,7 @@ class WatchersController < ApplicationController end rescue render_404 - end + end def find_watchables klass = Object.const_get(params[:object_type].camelcase) rescue nil @@ -100,7 +98,27 @@ class WatchersController < ApplicationController def set_watcher(watchables, user, watching) watchables.each do |watchable| watchable.set_watcher(user, watching) - @user = watchable # added by william + # @user = watchable # added by william + if watching + # 修改 user和project的状态 + if watchable.instance_of?(User) + #写user_statuses表 + watchable.user_status.update_watchers_count(1) + elsif watchable.instance_of?(Project) + #写project_statuese表 + watchable.project_status.update_watchers_count(1) + end + else + # 修改 user和project的状态 + if watchable.instance_of?(User) + #写user_statuses表 + watchable.user_status.update_watchers_count(-1) + elsif watchable.instance_of?(Project) + #写project_statuese表 :project_status + watchable.project_status.update_watchers_count(-1) + end + end + end respond_to do |format| format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} diff --git a/app/helpers/bids_helper.rb b/app/helpers/bids_helper.rb index 71f73ff39..784a11379 100644 --- a/app/helpers/bids_helper.rb +++ b/app/helpers/bids_helper.rb @@ -34,7 +34,7 @@ module BidsHelper # this method is used to get all projects that tagged one tag # added by william def get_bids_by_tag(tag_name) - Bid.tagged_with(tag_name) + Bid.tagged_with(tag_name).order('updated_on desc') end end \ No newline at end of file diff --git a/app/helpers/groups_helper.rb b/app/helpers/groups_helper.rb index aa58eb575..b0b507dd4 100644 --- a/app/helpers/groups_helper.rb +++ b/app/helpers/groups_helper.rb @@ -33,7 +33,7 @@ module GroupsHelper s = content_tag('div', principals_check_box_tags('user_ids[]', principals), :id => 'principals') - links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| + links = pagination_links_full(principal_pages, principal_count, :per_page_links => false){|text, parameters, options| link_to text, autocomplete_for_user_group_path(group, parameters.merge(:q => params[:q], :format => 'js')), :remote => true } diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 4ecc33690..f5847abbc 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -377,7 +377,7 @@ module IssuesHelper # this method is used to get all projects that tagged one tag # added by william def get_issues_by_tag(tag_name) - Issue.tagged_with(tag_name) + Issue.tagged_with(tag_name).order('updated_on desc') end end diff --git a/app/helpers/members_helper.rb b/app/helpers/members_helper.rb index aa6b9d436..915e02627 100644 --- a/app/helpers/members_helper.rb +++ b/app/helpers/members_helper.rb @@ -21,7 +21,7 @@ module MembersHelper def render_principals_for_new_members(project) scope = Principal.active.sorted.not_member_of(project).like(params[:q]) principal_count = scope.count - principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page']#by young + principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] #by young principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all s = content_tag('div', principals_check_box_tags('membership[user_ids][]', principals), :id => 'principals') diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index cbe57d1c7..6e87b6a7d 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -85,7 +85,7 @@ module ProjectsHelper # this method is used to get all projects that tagged one tag # added by william def get_projects_by_tag(tag_name) - Project.tagged_with(tag_name) + Project.tagged_with(tag_name).order('updated_on desc') end end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 491112b82..bb854ac43 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -58,7 +58,7 @@ module UsersHelper # this method is used to get all projects that tagged one tag # added by william def get_users_by_tag(tag_name) - User.tagged_with(tag_name) + User.tagged_with(tag_name).order('updated_on desc') end # added by fq diff --git a/app/models/project.rb b/app/models/project.rb index 7af230aa4..31cf1c934 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -56,6 +56,8 @@ class Project < ActiveRecord::Base # end #ADDED BY NIE has_many :project_infos, :dependent => :destroy + has_one :project_status, :class_name => "ProjectStatus", :dependent => :destroy + #end has_one :wiki, :dependent => :destroy # Custom field for the project issues @@ -81,10 +83,10 @@ class Project < ActiveRecord::Base :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}}, :author => nil ############################added by william - acts_as_taggable - scope :by_join_date, order("created_at DESC") + acts_as_taggable + scope :by_join_date, order("created_on DESC") ###################added by liuping 关注 - acts_as_watchable + acts_as_watchable attr_protected :status diff --git a/app/models/project_status.rb b/app/models/project_status.rb new file mode 100644 index 000000000..0e6882693 --- /dev/null +++ b/app/models/project_status.rb @@ -0,0 +1,21 @@ +class ProjectStatus < ActiveRecord::Base + attr_accessible :changesets_count, :watchers_count, :project_id + belongs_to :projects + belongs_to :watchers + belongs_to :changesets + validates_presence_of :project_id + validates_uniqueness_of :project_id + + + # 更新字段 watchers_count 加1 这里没有做用户是否存在的匹配 + # 负责这个表的聂同学 是在新建用户时就新建了该表的记录 + # 但是 如果超级用户删除其他用户的话会造成读取错误 这里是遗漏点 + # 删除用户时 此表创建人员未作相应删除动作 + def update_watchers_count(num) + if self.watchers_count >= 0 + self.update_attribute(:watchers_count, self.watchers_count.to_i + num) + end + end + + +end diff --git a/app/models/user.rb b/app/models/user.rb index 5e6c0963b..7a6f52e57 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -89,6 +89,7 @@ class User < Principal ######added by nie has_many :project_infos, :dependent => :destroy + has_one :user_status, :dependent => :destroy ##### scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") } @@ -99,10 +100,10 @@ class User < Principal acts_as_customizable ############################added by william acts_as_taggable - scope :by_join_date, order("created_at DESC") + scope :by_join_date, order("created_on DESC") ############################# added by liuping 关注 - acts_as_watchable - has_one :user_extensions + acts_as_watchable + has_one :user_extensions,:dependent => :destroy ## end attr_accessor :password, :password_confirmation diff --git a/app/models/user_extension.rb b/app/models/user_extension.rb deleted file mode 100644 index cdfad28c6..000000000 --- a/app/models/user_extension.rb +++ /dev/null @@ -1,11 +0,0 @@ -class UserExtension < ActiveRecord::Base - attr_accessible :birthday, :brief_introduction, :fans_num, :finish_project_num, :follow_num, :gender, :good_num, :location, :occupation, :publish_requirement_num, :user_id, :work_experience, :zip_code - - belongs_to :user - validate :validate_user - - def validate_user - errors.add :user_id, :invalid if user.nil? || !user.active? || User.current != user_id - end - -end diff --git a/app/models/user_status.rb b/app/models/user_status.rb new file mode 100644 index 000000000..ab710bf15 --- /dev/null +++ b/app/models/user_status.rb @@ -0,0 +1,20 @@ +class UserStatus < ActiveRecord::Base + attr_accessible :changesets_count, :user_id, :watchers_count + belongs_to :users + belongs_to :watchers + belongs_to :changesets + validates_presence_of :user_id + validates_uniqueness_of :user_id + + + # 更新字段 watchers_count 加1 这里没有做用户是否存在的匹配 + # 负责这个表的聂同学 是在新建用户时就新建了该表的记录 + # 但是 如果超级用户删除其他用户的话会造成读取错误 这里是遗漏点 + # 删除用户时 此表创建人员未作相应删除动作 + def update_watchers_count(num) + if self.watchers_count >= 0 + self.update_attribute(:watchers_count, self.watchers_count.to_i + num) + end + end + +end diff --git a/app/views/my/account.html.erb b/app/views/my/account.html.erb index c5e591e1a..b0bcd70ea 100644 --- a/app/views/my/account.html.erb +++ b/app/views/my/account.html.erb @@ -69,7 +69,7 @@ <%= render :partial => 'users/preferences' %> -