Rewrites named scopes with ARel queries.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10950 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-12-07 17:59:20 +00:00
parent 4896d5c7e8
commit 7222e4012d
12 changed files with 55 additions and 55 deletions

View File

@ -30,8 +30,9 @@ class Board < ActiveRecord::Base
validates_length_of :description, :maximum => 255
validate :validate_board
scope :visible, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
scope :visible, lambda {|*args|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
}
safe_attributes 'name', 'description', 'parent_id', 'move_to'

View File

@ -49,9 +49,9 @@ class Changeset < ActiveRecord::Base
validates_uniqueness_of :revision, :scope => :repository_id
validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
scope :visible,
lambda {|*args| { :include => {:repository => :project},
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } }
scope :visible, lambda {|*args|
includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args))
}
after_create :scan_for_issues
before_create :before_create_cs

View File

@ -30,8 +30,9 @@ class Document < ActiveRecord::Base
validates_presence_of :project, :title, :category
validates_length_of :title, :maximum => 60
scope :visible, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
scope :visible, lambda {|*args|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
}
safe_attributes 'category_id', 'title', 'description'

View File

@ -70,18 +70,19 @@ class Issue < ActiveRecord::Base
validates_numericality_of :estimated_hours, :allow_nil => true
validate :validate_issue, :validate_required_fields
scope :visible,
lambda {|*args| { :include => :project,
:conditions => Issue.visible_condition(args.shift || User.current, *args) } }
scope :visible, lambda {|*args|
includes(:project).where(Issue.visible_condition(args.shift || User.current, *args))
}
scope :open, lambda {|*args|
is_closed = args.size > 0 ? !args.first : false
{:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
includes(:status).where("#{IssueStatus.table_name}.is_closed = ?", is_closed)
}
scope :recently_updated, lambda { { :order => "#{Issue.table_name}.updated_on DESC" } }
scope :on_active_project, lambda { { :include => [:status, :project, :tracker],
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] } }
scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") }
scope :on_active_project, lambda {
includes(:status, :project, :tracker).where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE)
}
before_create :default_assign
before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change

View File

@ -45,8 +45,9 @@ class Message < ActiveRecord::Base
after_update :update_messages_board
after_destroy :reset_counters!
scope :visible, lambda {|*args| { :include => {:board => :project},
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
scope :visible, lambda {|*args|
includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
}
safe_attributes 'subject', 'content'
safe_attributes 'locked', 'sticky', 'board_id',

View File

@ -34,10 +34,9 @@ class News < ActiveRecord::Base
after_create :add_author_as_watcher
scope :visible, lambda {|*args| {
:include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
}}
scope :visible, lambda {|*args|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
}
safe_attributes 'title', 'summary', 'description'

View File

@ -24,7 +24,7 @@ class Principal < ActiveRecord::Base
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
# Groups and active users
scope :active, lambda { { :conditions => "#{Principal.table_name}.status = 1" } }
scope :active, lambda { where("#{Principal.table_name}.status = 1") }
scope :like, lambda {|q|
q = q.to_s

View File

@ -84,11 +84,13 @@ class Project < ActiveRecord::Base
after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
before_destroy :delete_all_members
scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
scope :active, lambda { { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}" } }
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
scope :all_public, lambda { { :conditions => { :is_public => true } } }
scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
scope :has_module, lambda {|mod|
where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
}
scope :active, lambda { where(:status => STATUS_ACTIVE) }
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
scope :all_public, lambda { where(:is_public => true) }
scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
scope :allowed_to, lambda {|*args|
user = User.current
permission = nil
@ -98,14 +100,14 @@ class Project < ActiveRecord::Base
user = args.shift
permission = args.shift
end
{ :conditions => Project.allowed_to_condition(user, permission, *args) }
where(Project.allowed_to_condition(user, permission, *args))
}
scope :like, lambda {|arg|
if arg.blank?
{}
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
{:conditions => ["LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", {:p => pattern}]}
where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern)
end
}

View File

@ -168,10 +168,8 @@ class Query < ActiveRecord::Base
user = args.shift || User.current
base = Project.allowed_to_condition(user, :view_issues, *args)
user_id = user.logged? ? user.id : 0
{
:conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id],
:include => :project
}
includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
}
def initialize(attributes=nil, *args)

View File

@ -42,27 +42,24 @@ class TimeEntry < ActiveRecord::Base
before_validation :set_project_if_nil
validate :validate_time_entry
scope :visible, lambda {|*args| {
:include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
}}
scope :on_issue, lambda {|issue| {
:include => :issue,
:conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
}}
scope :on_project, lambda {|project, include_subprojects| {
:include => :project,
:conditions => project.project_condition(include_subprojects)
}}
scope :visible, lambda {|*args|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
}
scope :on_issue, lambda {|issue|
includes(:issue).where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
}
scope :on_project, lambda {|project, include_subprojects|
includes(:project).where(project.project_condition(include_subprojects))
}
scope :spent_between, lambda {|from, to|
if from && to
{:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
elsif from
{:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
where("#{TimeEntry.table_name}.spent_on >= ?", from)
elsif to
{:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
where("#{TimeEntry.table_name}.spent_on <= ?", to)
else
{}
where(nil)
end
}

View File

@ -82,8 +82,8 @@ class User < Principal
has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
belongs_to :auth_source
scope :logged, lambda { { :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" } }
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") }
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
acts_as_customizable

View File

@ -49,10 +49,10 @@ class WikiPage < ActiveRecord::Base
before_save :handle_redirects
# eager load information about last updates, without loading text
scope :with_updated_on, lambda { {
:select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version",
:joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
} }
scope :with_updated_on, lambda {
select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
}
# Wiki pages that are protected by default
DEFAULT_PROTECTED_PAGES = %w(sidebar)