]> source.dussan.org Git - redmine.git/commitdiff
Rewrites named scopes with ARel queries.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 7 Dec 2012 17:59:20 +0000 (17:59 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 7 Dec 2012 17:59:20 +0000 (17:59 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10950 e93f8b46-1217-0410-a6f0-8f06a7374b81

12 files changed:
app/models/board.rb
app/models/changeset.rb
app/models/document.rb
app/models/issue.rb
app/models/message.rb
app/models/news.rb
app/models/principal.rb
app/models/project.rb
app/models/query.rb
app/models/time_entry.rb
app/models/user.rb
app/models/wiki_page.rb

index 42b5499e182e9cbac4bc04cc086fefcab055942d..f5efa708790c0035ed5c28f4b1aab10d14aa4c2c 100644 (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'
 
index e9853e68d8563c44a4b468ce2319546a254f122a..7539dad9d1c99c9b85024bbcf0b1653062dfec47 100644 (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
index 3fd43e1c0c139363b0d4f5e3f27c03af646183a4..fce157bddb3226ab80da3ac81abef8efbac8e539 100644 (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'
 
index 7ba0ba207284f686e9732f6c6a0e07e19e0878d0..c62592e26f6ddab59eb5684f49b0542ea9841b61 100644 (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
index 80fff2cf98815150298b8073d63f6902b401c1db..f41d48f2fe75a9aa6e2c870d8c23a8fca69c941a 100644 (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',
index 1810baa3324bb7b0c1c45a153173dac157623664..24fc6ec48a8e81d44ad59c434aa5c0c281b07762 100644 (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'
 
index 044f256ad4cac61400fdd268a984e425266481ef..f467f847f5e315d1cbdd78fb6b4303850fdcac35 100644 (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
index 5e26bd7f67bf6e772906b1f881e0ce091c2418f7..acd5f5567e3d25bbd74bead809e67e73e26e54a5 100644 (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
   }
 
index a4c9a53ed769dc39763defa55b372e6f470449e0..381d18745422d91e2608cf916ad7e6bc1176ca9e 100644 (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)
index ef085d875626b04681828220a89f6a5269e05259..3abe114c0e06047e9c834af8cd539b16294bc948 100644 (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
   }
 
index 32b7d4499ffac7b60167cf1383b64dd62ac61817..eed359feabf7476e0e8b94f1c38e752dd1a4479f 100644 (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
 
index 45954a08d7dd283611d09fd5fa234c9932253320..31b8845a3ef27c34678f1cab4430dccfce8ab9d4 100644 (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)