##// END OF EJS Templates
Rewrites named scopes with ARel queries....
Jean-Philippe Lang -
r10723:7222e4012db5
parent child
Show More
@@ -30,8 +30,9 class Board < ActiveRecord::Base
30 30 validates_length_of :description, :maximum => 255
31 31 validate :validate_board
32 32
33 scope :visible, lambda {|*args| { :include => :project,
34 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
33 scope :visible, lambda {|*args|
34 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
35 }
35 36
36 37 safe_attributes 'name', 'description', 'parent_id', 'move_to'
37 38
@@ -49,9 +49,9 class Changeset < ActiveRecord::Base
49 49 validates_uniqueness_of :revision, :scope => :repository_id
50 50 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
51 51
52 scope :visible,
53 lambda {|*args| { :include => {:repository => :project},
54 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } }
52 scope :visible, lambda {|*args|
53 includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args))
54 }
55 55
56 56 after_create :scan_for_issues
57 57 before_create :before_create_cs
@@ -30,8 +30,9 class Document < ActiveRecord::Base
30 30 validates_presence_of :project, :title, :category
31 31 validates_length_of :title, :maximum => 60
32 32
33 scope :visible, lambda {|*args| { :include => :project,
34 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
33 scope :visible, lambda {|*args|
34 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
35 }
35 36
36 37 safe_attributes 'category_id', 'title', 'description'
37 38
@@ -70,18 +70,19 class Issue < ActiveRecord::Base
70 70 validates_numericality_of :estimated_hours, :allow_nil => true
71 71 validate :validate_issue, :validate_required_fields
72 72
73 scope :visible,
74 lambda {|*args| { :include => :project,
75 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
73 scope :visible, lambda {|*args|
74 includes(:project).where(Issue.visible_condition(args.shift || User.current, *args))
75 }
76 76
77 77 scope :open, lambda {|*args|
78 78 is_closed = args.size > 0 ? !args.first : false
79 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
79 includes(:status).where("#{IssueStatus.table_name}.is_closed = ?", is_closed)
80 80 }
81 81
82 scope :recently_updated, lambda { { :order => "#{Issue.table_name}.updated_on DESC" } }
83 scope :on_active_project, lambda { { :include => [:status, :project, :tracker],
84 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] } }
82 scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") }
83 scope :on_active_project, lambda {
84 includes(:status, :project, :tracker).where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE)
85 }
85 86
86 87 before_create :default_assign
87 88 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
@@ -45,8 +45,9 class Message < ActiveRecord::Base
45 45 after_update :update_messages_board
46 46 after_destroy :reset_counters!
47 47
48 scope :visible, lambda {|*args| { :include => {:board => :project},
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
48 scope :visible, lambda {|*args|
49 includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
50 }
50 51
51 52 safe_attributes 'subject', 'content'
52 53 safe_attributes 'locked', 'sticky', 'board_id',
@@ -34,10 +34,9 class News < ActiveRecord::Base
34 34
35 35 after_create :add_author_as_watcher
36 36
37 scope :visible, lambda {|*args| {
38 :include => :project,
39 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
40 }}
37 scope :visible, lambda {|*args|
38 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
39 }
41 40
42 41 safe_attributes 'title', 'summary', 'description'
43 42
@@ -24,7 +24,7 class Principal < ActiveRecord::Base
24 24 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
25 25
26 26 # Groups and active users
27 scope :active, lambda { { :conditions => "#{Principal.table_name}.status = 1" } }
27 scope :active, lambda { where("#{Principal.table_name}.status = 1") }
28 28
29 29 scope :like, lambda {|q|
30 30 q = q.to_s
@@ -84,11 +84,13 class Project < ActiveRecord::Base
84 84 after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
85 85 before_destroy :delete_all_members
86 86
87 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] } }
88 scope :active, lambda { { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}" } }
89 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
90 scope :all_public, lambda { { :conditions => { :is_public => true } } }
91 scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
87 scope :has_module, lambda {|mod|
88 where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
89 }
90 scope :active, lambda { where(:status => STATUS_ACTIVE) }
91 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
92 scope :all_public, lambda { where(:is_public => true) }
93 scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
92 94 scope :allowed_to, lambda {|*args|
93 95 user = User.current
94 96 permission = nil
@@ -98,14 +100,14 class Project < ActiveRecord::Base
98 100 user = args.shift
99 101 permission = args.shift
100 102 end
101 { :conditions => Project.allowed_to_condition(user, permission, *args) }
103 where(Project.allowed_to_condition(user, permission, *args))
102 104 }
103 105 scope :like, lambda {|arg|
104 106 if arg.blank?
105 {}
107 where(nil)
106 108 else
107 109 pattern = "%#{arg.to_s.strip.downcase}%"
108 {:conditions => ["LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", {:p => pattern}]}
110 where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern)
109 111 end
110 112 }
111 113
@@ -168,10 +168,8 class Query < ActiveRecord::Base
168 168 user = args.shift || User.current
169 169 base = Project.allowed_to_condition(user, :view_issues, *args)
170 170 user_id = user.logged? ? user.id : 0
171 {
172 :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id],
173 :include => :project
174 }
171
172 includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
175 173 }
176 174
177 175 def initialize(attributes=nil, *args)
@@ -42,27 +42,24 class TimeEntry < ActiveRecord::Base
42 42 before_validation :set_project_if_nil
43 43 validate :validate_time_entry
44 44
45 scope :visible, lambda {|*args| {
46 :include => :project,
47 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
48 }}
49 scope :on_issue, lambda {|issue| {
50 :include => :issue,
51 :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
52 }}
53 scope :on_project, lambda {|project, include_subprojects| {
54 :include => :project,
55 :conditions => project.project_condition(include_subprojects)
56 }}
45 scope :visible, lambda {|*args|
46 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
47 }
48 scope :on_issue, lambda {|issue|
49 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}")
50 }
51 scope :on_project, lambda {|project, include_subprojects|
52 includes(:project).where(project.project_condition(include_subprojects))
53 }
57 54 scope :spent_between, lambda {|from, to|
58 55 if from && to
59 {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
56 where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
60 57 elsif from
61 {:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
58 where("#{TimeEntry.table_name}.spent_on >= ?", from)
62 59 elsif to
63 {:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
60 where("#{TimeEntry.table_name}.spent_on <= ?", to)
64 61 else
65 {}
62 where(nil)
66 63 end
67 64 }
68 65
@@ -82,8 +82,8 class User < Principal
82 82 has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
83 83 belongs_to :auth_source
84 84
85 scope :logged, lambda { { :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" } }
86 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
85 scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") }
86 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
87 87
88 88 acts_as_customizable
89 89
@@ -49,10 +49,10 class WikiPage < ActiveRecord::Base
49 49 before_save :handle_redirects
50 50
51 51 # eager load information about last updates, without loading text
52 scope :with_updated_on, lambda { {
53 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version",
54 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
55 } }
52 scope :with_updated_on, lambda {
53 select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
54 joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
55 }
56 56
57 57 # Wiki pages that are protected by default
58 58 DEFAULT_PROTECTED_PAGES = %w(sidebar)
General Comments 0
You need to be logged in to leave comments. Login now