diff options
author | Toshi MARUYAMA <marutosijp2@yahoo.co.jp> | 2012-04-26 23:51:10 +0000 |
---|---|---|
committer | Toshi MARUYAMA <marutosijp2@yahoo.co.jp> | 2012-04-26 23:51:10 +0000 |
commit | d0d01d4e704b0d15dfd3ce2d5213ab8b5a6678fb (patch) | |
tree | 31a2f938e01ae8032d0e4c19d979f7df575d332e /app/models | |
parent | 71649ba2f137f3a48af031193af2b7f315519299 (diff) | |
download | redmine-d0d01d4e704b0d15dfd3ce2d5213ab8b5a6678fb.tar.gz redmine-d0d01d4e704b0d15dfd3ce2d5213ab8b5a6678fb.zip |
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
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/board.rb | 2 | ||||
-rw-r--r-- | app/models/changeset.rb | 3 | ||||
-rw-r--r-- | app/models/document.rb | 2 | ||||
-rw-r--r-- | app/models/enumeration.rb | 6 | ||||
-rw-r--r-- | app/models/issue.rb | 15 | ||||
-rw-r--r-- | app/models/issue_category.rb | 2 | ||||
-rw-r--r-- | app/models/issue_status.rb | 2 | ||||
-rw-r--r-- | app/models/journal.rb | 2 | ||||
-rw-r--r-- | app/models/message.rb | 2 | ||||
-rw-r--r-- | app/models/news.rb | 2 | ||||
-rw-r--r-- | app/models/principal.rb | 8 | ||||
-rw-r--r-- | app/models/project.rb | 14 | ||||
-rw-r--r-- | app/models/query.rb | 2 | ||||
-rw-r--r-- | app/models/role.rb | 6 | ||||
-rw-r--r-- | app/models/time_entry.rb | 8 | ||||
-rw-r--r-- | app/models/tracker.rb | 2 | ||||
-rw-r--r-- | app/models/user.rb | 10 | ||||
-rw-r--r-- | app/models/version.rb | 6 | ||||
-rw-r--r-- | app/models/wiki_page.rb | 2 |
19 files changed, 49 insertions, 47 deletions
diff --git a/app/models/board.rb b/app/models/board.rb index 40fd35673..624ef4e05 100644 --- a/app/models/board.rb +++ b/app/models/board.rb @@ -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' diff --git a/app/models/changeset.rb b/app/models/changeset.rb index ea3d61d87..89825c652 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -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 diff --git a/app/models/document.rb b/app/models/document.rb index d4a89ffd9..1f8e72c52 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -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' diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index d532c78be..f1d484d88 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -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 diff --git a/app/models/issue.rb b/app/models/issue.rb index a81dda16f..b8ea90efd 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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 diff --git a/app/models/issue_category.rb b/app/models/issue_category.rb index 162e7dc72..3ff0d0591 100644 --- a/app/models/issue_category.rb +++ b/app/models/issue_category.rb @@ -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 diff --git a/app/models/issue_status.rb b/app/models/issue_status.rb index 600d0cbf4..d69aee079 100644 --- a/app/models/issue_status.rb +++ b/app/models/issue_status.rb @@ -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? diff --git a/app/models/journal.rb b/app/models/journal.rb index fe8a432b6..73db1f62d 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -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) }} diff --git a/app/models/message.rb b/app/models/message.rb index 5d028870d..5294a655e 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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' diff --git a/app/models/news.rb b/app/models/news.rb index 1f7c6f4a7..bf1cc4f80 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -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) }} diff --git a/app/models/principal.rb b/app/models/principal.rb index de6539986..f2bec0e26 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -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"} diff --git a/app/models/project.rb b/app/models/project.rb index 6dfe95cc4..8006d5818 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/models/query.rb b/app/models/query.rb index 4e72ae5c7..e2e1326e2 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -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 diff --git a/app/models/role.rb b/app/models/role.rb index 06822fdba..6778f6f69 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -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" } } diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 6dbf78413..e6668281f 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -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 diff --git a/app/models/tracker.rb b/app/models/tracker.rb index 32eea2309..c4d4f028a 100644 --- a/app/models/tracker.rb +++ b/app/models/tracker.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index b11d91807..8b2b61b7a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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] } } diff --git a/app/models/version.rb b/app/models/version.rb index 24be5df02..fb97fa076 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -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', diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 6088e48ca..5ca0583c2 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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" } |