##// END OF EJS Templates
model: replace Rails2 "named_scope" to Rails3 "scope"...
Toshi MARUYAMA -
r9355:d0d01d4e704b
parent child
Show More
@@ -28,7 +28,7 class Board < ActiveRecord::Base
28 validates_length_of :name, :maximum => 30
28 validates_length_of :name, :maximum => 30
29 validates_length_of :description, :maximum => 255
29 validates_length_of :description, :maximum => 255
30
30
31 named_scope :visible, lambda {|*args| { :include => :project,
31 scope :visible, lambda {|*args| { :include => :project,
32 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
32 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
33
33
34 safe_attributes 'name', 'description', 'move_to'
34 safe_attributes 'name', 'description', 'move_to'
@@ -49,7 +49,8 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 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
52 scope :visible,
53 lambda {|*args| { :include => {:repository => :project},
53 :conditions => 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 after_create :scan_for_issues
56 after_create :scan_for_issues
@@ -30,7 +30,7 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 named_scope :visible, lambda {|*args| { :include => :project,
33 scope :visible, lambda {|*args| { :include => :project,
34 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
34 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
35
35
36 safe_attributes 'category_id', 'title', 'description'
36 safe_attributes 'category_id', 'title', 'description'
@@ -35,9 +35,9 class Enumeration < ActiveRecord::Base
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
36 validates_length_of :name, :maximum => 30
36 validates_length_of :name, :maximum => 30
37
37
38 named_scope :shared, :conditions => { :project_id => nil }
38 scope :shared, :conditions => { :project_id => nil }
39 named_scope :active, :conditions => { :active => true }
39 scope :active, :conditions => { :active => true }
40 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
40 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
41
41
42 def self.default
42 def self.default
43 # Creates a fake default scope so Enumeration.default will check
43 # Creates a fake default scope so Enumeration.default will check
@@ -60,18 +60,19 class Issue < ActiveRecord::Base
60 validates_numericality_of :estimated_hours, :allow_nil => true
60 validates_numericality_of :estimated_hours, :allow_nil => true
61 validate :validate_issue
61 validate :validate_issue
62
62
63 named_scope :visible, lambda {|*args| { :include => :project,
63 scope :visible,
64 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
64 lambda {|*args| { :include => :project,
65 :conditions => Issue.visible_condition(args.shift || User.current, *args) } }
65
66
66 named_scope :open, lambda {|*args|
67 scope :open, lambda {|*args|
67 is_closed = args.size > 0 ? !args.first : false
68 is_closed = args.size > 0 ? !args.first : false
68 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
69 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
69 }
70 }
70
71
71 named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
72 scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
72 named_scope :with_limit, lambda { |limit| { :limit => limit} }
73 scope :with_limit, lambda { |limit| { :limit => limit} }
73 named_scope :on_active_project, :include => [:status, :project, :tracker],
74 scope :on_active_project, :include => [:status, :project, :tracker],
74 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
75 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
75
76
76 before_create :default_assign
77 before_create :default_assign
77 before_save :close_duplicates, :update_done_ratio_from_issue_status
78 before_save :close_duplicates, :update_done_ratio_from_issue_status
@@ -27,7 +27,7 class IssueCategory < ActiveRecord::Base
27
27
28 safe_attributes 'name', 'assigned_to_id'
28 safe_attributes 'name', 'assigned_to_id'
29
29
30 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
30 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
31
31
32 alias :destroy_without_reassign :destroy
32 alias :destroy_without_reassign :destroy
33
33
@@ -28,7 +28,7 class IssueStatus < ActiveRecord::Base
28 validates_length_of :name, :maximum => 30
28 validates_length_of :name, :maximum => 30
29 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
29 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
30
30
31 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
31 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
32
32
33 def update_default
33 def update_default
34 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
34 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
@@ -37,7 +37,7 class Journal < ActiveRecord::Base
37 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
37 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
38 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
38 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
39
39
40 named_scope :visible, lambda {|*args| {
40 scope :visible, lambda {|*args| {
41 :include => {:issue => :project},
41 :include => {:issue => :project},
42 :conditions => Issue.visible_condition(args.shift || User.current, *args)
42 :conditions => Issue.visible_condition(args.shift || User.current, *args)
43 }}
43 }}
@@ -45,7 +45,7 class Message < ActiveRecord::Base
45 after_update :update_messages_board
45 after_update :update_messages_board
46 after_destroy :reset_board_counters
46 after_destroy :reset_board_counters
47
47
48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
48 scope :visible, lambda {|*args| { :include => {:board => :project},
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
50
50
51 safe_attributes 'subject', 'content'
51 safe_attributes 'subject', 'content'
@@ -34,7 +34,7 class News < ActiveRecord::Base
34
34
35 after_create :add_author_as_watcher
35 after_create :add_author_as_watcher
36
36
37 named_scope :visible, lambda {|*args| {
37 scope :visible, lambda {|*args| {
38 :include => :project,
38 :include => :project,
39 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
39 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args)
40 }}
40 }}
@@ -24,9 +24,9 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 named_scope :active, :conditions => "#{Principal.table_name}.status = 1"
27 scope :active, :conditions => "#{Principal.table_name}.status = 1"
28
28
29 named_scope :like, lambda {|q|
29 scope :like, lambda {|q|
30 if q.blank?
30 if q.blank?
31 {}
31 {}
32 else
32 else
@@ -44,7 +44,7 class Principal < ActiveRecord::Base
44 }
44 }
45
45
46 # Principals that are members of a collection of projects
46 # Principals that are members of a collection of projects
47 named_scope :member_of, lambda {|projects|
47 scope :member_of, lambda {|projects|
48 projects = [projects] unless projects.is_a?(Array)
48 projects = [projects] unless projects.is_a?(Array)
49 if projects.empty?
49 if projects.empty?
50 {:conditions => "1=0"}
50 {:conditions => "1=0"}
@@ -54,7 +54,7 class Principal < ActiveRecord::Base
54 end
54 end
55 }
55 }
56 # Principals that are not members of projects
56 # Principals that are not members of projects
57 named_scope :not_member_of, lambda {|projects|
57 scope :not_member_of, lambda {|projects|
58 projects = [projects] unless projects.is_a?(Array)
58 projects = [projects] unless projects.is_a?(Array)
59 if projects.empty?
59 if projects.empty?
60 {:conditions => "1=0"}
60 {:conditions => "1=0"}
@@ -82,12 +82,12 class Project < ActiveRecord::Base
82
82
83 before_destroy :delete_all_members
83 before_destroy :delete_all_members
84
84
85 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] } }
85 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] } }
86 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
86 scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
87 named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
87 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
88 named_scope :all_public, { :conditions => { :is_public => true } }
88 scope :all_public, { :conditions => { :is_public => true } }
89 named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
89 scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
90 named_scope :allowed_to, lambda {|*args|
90 scope :allowed_to, lambda {|*args|
91 user = User.current
91 user = User.current
92 permission = nil
92 permission = nil
93 if args.first.is_a?(Symbol)
93 if args.first.is_a?(Symbol)
@@ -98,7 +98,7 class Project < ActiveRecord::Base
98 end
98 end
99 { :conditions => Project.allowed_to_condition(user, permission, *args) }
99 { :conditions => Project.allowed_to_condition(user, permission, *args) }
100 }
100 }
101 named_scope :like, lambda {|arg|
101 scope :like, lambda {|arg|
102 if arg.blank?
102 if arg.blank?
103 {}
103 {}
104 else
104 else
@@ -153,7 +153,7 class Query < ActiveRecord::Base
153 ]
153 ]
154 cattr_reader :available_columns
154 cattr_reader :available_columns
155
155
156 named_scope :visible, lambda {|*args|
156 scope :visible, lambda {|*args|
157 user = args.shift || User.current
157 user = args.shift || User.current
158 base = Project.allowed_to_condition(user, :view_issues, *args)
158 base = Project.allowed_to_condition(user, :view_issues, *args)
159 user_id = user.logged? ? user.id : 0
159 user_id = user.logged? ? user.id : 0
@@ -26,9 +26,9 class Role < ActiveRecord::Base
26 ['own', :label_issues_visibility_own]
26 ['own', :label_issues_visibility_own]
27 ]
27 ]
28
28
29 named_scope :sorted, {:order => 'builtin, position'}
29 scope :sorted, {:order => 'builtin, position'}
30 named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
30 scope :givable, { :conditions => "builtin = 0", :order => 'position' }
31 named_scope :builtin, lambda { |*args|
31 scope :builtin, lambda { |*args|
32 compare = 'not' if args.first == true
32 compare = 'not' if args.first == true
33 { :conditions => "#{compare} builtin = 0" }
33 { :conditions => "#{compare} builtin = 0" }
34 }
34 }
@@ -42,19 +42,19 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 named_scope :visible, lambda {|*args| {
45 scope :visible, lambda {|*args| {
46 :include => :project,
46 :include => :project,
47 :conditions => 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)
48 }}
48 }}
49 named_scope :on_issue, lambda {|issue| {
49 scope :on_issue, lambda {|issue| {
50 :include => :issue,
50 :include => :issue,
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 :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
52 }}
52 }}
53 named_scope :on_project, lambda {|project, include_subprojects| {
53 scope :on_project, lambda {|project, include_subprojects| {
54 :include => :project,
54 :include => :project,
55 :conditions => project.project_condition(include_subprojects)
55 :conditions => project.project_condition(include_subprojects)
56 }}
56 }}
57 named_scope :spent_between, lambda {|from, to|
57 scope :spent_between, lambda {|from, to|
58 if from && to
58 if from && to
59 {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
59 {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
60 elsif from
60 elsif from
@@ -32,7 +32,7 class Tracker < ActiveRecord::Base
32 validates_uniqueness_of :name
32 validates_uniqueness_of :name
33 validates_length_of :name, :maximum => 30
33 validates_length_of :name, :maximum => 30
34
34
35 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
35 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
36
36
37 def to_s; name end
37 def to_s; name end
38
38
@@ -53,9 +53,9 class User < Principal
53 belongs_to :auth_source
53 belongs_to :auth_source
54
54
55 # Active non-anonymous users scope
55 # Active non-anonymous users scope
56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
56 scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
57 named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
57 scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
58 named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
58 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
59
59
60 acts_as_customizable
60 acts_as_customizable
61
61
@@ -84,11 +84,11 class User < Principal
84 before_save :update_hashed_password
84 before_save :update_hashed_password
85 before_destroy :remove_references_before_destroy
85 before_destroy :remove_references_before_destroy
86
86
87 named_scope :in_group, lambda {|group|
87 scope :in_group, lambda {|group|
88 group_id = group.is_a?(Group) ? group.id : group.to_i
88 group_id = group.is_a?(Group) ? group.id : group.to_i
89 { :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] }
89 { :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] }
90 }
90 }
91 named_scope :not_in_group, lambda {|group|
91 scope :not_in_group, lambda {|group|
92 group_id = group.is_a?(Group) ? group.id : group.to_i
92 group_id = group.is_a?(Group) ? group.id : group.to_i
93 { :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] }
93 { :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] }
94 }
94 }
@@ -34,9 +34,9 class Version < ActiveRecord::Base
34 validates_inclusion_of :status, :in => VERSION_STATUSES
34 validates_inclusion_of :status, :in => VERSION_STATUSES
35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36
36
37 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
37 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
38 named_scope :open, :conditions => {:status => 'open'}
38 scope :open, :conditions => {:status => 'open'}
39 named_scope :visible, lambda {|*args| { :include => :project,
39 scope :visible, lambda {|*args| { :include => :project,
40 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
40 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
41
41
42 safe_attributes 'name',
42 safe_attributes 'name',
@@ -49,7 +49,7 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 named_scope :with_updated_on, {
52 scope :with_updated_on, {
53 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
53 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
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 }
General Comments 0
You need to be logged in to leave comments. Login now