##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9532:aa18cd54c7ec
parent child
Show More
@@ -26,11 +26,11 class Role < ActiveRecord::Base
26 ['own', :label_issues_visibility_own]
26 ['own', :label_issues_visibility_own]
27 ]
27 ]
28
28
29 scope :sorted, {:order => 'builtin, position'}
29 scope :sorted, order("#{table_name}.builtin ASC, #{table_name}.position ASC")
30 scope :givable, { :conditions => "builtin = 0", :order => 'position' }
30 scope :givable, order("#{table_name}.position ASC").where(:builtin => 0)
31 scope :builtin, lambda { |*args|
31 scope :builtin, lambda { |*args|
32 compare = 'not' if args.first == true
32 compare = (args.first == true ? 'not' : '')
33 { :conditions => "#{compare} builtin = 0" }
33 where("#{compare} builtin = 0")
34 }
34 }
35
35
36 before_destroy :check_deletable
36 before_destroy :check_deletable
@@ -87,7 +87,15 class Role < ActiveRecord::Base
87 end
87 end
88
88
89 def <=>(role)
89 def <=>(role)
90 role ? position <=> role.position : -1
90 if role
91 if builtin == role.builtin
92 position <=> role.position
93 else
94 builtin <=> role.builtin
95 end
96 else
97 -1
98 end
91 end
99 end
92
100
93 def to_s
101 def to_s
@@ -134,7 +142,7 class Role < ActiveRecord::Base
134
142
135 # Find all the roles that can be given to a project member
143 # Find all the roles that can be given to a project member
136 def self.find_all_givable
144 def self.find_all_givable
137 find(:all, :conditions => {:builtin => 0}, :order => 'position')
145 Role.givable.all
138 end
146 end
139
147
140 # Return the builtin 'non member' role. If the role doesn't exist,
148 # Return the builtin 'non member' role. If the role doesn't exist,
@@ -165,7 +173,7 private
165 end
173 end
166
174
167 def self.find_or_create_system_role(builtin, name)
175 def self.find_or_create_system_role(builtin, name)
168 role = first(:conditions => {:builtin => builtin})
176 role = where(:builtin => builtin).first
169 if role.nil?
177 if role.nil?
170 role = create(:name => name, :position => 0) do |r|
178 role = create(:name => name, :position => 0) do |r|
171 r.builtin = builtin
179 r.builtin = builtin
@@ -25,8 +25,8 class Workflow < ActiveRecord::Base
25 # Returns workflow transitions count by tracker and role
25 # Returns workflow transitions count by tracker and role
26 def self.count_by_tracker_and_role
26 def self.count_by_tracker_and_role
27 counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id")
27 counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id")
28 roles = Role.find(:all, :order => 'builtin, position')
28 roles = Role.sorted.all
29 trackers = Tracker.find(:all, :order => 'position')
29 trackers = Tracker.sorted.all
30
30
31 result = []
31 result = []
32 trackers.each do |tracker|
32 trackers.each do |tracker|
@@ -20,6 +20,19 require File.expand_path('../../test_helper', __FILE__)
20 class RoleTest < ActiveSupport::TestCase
20 class RoleTest < ActiveSupport::TestCase
21 fixtures :roles, :workflows
21 fixtures :roles, :workflows
22
22
23 def test_sorted_scope
24 assert_equal Role.all.sort, Role.sorted.all
25 end
26
27 def test_givable_scope
28 assert_equal Role.all.reject(&:builtin?).sort, Role.givable.all
29 end
30
31 def test_builtin_scope
32 assert_equal Role.all.select(&:builtin?).sort, Role.builtin(true).all.sort
33 assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).all.sort
34 end
35
23 def test_copy_workflows
36 def test_copy_workflows
24 source = Role.find(1)
37 source = Role.find(1)
25 assert_equal 90, source.workflows.size
38 assert_equal 90, source.workflows.size
@@ -57,6 +70,10 class RoleTest < ActiveSupport::TestCase
57 assert_equal 'Non membre', Role.non_member.name
70 assert_equal 'Non membre', Role.non_member.name
58 end
71 end
59
72
73 def test_find_all_givable
74 assert_equal Role.all.reject(&:builtin?).sort, Role.find_all_givable
75 end
76
60 context "#anonymous" do
77 context "#anonymous" do
61 should "return the anonymous role" do
78 should "return the anonymous role" do
62 role = Role.anonymous
79 role = Role.anonymous
General Comments 0
You need to be logged in to leave comments. Login now