diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-06-12 19:13:25 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-06-12 19:13:25 +0000 |
commit | f9ddb562d58ae98bcc69f74396b028cbc8cce0b1 (patch) | |
tree | 8019be9178fb3d14c764c04e3e3a5be955de1385 /app/models | |
parent | 136cdc765afda57b9be02704e52b27334da42c73 (diff) | |
download | redmine-f9ddb562d58ae98bcc69f74396b028cbc8cce0b1.tar.gz redmine-f9ddb562d58ae98bcc69f74396b028cbc8cce0b1.zip |
Cleanup of finders with :conditions option.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11963 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/issue.rb | 15 | ||||
-rw-r--r-- | app/models/issue_query.rb | 67 | ||||
-rw-r--r-- | app/models/repository.rb | 25 | ||||
-rw-r--r-- | app/models/repository/bazaar.rb | 14 | ||||
-rw-r--r-- | app/models/repository/cvs.rb | 21 | ||||
-rw-r--r-- | app/models/repository/git.rb | 17 | ||||
-rw-r--r-- | app/models/wiki.rb | 4 |
7 files changed, 78 insertions, 85 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb index bdb9582b7..e68dc02f7 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -812,7 +812,7 @@ class Issue < ActiveRecord::Base # Preloads relations for a collection of issues def self.load_relations(issues) if issues.any? - relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}]) + relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all issues.each do |issue| issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} end @@ -822,7 +822,7 @@ class Issue < ActiveRecord::Base # Preloads visible spent time for a collection of issues def self.load_visible_spent_hours(issues, user=User.current) if issues.any? - hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id) + hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours) issues.each do |issue| issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) end @@ -850,7 +850,7 @@ class Issue < ActiveRecord::Base # Finds an issue relation given its id. def find_relation(relation_id) - IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id]) + IssueRelation.where("issue_to_id = ? OR issue_from_id = ?", id, id).find(relation_id) end # Returns all the other issues that depend on the issue @@ -1350,12 +1350,11 @@ class Issue < ActiveRecord::Base def self.update_versions(conditions=nil) # Only need to update issues with a fixed_version from # a different project and that is not systemwide shared - Issue.scoped(:conditions => conditions).all( - :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" + + Issue.includes(:project, :fixed_version). + where("#{Issue.table_name}.fixed_version_id IS NOT NULL" + " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + - " AND #{Version.table_name}.sharing <> 'system'", - :include => [:project, :fixed_version] - ).each do |issue| + " AND #{Version.table_name}.sharing <> 'system'"). + where(conditions).each do |issue| next if issue.project.nil? || issue.fixed_version.nil? unless issue.project.shared_versions.include?(issue.fixed_version) issue.init_journal(User.current) diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index cf6074f72..f9adf4697 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -226,7 +226,7 @@ class IssueQuery < Query # Returns the issue count def issue_count - Issue.visible.count(:include => [:status, :project], :conditions => statement) + Issue.visible.joins(:status, :project).where(statement).count rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end @@ -237,7 +237,12 @@ class IssueQuery < Query if grouped? begin # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value - r = Issue.visible.count(:joins => joins_for_order_statement(group_by_statement), :group => group_by_statement, :include => [:status, :project], :conditions => statement) + r = Issue.visible. + joins(:status, :project). + where(statement). + joins(joins_for_order_statement(group_by_statement)). + group(group_by_statement). + count rescue ActiveRecord::RecordNotFound r = {nil => issue_count} end @@ -256,14 +261,16 @@ class IssueQuery < Query def issues(options={}) order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) - issues = Issue.visible.where(options[:conditions]).all( - :include => ([:status, :project] + (options[:include] || [])).uniq, - :conditions => statement, - :order => order_option, - :joins => joins_for_order_statement(order_option.join(',')), - :limit => options[:limit], - :offset => options[:offset] - ) + issues = Issue.visible. + joins(:status, :project). + where(statement). + includes(([:status, :project] + (options[:include] || [])).uniq). + where(options[:conditions]). + order(order_option). + joins(joins_for_order_statement(order_option.join(','))). + limit(options[:limit]). + offset(options[:offset]). + all if has_column?(:spent_hours) Issue.load_visible_spent_hours(issues) @@ -280,12 +287,16 @@ class IssueQuery < Query def issue_ids(options={}) order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) - Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, - :conditions => statement, - :order => order_option, - :joins => joins_for_order_statement(order_option.join(',')), - :limit => options[:limit], - :offset => options[:offset]).find_ids + Issue.visible. + joins(:status, :project). + where(statement). + includes(([:status, :project] + (options[:include] || [])).uniq). + where(options[:conditions]). + order(order_option). + joins(joins_for_order_statement(order_option.join(','))). + limit(options[:limit]). + offset(options[:offset]). + find_ids rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end @@ -293,13 +304,14 @@ class IssueQuery < Query # Returns the journals # Valid options are :order, :offset, :limit def journals(options={}) - Journal.visible.all( - :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], - :conditions => statement, - :order => options[:order], - :limit => options[:limit], - :offset => options[:offset] - ) + Journal.visible. + joins(:issue => [:project, :status]). + where(statement). + order(options[:order]). + limit(options[:limit]). + offset(options[:offset]). + preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). + all rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end @@ -307,10 +319,11 @@ class IssueQuery < Query # Returns the versions # Valid options are :conditions def versions(options={}) - Version.visible.where(options[:conditions]).all( - :include => :project, - :conditions => project_statement - ) + Version.visible. + where(project_statement). + where(options[:conditions]). + includes(:project). + all rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end diff --git a/app/models/repository.rb b/app/models/repository.rb index b0e946365..de3ca99a6 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -249,19 +249,18 @@ class Repository < ActiveRecord::Base # Default behaviour is to search in cached changesets def latest_changesets(path, rev, limit=10) if path.blank? - changesets.find( - :all, - :include => :user, - :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", - :limit => limit) + changesets. + reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). + limit(limit). + preload(:user). + all else - filechanges.find( - :all, - :include => {:changeset => :user}, - :conditions => ["path = ?", path.with_leading_slash], - :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", - :limit => limit - ).collect(&:changeset) + filechanges. + where("path = ?", path.with_leading_slash). + reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). + limit(limit). + preload(:changeset => :user). + collect(&:changeset) end end @@ -393,7 +392,7 @@ class Repository < ActiveRecord::Base end def set_as_default? - new_record? && project && !Repository.first(:conditions => {:project_id => project.id}) + new_record? && project && Repository.where(:project_id => project.id).empty? end protected diff --git a/app/models/repository/bazaar.rb b/app/models/repository/bazaar.rb index ea20fe37d..135be83ca 100644 --- a/app/models/repository/bazaar.rb +++ b/app/models/repository/bazaar.rb @@ -68,15 +68,11 @@ class Repository::Bazaar < Repository full_path = File.join(root_url, e.path) e.size = File.stat(full_path).size if File.file?(full_path) end - c = Change.find( - :first, - :include => :changeset, - :conditions => [ - "#{Change.table_name}.revision = ? and #{Changeset.table_name}.repository_id = ?", - e.lastrev.revision, - id - ], - :order => "#{Changeset.table_name}.revision DESC") + c = Change. + includes(:changeset). + where("#{Change.table_name}.revision = ? and #{Changeset.table_name}.repository_id = ?", e.lastrev.revision, id). + order("#{Changeset.table_name}.revision DESC"). + first if c e.lastrev.identifier = c.changeset.revision e.lastrev.name = c.changeset.revision diff --git a/app/models/repository/cvs.rb b/app/models/repository/cvs.rb index 155ac10ed..7bae8c091 100644 --- a/app/models/repository/cvs.rb +++ b/app/models/repository/cvs.rb @@ -143,14 +143,11 @@ class Repository::Cvs < Repository ) cmt = Changeset.normalize_comments(revision.message, repo_log_encoding) author_utf8 = Changeset.to_utf8(revision.author, repo_log_encoding) - cs = changesets.find( - :first, - :conditions => { - :committed_on => tmp_time - time_delta .. tmp_time + time_delta, - :committer => author_utf8, - :comments => cmt - } - ) + cs = changesets.where( + :committed_on => tmp_time - time_delta .. tmp_time + time_delta, + :committer => author_utf8, + :comments => cmt + ).first # create a new changeset.... unless cs # we use a temporaray revision number here (just for inserting) @@ -185,10 +182,10 @@ class Repository::Cvs < Repository end # Renumber new changesets in chronological order - Changeset.all( - :order => 'committed_on ASC, id ASC', - :conditions => ["repository_id = ? AND revision LIKE 'tmp%'", id] - ).each do |changeset| + Changeset. + order('committed_on ASC, id ASC'). + where("repository_id = ? AND revision LIKE 'tmp%'", id). + each do |changeset| changeset.update_attribute :revision, next_revision_number end end # transaction diff --git a/app/models/repository/git.rb b/app/models/repository/git.rb index c1f0020eb..9b3617ba9 100644 --- a/app/models/repository/git.rb +++ b/app/models/repository/git.rb @@ -191,13 +191,8 @@ class Repository::Git < Repository offset = 0 revisions_copy = revisions.clone # revisions will change while offset < revisions_copy.size - recent_changesets_slice = changesets.find( - :all, - :conditions => [ - 'scmid IN (?)', - revisions_copy.slice(offset, limit).map{|x| x.scmid} - ] - ) + scmids = revisions_copy.slice(offset, limit).map{|x| x.scmid} + recent_changesets_slice = changesets.where(:scmid => scmids).all # Subtract revisions that redmine already knows about recent_revisions = recent_changesets_slice.map{|c| c.scmid} revisions.reject!{|r| recent_revisions.include?(r.scmid)} @@ -246,13 +241,7 @@ class Repository::Git < Repository revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false) return [] if revisions.nil? || revisions.empty? - changesets.find( - :all, - :conditions => [ - "scmid IN (?)", - revisions.map!{|c| c.scmid} - ] - ) + changesets.where(:scmid => revisions.map {|c| c.scmid}).all end def clear_extra_info_of_changesets diff --git a/app/models/wiki.rb b/app/models/wiki.rb index d58212d68..dabe19a4e 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -50,10 +50,10 @@ class Wiki < ActiveRecord::Base @page_found_with_redirect = false title = start_page if title.blank? title = Wiki.titleize(title) - page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title]) + page = pages.where("LOWER(title) = LOWER(?)", title).first if !page && !(options[:with_redirect] == false) # search for a redirect - redirect = redirects.first(:conditions => ["LOWER(title) = LOWER(?)", title]) + redirect = redirects.where("LOWER(title) = LOWER(?)", title).first if redirect page = find_page(redirect.redirects_to, :with_redirect => false) @page_found_with_redirect = true |