From: Jean-Philippe Lang Date: Fri, 7 Dec 2012 17:59:20 +0000 (+0000) Subject: Rewrites named scopes with ARel queries. X-Git-Tag: 2.3.0~483 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7222e4012db58c53d364e3b065e70338c094e878;p=redmine.git Rewrites named scopes with ARel queries. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10950 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/board.rb b/app/models/board.rb index 42b5499e1..f5efa7087 100644 --- a/app/models/board.rb +++ b/app/models/board.rb @@ -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' diff --git a/app/models/changeset.rb b/app/models/changeset.rb index e9853e68d..7539dad9d 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -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 diff --git a/app/models/document.rb b/app/models/document.rb index 3fd43e1c0..fce157bdd 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -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' diff --git a/app/models/issue.rb b/app/models/issue.rb index 7ba0ba207..c62592e26 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 80fff2cf9..f41d48f2f 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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', diff --git a/app/models/news.rb b/app/models/news.rb index 1810baa33..24fc6ec48 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -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' diff --git a/app/models/principal.rb b/app/models/principal.rb index 044f256ad..f467f847f 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index 5e26bd7f6..acd5f5567 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 } diff --git a/app/models/query.rb b/app/models/query.rb index a4c9a53ed..381d18745 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -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) diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index ef085d875..3abe114c0 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -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 } diff --git a/app/models/user.rb b/app/models/user.rb index 32b7d4499..eed359fea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 45954a08d..31b8845a3 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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)