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