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:
parent
4896d5c7e8
commit
7222e4012d
@ -30,8 +30,9 @@ class Board < ActiveRecord::Base
|
|||||||
validates_length_of :description, :maximum => 255
|
validates_length_of :description, :maximum => 255
|
||||||
validate :validate_board
|
validate :validate_board
|
||||||
|
|
||||||
scope :visible, lambda {|*args| { :include => :project,
|
scope :visible, lambda {|*args|
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
|
||||||
|
}
|
||||||
|
|
||||||
safe_attributes 'name', 'description', 'parent_id', 'move_to'
|
safe_attributes 'name', 'description', 'parent_id', 'move_to'
|
||||||
|
|
||||||
|
@ -49,9 +49,9 @@ class Changeset < ActiveRecord::Base
|
|||||||
validates_uniqueness_of :revision, :scope => :repository_id
|
validates_uniqueness_of :revision, :scope => :repository_id
|
||||||
validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
|
validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
|
||||||
|
|
||||||
scope :visible,
|
scope :visible, lambda {|*args|
|
||||||
lambda {|*args| { :include => {:repository => :project},
|
includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args))
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } }
|
}
|
||||||
|
|
||||||
after_create :scan_for_issues
|
after_create :scan_for_issues
|
||||||
before_create :before_create_cs
|
before_create :before_create_cs
|
||||||
|
@ -30,8 +30,9 @@ class Document < ActiveRecord::Base
|
|||||||
validates_presence_of :project, :title, :category
|
validates_presence_of :project, :title, :category
|
||||||
validates_length_of :title, :maximum => 60
|
validates_length_of :title, :maximum => 60
|
||||||
|
|
||||||
scope :visible, lambda {|*args| { :include => :project,
|
scope :visible, lambda {|*args|
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
|
||||||
|
}
|
||||||
|
|
||||||
safe_attributes 'category_id', 'title', 'description'
|
safe_attributes 'category_id', 'title', 'description'
|
||||||
|
|
||||||
|
@ -70,18 +70,19 @@ class Issue < ActiveRecord::Base
|
|||||||
validates_numericality_of :estimated_hours, :allow_nil => true
|
validates_numericality_of :estimated_hours, :allow_nil => true
|
||||||
validate :validate_issue, :validate_required_fields
|
validate :validate_issue, :validate_required_fields
|
||||||
|
|
||||||
scope :visible,
|
scope :visible, lambda {|*args|
|
||||||
lambda {|*args| { :include => :project,
|
includes(:project).where(Issue.visible_condition(args.shift || User.current, *args))
|
||||||
:conditions => Issue.visible_condition(args.shift || User.current, *args) } }
|
}
|
||||||
|
|
||||||
scope :open, lambda {|*args|
|
scope :open, lambda {|*args|
|
||||||
is_closed = args.size > 0 ? !args.first : false
|
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 :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") }
|
||||||
scope :on_active_project, lambda { { :include => [:status, :project, :tracker],
|
scope :on_active_project, lambda {
|
||||||
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] } }
|
includes(:status, :project, :tracker).where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE)
|
||||||
|
}
|
||||||
|
|
||||||
before_create :default_assign
|
before_create :default_assign
|
||||||
before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
|
before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
|
||||||
|
@ -45,8 +45,9 @@ class Message < ActiveRecord::Base
|
|||||||
after_update :update_messages_board
|
after_update :update_messages_board
|
||||||
after_destroy :reset_counters!
|
after_destroy :reset_counters!
|
||||||
|
|
||||||
scope :visible, lambda {|*args| { :include => {:board => :project},
|
scope :visible, lambda {|*args|
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
|
||||||
|
}
|
||||||
|
|
||||||
safe_attributes 'subject', 'content'
|
safe_attributes 'subject', 'content'
|
||||||
safe_attributes 'locked', 'sticky', 'board_id',
|
safe_attributes 'locked', 'sticky', 'board_id',
|
||||||
|
@ -34,10 +34,9 @@ class News < ActiveRecord::Base
|
|||||||
|
|
||||||
after_create :add_author_as_watcher
|
after_create :add_author_as_watcher
|
||||||
|
|
||||||
scope :visible, lambda {|*args| {
|
scope :visible, lambda {|*args|
|
||||||
:include => :project,
|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
|
}
|
||||||
}}
|
|
||||||
|
|
||||||
safe_attributes 'title', 'summary', 'description'
|
safe_attributes 'title', 'summary', 'description'
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ class Principal < ActiveRecord::Base
|
|||||||
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
|
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
|
||||||
|
|
||||||
# Groups and active users
|
# 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|
|
scope :like, lambda {|q|
|
||||||
q = q.to_s
|
q = q.to_s
|
||||||
|
@ -84,11 +84,13 @@ class Project < ActiveRecord::Base
|
|||||||
after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
|
after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
|
||||||
before_destroy :delete_all_members
|
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 :has_module, lambda {|mod|
|
||||||
scope :active, lambda { { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}" } }
|
where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
|
||||||
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
}
|
||||||
scope :all_public, lambda { { :conditions => { :is_public => true } } }
|
scope :active, lambda { where(:status => STATUS_ACTIVE) }
|
||||||
scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
|
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|
|
scope :allowed_to, lambda {|*args|
|
||||||
user = User.current
|
user = User.current
|
||||||
permission = nil
|
permission = nil
|
||||||
@ -98,14 +100,14 @@ class Project < ActiveRecord::Base
|
|||||||
user = args.shift
|
user = args.shift
|
||||||
permission = args.shift
|
permission = args.shift
|
||||||
end
|
end
|
||||||
{ :conditions => Project.allowed_to_condition(user, permission, *args) }
|
where(Project.allowed_to_condition(user, permission, *args))
|
||||||
}
|
}
|
||||||
scope :like, lambda {|arg|
|
scope :like, lambda {|arg|
|
||||||
if arg.blank?
|
if arg.blank?
|
||||||
{}
|
where(nil)
|
||||||
else
|
else
|
||||||
pattern = "%#{arg.to_s.strip.downcase}%"
|
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
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,10 +168,8 @@ class Query < ActiveRecord::Base
|
|||||||
user = args.shift || User.current
|
user = args.shift || User.current
|
||||||
base = Project.allowed_to_condition(user, :view_issues, *args)
|
base = Project.allowed_to_condition(user, :view_issues, *args)
|
||||||
user_id = user.logged? ? user.id : 0
|
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],
|
includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
|
||||||
:include => :project
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def initialize(attributes=nil, *args)
|
def initialize(attributes=nil, *args)
|
||||||
|
@ -42,27 +42,24 @@ class TimeEntry < ActiveRecord::Base
|
|||||||
before_validation :set_project_if_nil
|
before_validation :set_project_if_nil
|
||||||
validate :validate_time_entry
|
validate :validate_time_entry
|
||||||
|
|
||||||
scope :visible, lambda {|*args| {
|
scope :visible, lambda {|*args|
|
||||||
:include => :project,
|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
|
}
|
||||||
}}
|
scope :on_issue, lambda {|issue|
|
||||||
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}")
|
||||||
: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|
|
||||||
}}
|
includes(:project).where(project.project_condition(include_subprojects))
|
||||||
scope :on_project, lambda {|project, include_subprojects| {
|
}
|
||||||
:include => :project,
|
|
||||||
:conditions => project.project_condition(include_subprojects)
|
|
||||||
}}
|
|
||||||
scope :spent_between, lambda {|from, to|
|
scope :spent_between, lambda {|from, to|
|
||||||
if 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
|
elsif from
|
||||||
{:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
|
where("#{TimeEntry.table_name}.spent_on >= ?", from)
|
||||||
elsif to
|
elsif to
|
||||||
{:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
|
where("#{TimeEntry.table_name}.spent_on <= ?", to)
|
||||||
else
|
else
|
||||||
{}
|
where(nil)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,8 +82,8 @@ class User < Principal
|
|||||||
has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
|
has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
|
||||||
belongs_to :auth_source
|
belongs_to :auth_source
|
||||||
|
|
||||||
scope :logged, lambda { { :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" } }
|
scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") }
|
||||||
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
|
||||||
|
|
||||||
acts_as_customizable
|
acts_as_customizable
|
||||||
|
|
||||||
|
@ -49,10 +49,10 @@ class WikiPage < ActiveRecord::Base
|
|||||||
before_save :handle_redirects
|
before_save :handle_redirects
|
||||||
|
|
||||||
# eager load information about last updates, without loading text
|
# eager load information about last updates, without loading text
|
||||||
scope :with_updated_on, lambda { {
|
scope :with_updated_on, lambda {
|
||||||
:select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version",
|
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"
|
joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
|
||||||
} }
|
}
|
||||||
|
|
||||||
# Wiki pages that are protected by default
|
# Wiki pages that are protected by default
|
||||||
DEFAULT_PROTECTED_PAGES = %w(sidebar)
|
DEFAULT_PROTECTED_PAGES = %w(sidebar)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user