diff app/models/role.rb @ 1339:c03a6c3c4db9 luisf

Merge
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 20 Jun 2013 14:34:42 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
line wrap: on
line diff
--- a/app/models/role.rb	Wed Nov 21 18:16:32 2012 +0000
+++ b/app/models/role.rb	Thu Jun 20 14:34:42 2013 +0100
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -16,6 +16,19 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 class Role < ActiveRecord::Base
+  # Custom coder for the permissions attribute that should be an
+  # array of symbols. Rails 3 uses Psych which can be *unbelievably*
+  # slow on some platforms (eg. mingw32).
+  class PermissionsAttributeCoder
+    def self.load(str)
+      str.to_s.scan(/:([a-z0-9_]+)/).flatten.map(&:to_sym)
+    end
+
+    def self.dump(value)
+      YAML.dump(value)
+    end
+  end
+
   # Built-in roles
   BUILTIN_NON_MEMBER = 1
   BUILTIN_ANONYMOUS  = 2
@@ -26,16 +39,17 @@
     ['own', :label_issues_visibility_own]
   ]
 
-  named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
-  named_scope :builtin, lambda { |*args|
-    compare = 'not' if args.first == true
-    { :conditions => "#{compare} builtin = 0" }
+  scope :sorted, order("#{table_name}.builtin ASC, #{table_name}.position ASC")
+  scope :givable, order("#{table_name}.position ASC").where(:builtin => 0)
+  scope :builtin, lambda { |*args|
+    compare = (args.first == true ? 'not' : '')
+    where("#{compare} builtin = 0")
   }
 
   before_destroy :check_deletable
-  has_many :workflows, :dependent => :delete_all do
+  has_many :workflow_rules, :dependent => :delete_all do
     def copy(source_role)
-      Workflow.copy(nil, source_role, nil, proxy_owner)
+      WorkflowRule.copy(nil, source_role, nil, proxy_association.owner)
     end
   end
 
@@ -43,7 +57,7 @@
   has_many :members, :through => :member_roles
   acts_as_list
 
-  serialize :permissions, Array
+  serialize :permissions, ::Role::PermissionsAttributeCoder
   attr_protected :builtin
 
   validates_presence_of :name
@@ -53,8 +67,13 @@
     :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
     :if => lambda {|role| role.respond_to?(:issues_visibility)}
 
-  def permissions
-    read_attribute(:permissions) || []
+  # Copies attributes from another role, arg can be an id or a Role
+  def copy_from(arg, options={})
+    return unless arg.present?
+    role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
+    self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
+    self.permissions = role.permissions.dup
+    self
   end
 
   def permissions=(perms)
@@ -86,7 +105,15 @@
   end
 
   def <=>(role)
-    role ? position <=> role.position : -1
+    if role
+      if builtin == role.builtin
+        position <=> role.position
+      else
+        builtin <=> role.builtin
+      end
+    else
+      -1
+    end
   end
 
   def to_s
@@ -106,6 +133,11 @@
     self.builtin != 0
   end
 
+  # Return true if the role is the anonymous role
+  def anonymous?
+    builtin == 2
+  end
+  
   # Return true if the role is a project member role
   def member?
     !self.builtin?
@@ -133,7 +165,7 @@
 
   # Find all the roles that can be given to a project member
   def self.find_all_givable
-    find(:all, :conditions => {:builtin => 0}, :order => 'position')
+    Role.givable.all
   end
 
   # Return the builtin 'non member' role.  If the role doesn't exist,
@@ -164,7 +196,7 @@
   end
 
   def self.find_or_create_system_role(builtin, name)
-    role = first(:conditions => {:builtin => builtin})
+    role = where(:builtin => builtin).first
     if role.nil?
       role = create(:name => name, :position => 0) do |r|
         r.builtin = builtin