Skip to content

Commit

Permalink
Cleanup of finders with :conditions option.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11963 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
jplang committed Jun 12, 2013
1 parent 136cdc7 commit f9ddb56
Show file tree
Hide file tree
Showing 37 changed files with 155 additions and 170 deletions.
3 changes: 1 addition & 2 deletions app/controllers/context_menus_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ def issues
end

def time_entries
@time_entries = TimeEntry.all(
:conditions => {:id => params[:ids]}, :include => :project)
@time_entries = TimeEntry.where(:id => params[:ids]).preload(:project).to_a
(render_404; return) unless @time_entries.present?

@projects = @time_entries.collect(&:project).compact.uniq
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def bulk_update
end

def destroy
@hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
@hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
if @hours > 0
case params[:todo]
when 'destroy'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def show
page = params[:page]
# Find the page of the requested reply
if params[:r] && page.nil?
offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count
page = 1 + offset / REPLIES_PER_PAGE
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def show
@total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)

if User.current.allowed_to?(:view_time_entries, @project)
@total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
@total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
end

@key = User.current.rss_key
Expand Down
19 changes: 11 additions & 8 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,18 @@ def graph_commits_per_month(repository)
@date_to = Date.today
@date_from = @date_to << 11
@date_from = Date.civil(@date_from.year, @date_from.month, 1)
commits_by_day = Changeset.count(
:all, :group => :commit_date,
:conditions => ["repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
commits_by_day = Changeset.
where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
group(:commit_date).
count
commits_by_month = [0] * 12
commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }

changes_by_day = Change.count(
:all, :group => :commit_date, :include => :changeset,
:conditions => ["#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
changes_by_day = Change.
joins(:changeset).
where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
group(:commit_date).
count
changes_by_month = [0] * 12
changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }

Expand Down Expand Up @@ -393,10 +396,10 @@ def graph_commits_per_month(repository)
end

def graph_commits_per_author(repository)
commits_by_author = Changeset.count(:all, :group => :committer, :conditions => ["repository_id = ?", repository.id])
commits_by_author = Changeset.where("repository_id = ?", repository.id).group(:committer).count
commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}

changes_by_author = Change.count(:all, :group => :committer, :include => :changeset, :conditions => ["#{Changeset.table_name}.repository_id = ?", repository.id])
changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", repository.id).group(:committer).count
h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}

fields = commits_by_author.collect {|r| r.first}
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/sys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ class SysController < ActionController::Base
before_filter :check_enabled

def projects
p = Project.active.has_module(:repository).find(
:all,
:include => :repository,
:order => "#{Project.table_name}.identifier"
)
p = Project.active.has_module(:repository).order("#{Project.table_name}.identifier").preload(:repository).all
# extra_info attribute from repository breaks activeresource client
render :xml => p.to_xml(
:only => [:id, :identifier, :name, :is_public, :status],
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def index

def show
# show projects based on current user visibility
@memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
@memberships = @user.memberships.where(Project.visible_condition(User.current)).all

events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
@events_by_day = events.group_by(&:event_date)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def index

@issues_by_version = {}
if @selected_tracker_ids.any? && @versions.any?
issues = Issue.visible.all(
:include => [:project, :status, :tracker, :priority, :fixed_version],
:conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)},
:order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id"
)
issues = Issue.visible.
includes(:project, :tracker).
preload(:status, :priority, :fixed_version).
where(:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)).
order("#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
@issues_by_version = issues.group_by(&:fixed_version)
end
@versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/queries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def retrieve_query
if !params[:query_id].blank?
cond = "project_id IS NULL"
cond << " OR project_id = #{@project.id}" if @project
@query = IssueQuery.find(params[:query_id], :conditions => cond)
@query = IssueQuery.where(cond).find(params[:query_id])
raise ::Unauthorized unless @query.visible?
@query.project = @project
session[:query] = {:id => @query.id, :project_id => @query.project_id}
Expand Down
7 changes: 2 additions & 5 deletions app/helpers/versions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ def render_issue_status_by(version, criteria)
h = Hash.new {|k,v| k[v] = [0, 0]}
begin
# Total issue count
Issue.count(:group => criteria,
:conditions => ["#{Issue.table_name}.fixed_version_id = ?", version.id]).each {|c,s| h[c][0] = s}
Issue.where(:fixed_version_id => version.id).group(criteria).count.each {|c,s| h[c][0] = s}
# Open issues count
Issue.count(:group => criteria,
:include => :status,
:conditions => ["#{Issue.table_name}.fixed_version_id = ? AND #{IssueStatus.table_name}.is_closed = ?", version.id, false]).each {|c,s| h[c][1] = s}
Issue.open.where(:fixed_version_id => version.id).group(criteria).count.each {|c,s| h[c][1] = s}
rescue ActiveRecord::RecordNotFound
# When grouping by an association, Rails throws this exception if there's no result (bug)
end
Expand Down
15 changes: 7 additions & 8 deletions app/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ def relations
# 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
Expand All @@ -822,7 +822,7 @@ def self.load_relations(issues)
# 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
Expand Down Expand Up @@ -850,7 +850,7 @@ def self.load_visible_relations(issues, user=User.current)

# 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
Expand Down Expand Up @@ -1350,12 +1350,11 @@ def recalculate_attributes_for(issue_id)
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)
Expand Down
67 changes: 40 additions & 27 deletions app/models/issue_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def default_columns_names

# 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
Expand All @@ -237,7 +237,12 @@ def issue_count_by_group
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
Expand All @@ -256,14 +261,16 @@ def issue_count_by_group
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)
Expand All @@ -280,37 +287,43 @@ def issues(options={})
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

# 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

# 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
Expand Down
25 changes: 12 additions & 13 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,18 @@ def latest_changeset
# 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

Expand Down Expand Up @@ -393,7 +392,7 @@ def self.scm_available
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
Expand Down
14 changes: 5 additions & 9 deletions app/models/repository/bazaar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@ def entries(path=nil, identifier=nil)
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
Expand Down
21 changes: 9 additions & 12 deletions app/models/repository/cvs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,11 @@ def fetch_changesets
)
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)
Expand Down Expand Up @@ -185,10 +182,10 @@ def fetch_changesets
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
Expand Down
Loading

0 comments on commit f9ddb56

Please sign in to comment.