From 9395d53849040fb6dacc3de2ea67e563cd16d80e Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 29 Apr 2011 12:26:47 +0200 Subject: [PATCH] SONAR-2327, SONAR-2382 Refactor code Purpose: review service and web service use the same DB request method --- .../app/controllers/reviews_controller.rb | 41 ++++++++----------- .../main/webapp/WEB-INF/app/models/review.rb | 26 ++++++------ 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb index c037867e9c3..105830de552 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb @@ -298,40 +298,31 @@ class ReviewsController < ApplicationController end def search_reviews - conditions=['review_type<>:not_type'] - values={:not_type => Review::TYPE_FALSE_POSITIVE} - - unless @statuses == [""] - conditions << "status in (:statuses)" - values[:statuses]=@statuses + options = {} + unless @statuses == [''] + options['statuses']=@statuses.join(',') end - unless @projects == [""] - conditions << "project_id in (:projects)" - values[:projects]=@projects + unless @projects == [''] + options['projects']=@projects.join(',') end - unless @severities == [""] - conditions << "severity in (:severities)" - values[:severities]=@severities + unless @severities == [''] + options['severities']=@severities.join(',') end - unless @authors == [""] - conditions << "user_id in (:authors)" - values[:authors]=@authors.map{|s| s.to_i} + unless @authors == [''] + options['authors']=@authors.map{|s| s.to_i}.join(',') end - unless @assignees == [""] - conditions << "assignee_id in (:assignees)" - values[:assignees]=@assignees.map{|s| s.to_i} + unless @assignees == [''] + options['assignees']=@assignees.map{|s| s.to_i}.join(',') end - unless @id == "" + unless @id == '' if is_number? @id - conditions << "id = :id" - values[:id] = @id + options['id'] = @id else - conditions << "id = :id" - values[:id] = -1 + options['id'] = -1 end end - - @reviews = Review.find( :all, :order => "created_at DESC", :conditions => [ conditions.join(" AND "), values] ).uniq + + @reviews = Review.search(options) end def is_number?(s) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb index 90ef876cbeb..115f4b7b58f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb @@ -55,7 +55,7 @@ class Review < ActiveRecord::Base review_type = options['review_type'] if review_type - conditions << "review_type=:type" + conditions << 'review_type=:type' values[:type] = review_type.upcase else conditions=['review_type<>:not_type'] @@ -63,17 +63,17 @@ class Review < ActiveRecord::Base end ids=options['ids'].split(',') if options['ids'] - if options[:id] - conditions << "id=:id" - values[:id]=options[:id].to_i + if options['id'] + conditions << 'id=:id' + values[:id]=options['id'].to_i elsif ids && ids.size>0 && !ids[0].blank? - conditions << "id in (:ids)" + conditions << 'id in (:ids)' values[:ids]=ids.map{|id| id.to_i} end projects=options['projects'].split(',') if options['projects'] if projects && projects.size>0 && !projects[0].blank? - conditions << "project_id in (:projects)" + conditions << 'project_id in (:projects)' projectIds = [] projects.each do |project| foundProject = Project.by_key(project) @@ -84,7 +84,7 @@ class Review < ActiveRecord::Base resources=options['resources'].split(',') if options['resources'] if resources && resources.size>0 && !resources[0].blank? - conditions << "resource_id in (:resources)" + conditions << 'resource_id in (:resources)' resourceIds = [] resources.each do |resource| foundResource = Project.by_key(resource) @@ -95,19 +95,19 @@ class Review < ActiveRecord::Base statuses=options['statuses'].split(',') if options['statuses'] if statuses && statuses.size>0 && !statuses[0].blank? - conditions << "status in (:statuses)" + conditions << 'status in (:statuses)' values[:statuses]=statuses end - + severities=options['severities'].split(',') if options['severities'] if severities && severities.size>0 && !severities[0].blank? - conditions << "severity in (:severities)" + conditions << 'severity in (:severities)' values[:severities]=severities end authors=options['authors'].split(',') if options['authors'] if authors && authors.size>0 && !authors[0].blank? - conditions << "user_id in (:authors)" + conditions << 'user_id in (:authors)' unless is_number?(authors[0]) authors=User.logins_to_ids(authors) end @@ -116,14 +116,14 @@ class Review < ActiveRecord::Base assignees=options['assignees'].split(',') if options['assignees'] if assignees && assignees.size>0 && !assignees[0].blank? - conditions << "assignee_id in (:assignees)" + conditions << 'assignee_id in (:assignees)' unless is_number?(assignees[0]) assignees=User.logins_to_ids(assignees) end values[:assignees]=assignees end - Review.find(:all, :include => [ 'review_comments' ], :order => "created_at DESC", :conditions => [conditions.join(" AND "), values], :limit => 200) + Review.find(:all, :include => [ 'review_comments' ], :order => 'created_at DESC', :conditions => [conditions.join(' AND '), values], :limit => 200) end private -- 2.39.5