summaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-04-04 17:26:05 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-04-04 17:26:05 +0000
commite227b92972522ad24818d2e69877dbdb84f40884 (patch)
treeb967430882d75dfee1c429e50f1ae396c4948190 /app/models
parenta9a082f05cc3bec335bb6886a5e6f79d1c5f6f95 (diff)
downloadredmine-e227b92972522ad24818d2e69877dbdb84f40884.tar.gz
redmine-e227b92972522ad24818d2e69877dbdb84f40884.zip
Various code cleaning, mainly on User, Permission and IssueStatus models.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@414 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r--app/models/issue_status.rb17
-rw-r--r--app/models/permission.rb2
-rw-r--r--app/models/user.rb10
3 files changed, 15 insertions, 14 deletions
diff --git a/app/models/issue_status.rb b/app/models/issue_status.rb
index fd6194c9a..337aa5894 100644
--- a/app/models/issue_status.rb
+++ b/app/models/issue_status.rb
@@ -36,12 +36,19 @@ class IssueStatus < ActiveRecord::Base
end
# Returns an array of all statuses the given role can switch to
+ # Uses association cache when called more than one time
def new_statuses_allowed_to(role, tracker)
- statuses = []
- for workflow in self.workflows
- statuses << workflow.new_status if workflow.role_id == role.id and workflow.tracker_id == tracker.id
- end unless role.nil? or tracker.nil?
- statuses
+ new_statuses = [self] + workflows.select {|w| w.role_id == role.id && w.tracker_id == tracker.id}.collect{|w| w.new_status}
+ new_statuses.sort{|x, y| x.position <=> y.position }
+ end
+
+ # Same thing as above but uses a database query
+ # More efficient than the previous method if called just once
+ def find_new_statuses_allowed_to(role, tracker)
+ new_statuses = [self] + workflows.find(:all,
+ :include => :new_status,
+ :conditions => ["role_id=? and tracker_id=?", role.id, tracker.id]).collect{ |w| w.new_status }
+ new_statuses.sort{|x, y| x.position <=> y.position }
end
private
diff --git a/app/models/permission.rb b/app/models/permission.rb
index 23f8a5e91..609d5d561 100644
--- a/app/models/permission.rb
+++ b/app/models/permission.rb
@@ -57,7 +57,7 @@ class Permission < ActiveRecord::Base
find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
perms
end
- allowed_to_public(action) or (@@cached_perms_for_roles[action] and @@cached_perms_for_roles[action].include? role)
+ allowed_to_public(action) or (role && @@cached_perms_for_roles[action] && @@cached_perms_for_roles[action].include?(role.id))
end
def self.allowed_to_role_expired
diff --git a/app/models/user.rb b/app/models/user.rb
index 08220beaa..15f857542 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -124,14 +124,8 @@ class User < ActiveRecord::Base
User.hash_password(clear_password) == self.hashed_password
end
- def role_for_project(project_id)
- @role_for_projects ||=
- begin
- roles = {}
- self.memberships.each { |m| roles.store m.project_id, m.role_id }
- roles
- end
- @role_for_projects[project_id]
+ def role_for_project(project)
+ memberships.detect {|m| m.project_id == project.id}
end
def pref