##// END OF EJS Templates
Change Role#anonymous and #non_member so they generate the record as needed....
Eric Davis -
r3249:155083ec9722
parent child
Show More
@@ -120,14 +120,30 class Role < ActiveRecord::Base
120 find(:all, :conditions => {:builtin => 0}, :order => 'position')
120 find(:all, :conditions => {:builtin => 0}, :order => 'position')
121 end
121 end
122
122
123 # Return the builtin 'non member' role
123 # Return the builtin 'non member' role. If the role doesn't exist,
124 # it will be created on the fly.
124 def self.non_member
125 def self.non_member
125 find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) || raise('Missing non-member builtin role.')
126 non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER})
127 if non_member_role.nil?
128 non_member_role = create(:name => 'Non member', :position => 0) do |role|
129 role.builtin = BUILTIN_NON_MEMBER
130 end
131 raise 'Unable to create the non-member role.' if non_member_role.new_record?
132 end
133 non_member_role
126 end
134 end
127
135
128 # Return the builtin 'anonymous' role
136 # Return the builtin 'anonymous' role. If the role doesn't exist,
137 # it will be created on the fly.
129 def self.anonymous
138 def self.anonymous
130 find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) || raise('Missing anonymous builtin role.')
139 anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS})
140 if anonymous_role.nil?
141 anonymous_role = create(:name => 'Anonymous', :position => 0) do |role|
142 role.builtin = BUILTIN_ANONYMOUS
143 end
144 raise 'Unable to create the anonymous role.' if anonymous_role.new_record?
145 end
146 anonymous_role
131 end
147 end
132
148
133
149
@@ -50,4 +50,55 class RoleTest < ActiveSupport::TestCase
50 assert_equal size - 2, role.permissions.size
50 assert_equal size - 2, role.permissions.size
51 end
51 end
52
52
53 context "#anonymous" do
54 should "return the anonymous role" do
55 role = Role.anonymous
56 assert role.builtin?
57 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
58 end
59
60 context "with a missing anonymous role" do
61 setup do
62 Role.delete_all("builtin = #{Role::BUILTIN_ANONYMOUS}")
63 end
64
65 should "create a new anonymous role" do
66 assert_difference('Role.count') do
67 Role.anonymous
68 end
69 end
70
71 should "return the anonymous role" do
72 role = Role.anonymous
73 assert role.builtin?
74 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
75 end
76 end
77 end
78
79 context "#non_member" do
80 should "return the non-member role" do
81 role = Role.non_member
82 assert role.builtin?
83 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
84 end
85
86 context "with a missing non-member role" do
87 setup do
88 Role.delete_all("builtin = #{Role::BUILTIN_NON_MEMBER}")
89 end
90
91 should "create a new non-member role" do
92 assert_difference('Role.count') do
93 Role.non_member
94 end
95 end
96
97 should "return the non-member role" do
98 role = Role.non_member
99 assert role.builtin?
100 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
101 end
102 end
103 end
53 end
104 end
General Comments 0
You need to be logged in to leave comments. Login now