model: replace Rails2 "named_scope" to Rails3 "scope"
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9537 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
71649ba2f1
commit
d0d01d4e70
@ -28,7 +28,7 @@ class Board < ActiveRecord::Base
|
|||||||
validates_length_of :name, :maximum => 30
|
validates_length_of :name, :maximum => 30
|
||||||
validates_length_of :description, :maximum => 255
|
validates_length_of :description, :maximum => 255
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| { :include => :project,
|
scope :visible, lambda {|*args| { :include => :project,
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
||||||
|
|
||||||
safe_attributes 'name', 'description', 'move_to'
|
safe_attributes 'name', 'description', 'move_to'
|
||||||
|
@ -49,7 +49,8 @@ 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
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| { :include => {:repository => :project},
|
scope :visible,
|
||||||
|
lambda {|*args| { :include => {:repository => :project},
|
||||||
:conditions => 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
|
||||||
|
@ -30,7 +30,7 @@ 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
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| { :include => :project,
|
scope :visible, lambda {|*args| { :include => :project,
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
|
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
|
||||||
|
|
||||||
safe_attributes 'category_id', 'title', 'description'
|
safe_attributes 'category_id', 'title', 'description'
|
||||||
|
@ -35,9 +35,9 @@ class Enumeration < ActiveRecord::Base
|
|||||||
validates_uniqueness_of :name, :scope => [:type, :project_id]
|
validates_uniqueness_of :name, :scope => [:type, :project_id]
|
||||||
validates_length_of :name, :maximum => 30
|
validates_length_of :name, :maximum => 30
|
||||||
|
|
||||||
named_scope :shared, :conditions => { :project_id => nil }
|
scope :shared, :conditions => { :project_id => nil }
|
||||||
named_scope :active, :conditions => { :active => true }
|
scope :active, :conditions => { :active => true }
|
||||||
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
||||||
|
|
||||||
def self.default
|
def self.default
|
||||||
# Creates a fake default scope so Enumeration.default will check
|
# Creates a fake default scope so Enumeration.default will check
|
||||||
|
@ -60,18 +60,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 :validate_issue
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| { :include => :project,
|
scope :visible,
|
||||||
:conditions => Issue.visible_condition(args.shift || User.current, *args) } }
|
lambda {|*args| { :include => :project,
|
||||||
|
:conditions => Issue.visible_condition(args.shift || User.current, *args) } }
|
||||||
|
|
||||||
named_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}
|
{:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
|
||||||
}
|
}
|
||||||
|
|
||||||
named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
|
scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
|
||||||
named_scope :with_limit, lambda { |limit| { :limit => limit} }
|
scope :with_limit, lambda { |limit| { :limit => limit} }
|
||||||
named_scope :on_active_project, :include => [:status, :project, :tracker],
|
scope :on_active_project, :include => [:status, :project, :tracker],
|
||||||
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
|
:conditions => ["#{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
|
before_save :close_duplicates, :update_done_ratio_from_issue_status
|
||||||
|
@ -27,7 +27,7 @@ class IssueCategory < ActiveRecord::Base
|
|||||||
|
|
||||||
safe_attributes 'name', 'assigned_to_id'
|
safe_attributes 'name', 'assigned_to_id'
|
||||||
|
|
||||||
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
||||||
|
|
||||||
alias :destroy_without_reassign :destroy
|
alias :destroy_without_reassign :destroy
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ class IssueStatus < ActiveRecord::Base
|
|||||||
validates_length_of :name, :maximum => 30
|
validates_length_of :name, :maximum => 30
|
||||||
validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
|
validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
|
||||||
|
|
||||||
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
||||||
|
|
||||||
def update_default
|
def update_default
|
||||||
IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
|
IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
|
||||||
|
@ -37,7 +37,7 @@ class Journal < ActiveRecord::Base
|
|||||||
:conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
|
:conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
|
||||||
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
|
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| {
|
scope :visible, lambda {|*args| {
|
||||||
:include => {:issue => :project},
|
:include => {:issue => :project},
|
||||||
:conditions => Issue.visible_condition(args.shift || User.current, *args)
|
:conditions => Issue.visible_condition(args.shift || User.current, *args)
|
||||||
}}
|
}}
|
||||||
|
@ -45,7 +45,7 @@ class Message < ActiveRecord::Base
|
|||||||
after_update :update_messages_board
|
after_update :update_messages_board
|
||||||
after_destroy :reset_board_counters
|
after_destroy :reset_board_counters
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| { :include => {:board => :project},
|
scope :visible, lambda {|*args| { :include => {:board => :project},
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
|
||||||
|
|
||||||
safe_attributes 'subject', 'content'
|
safe_attributes 'subject', 'content'
|
||||||
|
@ -34,7 +34,7 @@ class News < ActiveRecord::Base
|
|||||||
|
|
||||||
after_create :add_author_as_watcher
|
after_create :add_author_as_watcher
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| {
|
scope :visible, lambda {|*args| {
|
||||||
:include => :project,
|
:include => :project,
|
||||||
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
|
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
|
||||||
}}
|
}}
|
||||||
|
@ -24,9 +24,9 @@ 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
|
||||||
named_scope :active, :conditions => "#{Principal.table_name}.status = 1"
|
scope :active, :conditions => "#{Principal.table_name}.status = 1"
|
||||||
|
|
||||||
named_scope :like, lambda {|q|
|
scope :like, lambda {|q|
|
||||||
if q.blank?
|
if q.blank?
|
||||||
{}
|
{}
|
||||||
else
|
else
|
||||||
@ -44,7 +44,7 @@ class Principal < ActiveRecord::Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Principals that are members of a collection of projects
|
# Principals that are members of a collection of projects
|
||||||
named_scope :member_of, lambda {|projects|
|
scope :member_of, lambda {|projects|
|
||||||
projects = [projects] unless projects.is_a?(Array)
|
projects = [projects] unless projects.is_a?(Array)
|
||||||
if projects.empty?
|
if projects.empty?
|
||||||
{:conditions => "1=0"}
|
{:conditions => "1=0"}
|
||||||
@ -54,7 +54,7 @@ class Principal < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
# Principals that are not members of projects
|
# Principals that are not members of projects
|
||||||
named_scope :not_member_of, lambda {|projects|
|
scope :not_member_of, lambda {|projects|
|
||||||
projects = [projects] unless projects.is_a?(Array)
|
projects = [projects] unless projects.is_a?(Array)
|
||||||
if projects.empty?
|
if projects.empty?
|
||||||
{:conditions => "1=0"}
|
{:conditions => "1=0"}
|
||||||
|
@ -82,12 +82,12 @@ class Project < ActiveRecord::Base
|
|||||||
|
|
||||||
before_destroy :delete_all_members
|
before_destroy :delete_all_members
|
||||||
|
|
||||||
named_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| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
|
||||||
named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
|
scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
|
||||||
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
||||||
named_scope :all_public, { :conditions => { :is_public => true } }
|
scope :all_public, { :conditions => { :is_public => true } }
|
||||||
named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
|
scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
|
||||||
named_scope :allowed_to, lambda {|*args|
|
scope :allowed_to, lambda {|*args|
|
||||||
user = User.current
|
user = User.current
|
||||||
permission = nil
|
permission = nil
|
||||||
if args.first.is_a?(Symbol)
|
if args.first.is_a?(Symbol)
|
||||||
@ -98,7 +98,7 @@ class Project < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
{ :conditions => Project.allowed_to_condition(user, permission, *args) }
|
{ :conditions => Project.allowed_to_condition(user, permission, *args) }
|
||||||
}
|
}
|
||||||
named_scope :like, lambda {|arg|
|
scope :like, lambda {|arg|
|
||||||
if arg.blank?
|
if arg.blank?
|
||||||
{}
|
{}
|
||||||
else
|
else
|
||||||
|
@ -153,7 +153,7 @@ class Query < ActiveRecord::Base
|
|||||||
]
|
]
|
||||||
cattr_reader :available_columns
|
cattr_reader :available_columns
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args|
|
scope :visible, lambda {|*args|
|
||||||
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
|
||||||
|
@ -26,9 +26,9 @@ class Role < ActiveRecord::Base
|
|||||||
['own', :label_issues_visibility_own]
|
['own', :label_issues_visibility_own]
|
||||||
]
|
]
|
||||||
|
|
||||||
named_scope :sorted, {:order => 'builtin, position'}
|
scope :sorted, {:order => 'builtin, position'}
|
||||||
named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
|
scope :givable, { :conditions => "builtin = 0", :order => 'position' }
|
||||||
named_scope :builtin, lambda { |*args|
|
scope :builtin, lambda { |*args|
|
||||||
compare = 'not' if args.first == true
|
compare = 'not' if args.first == true
|
||||||
{ :conditions => "#{compare} builtin = 0" }
|
{ :conditions => "#{compare} builtin = 0" }
|
||||||
}
|
}
|
||||||
|
@ -42,19 +42,19 @@ class TimeEntry < ActiveRecord::Base
|
|||||||
before_validation :set_project_if_nil
|
before_validation :set_project_if_nil
|
||||||
validate :validate_time_entry
|
validate :validate_time_entry
|
||||||
|
|
||||||
named_scope :visible, lambda {|*args| {
|
scope :visible, lambda {|*args| {
|
||||||
:include => :project,
|
:include => :project,
|
||||||
:conditions => 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)
|
||||||
}}
|
}}
|
||||||
named_scope :on_issue, lambda {|issue| {
|
scope :on_issue, lambda {|issue| {
|
||||||
:include => :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}"
|
:conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
|
||||||
}}
|
}}
|
||||||
named_scope :on_project, lambda {|project, include_subprojects| {
|
scope :on_project, lambda {|project, include_subprojects| {
|
||||||
:include => :project,
|
:include => :project,
|
||||||
:conditions => project.project_condition(include_subprojects)
|
:conditions => project.project_condition(include_subprojects)
|
||||||
}}
|
}}
|
||||||
named_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]}
|
{:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
|
||||||
elsif from
|
elsif from
|
||||||
|
@ -32,7 +32,7 @@ class Tracker < ActiveRecord::Base
|
|||||||
validates_uniqueness_of :name
|
validates_uniqueness_of :name
|
||||||
validates_length_of :name, :maximum => 30
|
validates_length_of :name, :maximum => 30
|
||||||
|
|
||||||
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
||||||
|
|
||||||
def to_s; name end
|
def to_s; name end
|
||||||
|
|
||||||
|
@ -53,9 +53,9 @@ class User < Principal
|
|||||||
belongs_to :auth_source
|
belongs_to :auth_source
|
||||||
|
|
||||||
# Active non-anonymous users scope
|
# Active non-anonymous users scope
|
||||||
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
|
scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
|
||||||
named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
|
scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
|
||||||
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
|
||||||
|
|
||||||
acts_as_customizable
|
acts_as_customizable
|
||||||
|
|
||||||
@ -84,11 +84,11 @@ class User < Principal
|
|||||||
before_save :update_hashed_password
|
before_save :update_hashed_password
|
||||||
before_destroy :remove_references_before_destroy
|
before_destroy :remove_references_before_destroy
|
||||||
|
|
||||||
named_scope :in_group, lambda {|group|
|
scope :in_group, lambda {|group|
|
||||||
group_id = group.is_a?(Group) ? group.id : group.to_i
|
group_id = group.is_a?(Group) ? group.id : group.to_i
|
||||||
{ :conditions => ["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
{ :conditions => ["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
||||||
}
|
}
|
||||||
named_scope :not_in_group, lambda {|group|
|
scope :not_in_group, lambda {|group|
|
||||||
group_id = group.is_a?(Group) ? group.id : group.to_i
|
group_id = group.is_a?(Group) ? group.id : group.to_i
|
||||||
{ :conditions => ["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
{ :conditions => ["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,9 @@ class Version < ActiveRecord::Base
|
|||||||
validates_inclusion_of :status, :in => VERSION_STATUSES
|
validates_inclusion_of :status, :in => VERSION_STATUSES
|
||||||
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
|
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
|
||||||
|
|
||||||
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
|
||||||
named_scope :open, :conditions => {:status => 'open'}
|
scope :open, :conditions => {:status => 'open'}
|
||||||
named_scope :visible, lambda {|*args| { :include => :project,
|
scope :visible, lambda {|*args| { :include => :project,
|
||||||
:conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
|
:conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
|
||||||
|
|
||||||
safe_attributes 'name',
|
safe_attributes 'name',
|
||||||
|
@ -49,7 +49,7 @@ 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
|
||||||
named_scope :with_updated_on, {
|
scope :with_updated_on, {
|
||||||
:select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
|
:select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
|
||||||
: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"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user