@@ -16,6 +16,19 | |||||
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class Role < ActiveRecord::Base |
|
18 | class Role < ActiveRecord::Base | |
|
19 | # Custom coder for the permissions attribute that should be an | |||
|
20 | # array of symbols. Rails 3 uses Psych which can be *unbelievably* | |||
|
21 | # slow on some platforms (eg. mingw32). | |||
|
22 | class PermissionsAttributeCoder | |||
|
23 | def self.load(str) | |||
|
24 | str.to_s.scan(/:([a-z0-9_]+)/).flatten.map(&:to_sym) | |||
|
25 | end | |||
|
26 | ||||
|
27 | def self.dump(value) | |||
|
28 | YAML.dump(value) | |||
|
29 | end | |||
|
30 | end | |||
|
31 | ||||
19 | # Built-in roles |
|
32 | # Built-in roles | |
20 | BUILTIN_NON_MEMBER = 1 |
|
33 | BUILTIN_NON_MEMBER = 1 | |
21 | BUILTIN_ANONYMOUS = 2 |
|
34 | BUILTIN_ANONYMOUS = 2 | |
@@ -44,7 +57,7 class Role < ActiveRecord::Base | |||||
44 | has_many :members, :through => :member_roles |
|
57 | has_many :members, :through => :member_roles | |
45 | acts_as_list |
|
58 | acts_as_list | |
46 |
|
59 | |||
47 |
serialize :permissions, Arr |
|
60 | serialize :permissions, ::Role::PermissionsAttributeCoder | |
48 | attr_protected :builtin |
|
61 | attr_protected :builtin | |
49 |
|
62 | |||
50 | validates_presence_of :name |
|
63 | validates_presence_of :name | |
@@ -54,10 +67,6 class Role < ActiveRecord::Base | |||||
54 | :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first), |
|
67 | :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first), | |
55 | :if => lambda {|role| role.respond_to?(:issues_visibility)} |
|
68 | :if => lambda {|role| role.respond_to?(:issues_visibility)} | |
56 |
|
69 | |||
57 | def permissions |
|
|||
58 | read_attribute(:permissions) || [] |
|
|||
59 | end |
|
|||
60 |
|
||||
61 | def permissions=(perms) |
|
70 | def permissions=(perms) | |
62 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms |
|
71 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms | |
63 | write_attribute(:permissions, perms) |
|
72 | write_attribute(:permissions, perms) |
@@ -44,6 +44,11 class RoleTest < ActiveSupport::TestCase | |||||
44 | assert_equal 90, target.workflows.size |
|
44 | assert_equal 90, target.workflows.size | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
|
47 | def test_permissions_should_be_unserialized_with_its_coder | |||
|
48 | Role::PermissionsAttributeCoder.expects(:load).once | |||
|
49 | Role.find(1).permissions | |||
|
50 | end | |||
|
51 | ||||
47 | def test_add_permission |
|
52 | def test_add_permission | |
48 | role = Role.find(1) |
|
53 | role = Role.find(1) | |
49 | size = role.permissions.size |
|
54 | size = role.permissions.size |
General Comments 0
You need to be logged in to leave comments.
Login now