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:
Toshi MARUYAMA 2012-04-26 23:51:10 +00:00
parent 71649ba2f1
commit d0d01d4e70
19 changed files with 49 additions and 47 deletions

View File

@ -28,7 +28,7 @@ class Board < ActiveRecord::Base
validates_length_of :name, :maximum => 30
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) } }
safe_attributes 'name', 'description', 'move_to'

View File

@ -49,7 +49,8 @@ class Changeset < ActiveRecord::Base
validates_uniqueness_of :revision, :scope => :repository_id
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) } }
after_create :scan_for_issues

View File

@ -30,7 +30,7 @@ class Document < ActiveRecord::Base
validates_presence_of :project, :title, :category
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) } }
safe_attributes 'category_id', 'title', 'description'

View File

@ -35,9 +35,9 @@ class Enumeration < ActiveRecord::Base
validates_uniqueness_of :name, :scope => [:type, :project_id]
validates_length_of :name, :maximum => 30
named_scope :shared, :conditions => { :project_id => nil }
named_scope :active, :conditions => { :active => true }
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
scope :shared, :conditions => { :project_id => nil }
scope :active, :conditions => { :active => true }
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
def self.default
# Creates a fake default scope so Enumeration.default will check

View File

@ -60,18 +60,19 @@ class Issue < ActiveRecord::Base
validates_numericality_of :estimated_hours, :allow_nil => true
validate :validate_issue
named_scope :visible, lambda {|*args| { :include => :project,
:conditions => Issue.visible_condition(args.shift || User.current, *args) } }
scope :visible,
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
{:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
}
named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
named_scope :with_limit, lambda { |limit| { :limit => limit} }
named_scope :on_active_project, :include => [:status, :project, :tracker],
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
scope :with_limit, lambda { |limit| { :limit => limit} }
scope :on_active_project, :include => [:status, :project, :tracker],
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
before_create :default_assign
before_save :close_duplicates, :update_done_ratio_from_issue_status

View File

@ -27,7 +27,7 @@ class IssueCategory < ActiveRecord::Base
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

View File

@ -28,7 +28,7 @@ class IssueStatus < ActiveRecord::Base
validates_length_of :name, :maximum => 30
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
IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?

View File

@ -37,7 +37,7 @@ class Journal < ActiveRecord::Base
:conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
named_scope :visible, lambda {|*args| {
scope :visible, lambda {|*args| {
:include => {:issue => :project},
:conditions => Issue.visible_condition(args.shift || User.current, *args)
}}

View File

@ -45,7 +45,7 @@ class Message < ActiveRecord::Base
after_update :update_messages_board
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) } }
safe_attributes 'subject', 'content'

View File

@ -34,7 +34,7 @@ class News < ActiveRecord::Base
after_create :add_author_as_watcher
named_scope :visible, lambda {|*args| {
scope :visible, lambda {|*args| {
:include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
}}

View File

@ -24,9 +24,9 @@ class Principal < ActiveRecord::Base
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
# 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?
{}
else
@ -44,7 +44,7 @@ class Principal < ActiveRecord::Base
}
# 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)
if projects.empty?
{:conditions => "1=0"}
@ -54,7 +54,7 @@ class Principal < ActiveRecord::Base
end
}
# 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)
if projects.empty?
{:conditions => "1=0"}

View File

@ -82,12 +82,12 @@ class Project < ActiveRecord::Base
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] } }
named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
named_scope :all_public, { :conditions => { :is_public => true } }
named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
named_scope :allowed_to, lambda {|*args|
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, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
scope :all_public, { :conditions => { :is_public => true } }
scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
scope :allowed_to, lambda {|*args|
user = User.current
permission = nil
if args.first.is_a?(Symbol)
@ -98,7 +98,7 @@ class Project < ActiveRecord::Base
end
{ :conditions => Project.allowed_to_condition(user, permission, *args) }
}
named_scope :like, lambda {|arg|
scope :like, lambda {|arg|
if arg.blank?
{}
else

View File

@ -153,7 +153,7 @@ class Query < ActiveRecord::Base
]
cattr_reader :available_columns
named_scope :visible, lambda {|*args|
scope :visible, lambda {|*args|
user = args.shift || User.current
base = Project.allowed_to_condition(user, :view_issues, *args)
user_id = user.logged? ? user.id : 0

View File

@ -26,9 +26,9 @@ class Role < ActiveRecord::Base
['own', :label_issues_visibility_own]
]
named_scope :sorted, {:order => 'builtin, position'}
named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
named_scope :builtin, lambda { |*args|
scope :sorted, {:order => 'builtin, position'}
scope :givable, { :conditions => "builtin = 0", :order => 'position' }
scope :builtin, lambda { |*args|
compare = 'not' if args.first == true
{ :conditions => "#{compare} builtin = 0" }
}

View File

@ -42,19 +42,19 @@ class TimeEntry < ActiveRecord::Base
before_validation :set_project_if_nil
validate :validate_time_entry
named_scope :visible, lambda {|*args| {
scope :visible, lambda {|*args| {
:include => :project,
: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,
: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,
:conditions => project.project_condition(include_subprojects)
}}
named_scope :spent_between, lambda {|from, to|
scope :spent_between, lambda {|from, to|
if from && to
{:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
elsif from

View File

@ -32,7 +32,7 @@ class Tracker < ActiveRecord::Base
validates_uniqueness_of :name
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

View File

@ -53,9 +53,9 @@ class User < Principal
belongs_to :auth_source
# Active non-anonymous users scope
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
acts_as_customizable
@ -84,11 +84,11 @@ class User < Principal
before_save :update_hashed_password
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
{ :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
{ :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] }
}

View File

@ -34,9 +34,9 @@ class Version < ActiveRecord::Base
validates_inclusion_of :status, :in => VERSION_STATUSES
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
named_scope :open, :conditions => {:status => 'open'}
named_scope :visible, lambda {|*args| { :include => :project,
scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
scope :open, :conditions => {:status => 'open'}
scope :visible, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
safe_attributes 'name',

View File

@ -49,7 +49,7 @@ class WikiPage < ActiveRecord::Base
before_save :handle_redirects
# 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",
:joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
}