##// END OF EJS Templates
Use lambda form in model scopes (#12499)...
Jean-Baptiste Barth -
r10722:4896d5c7e83b
parent child
Show More
@@ -30,7 +30,7 class CustomField < ActiveRecord::Base
30 validate :validate_custom_field
30 validate :validate_custom_field
31 before_validation :set_searchable
31 before_validation :set_searchable
32
32
33 scope :sorted, order("#{table_name}.position ASC")
33 scope :sorted, lambda { order("#{table_name}.position ASC") }
34
34
35 CUSTOM_FIELDS_TABS = [
35 CUSTOM_FIELDS_TABS = [
36 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
36 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
@@ -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 scope :shared, where(:project_id => nil)
38 scope :shared, lambda { where(:project_id => nil) }
39 scope :sorted, order("#{table_name}.position ASC")
39 scope :sorted, lambda { order("#{table_name}.position ASC") }
40 scope :active, where(:active => true)
40 scope :active, lambda { where(:active => true) }
41 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
41 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
42
42
43 def self.default
43 def self.default
@@ -29,7 +29,7 class Group < Principal
29
29
30 before_destroy :remove_references_before_destroy
30 before_destroy :remove_references_before_destroy
31
31
32 scope :sorted, order("#{table_name}.lastname ASC")
32 scope :sorted, lambda { order("#{table_name}.lastname ASC") }
33
33
34 safe_attributes 'name',
34 safe_attributes 'name',
35 'user_ids',
35 'user_ids',
@@ -79,9 +79,9 class Issue < ActiveRecord::Base
79 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
79 {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status}
80 }
80 }
81
81
82 scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC"
82 scope :recently_updated, lambda { { :order => "#{Issue.table_name}.updated_on DESC" } }
83 scope :on_active_project, :include => [:status, :project, :tracker],
83 scope :on_active_project, lambda { { :include => [:status, :project, :tracker],
84 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
84 :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] } }
85
85
86 before_create :default_assign
86 before_create :default_assign
87 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
87 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change
@@ -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 scope :sorted, order("#{table_name}.position ASC")
31 scope :sorted, lambda { order("#{table_name}.position ASC") }
32 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
32 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
33
33
34 def update_default
34 def update_default
@@ -24,7 +24,7 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 scope :active, :conditions => "#{Principal.table_name}.status = 1"
27 scope :active, lambda { { :conditions => "#{Principal.table_name}.status = 1" } }
28
28
29 scope :like, lambda {|q|
29 scope :like, lambda {|q|
30 q = q.to_s
30 q = q.to_s
@@ -85,9 +85,9 class Project < ActiveRecord::Base
85 before_destroy :delete_all_members
85 before_destroy :delete_all_members
86
86
87 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] } }
87 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] } }
88 scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
88 scope :active, lambda { { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}" } }
89 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
89 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
90 scope :all_public, { :conditions => { :is_public => true } }
90 scope :all_public, lambda { { :conditions => { :is_public => true } } }
91 scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
91 scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
92 scope :allowed_to, lambda {|*args|
92 scope :allowed_to, lambda {|*args|
93 user = User.current
93 user = User.current
@@ -39,8 +39,8 class Role < ActiveRecord::Base
39 ['own', :label_issues_visibility_own]
39 ['own', :label_issues_visibility_own]
40 ]
40 ]
41
41
42 scope :sorted, order("#{table_name}.builtin ASC, #{table_name}.position ASC")
42 scope :sorted, lambda { order("#{table_name}.builtin ASC, #{table_name}.position ASC") }
43 scope :givable, order("#{table_name}.position ASC").where(:builtin => 0)
43 scope :givable, lambda { order("#{table_name}.position ASC").where(:builtin => 0) }
44 scope :builtin, lambda { |*args|
44 scope :builtin, lambda { |*args|
45 compare = (args.first == true ? 'not' : '')
45 compare = (args.first == true ? 'not' : '')
46 where("#{compare} builtin = 0")
46 where("#{compare} builtin = 0")
@@ -41,7 +41,7 class Tracker < ActiveRecord::Base
41 validates_uniqueness_of :name
41 validates_uniqueness_of :name
42 validates_length_of :name, :maximum => 30
42 validates_length_of :name, :maximum => 30
43
43
44 scope :sorted, order("#{table_name}.position ASC")
44 scope :sorted, lambda { order("#{table_name}.position ASC") }
45 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
45 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
46
46
47 def to_s; name end
47 def to_s; name end
@@ -82,7 +82,7 class User < Principal
82 has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
82 has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
83 belongs_to :auth_source
83 belongs_to :auth_source
84
84
85 scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
85 scope :logged, lambda { { :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" } }
86 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
86 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
87
87
88 acts_as_customizable
88 acts_as_customizable
@@ -36,7 +36,7 class Version < ActiveRecord::Base
36 validate :validate_version
36 validate :validate_version
37
37
38 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
38 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
39 scope :open, where(:status => 'open')
39 scope :open, lambda { where(:status => 'open') }
40 scope :visible, lambda {|*args|
40 scope :visible, lambda {|*args|
41 includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues))
41 includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues))
42 }
42 }
@@ -49,10 +49,10 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 scope :with_updated_on, {
52 scope :with_updated_on, lambda { {
53 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version",
53 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version",
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 } }
56
56
57 # Wiki pages that are protected by default
57 # Wiki pages that are protected by default
58 DEFAULT_PROTECTED_PAGES = %w(sidebar)
58 DEFAULT_PROTECTED_PAGES = %w(sidebar)
General Comments 0
You need to be logged in to leave comments. Login now