find(:all, :conditions => {:builtin => 0}, :order => 'position')
end
- # Return the builtin 'non member' role
+ # Return the builtin 'non member' role. If the role doesn't exist,
+ # it will be created on the fly.
def self.non_member
- find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) || raise('Missing non-member builtin role.')
+ non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER})
+ if non_member_role.nil?
+ non_member_role = create(:name => 'Non member', :position => 0) do |role|
+ role.builtin = BUILTIN_NON_MEMBER
+ end
+ raise 'Unable to create the non-member role.' if non_member_role.new_record?
+ end
+ non_member_role
end
- # Return the builtin 'anonymous' role
+ # Return the builtin 'anonymous' role. If the role doesn't exist,
+ # it will be created on the fly.
def self.anonymous
- find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) || raise('Missing anonymous builtin role.')
+ anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS})
+ if anonymous_role.nil?
+ anonymous_role = create(:name => 'Anonymous', :position => 0) do |role|
+ role.builtin = BUILTIN_ANONYMOUS
+ end
+ raise 'Unable to create the anonymous role.' if anonymous_role.new_record?
+ end
+ anonymous_role
end
assert_equal size - 2, role.permissions.size
end
+ context "#anonymous" do
+ should "return the anonymous role" do
+ role = Role.anonymous
+ assert role.builtin?
+ assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
+ end
+
+ context "with a missing anonymous role" do
+ setup do
+ Role.delete_all("builtin = #{Role::BUILTIN_ANONYMOUS}")
+ end
+
+ should "create a new anonymous role" do
+ assert_difference('Role.count') do
+ Role.anonymous
+ end
+ end
+
+ should "return the anonymous role" do
+ role = Role.anonymous
+ assert role.builtin?
+ assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
+ end
+ end
+ end
+
+ context "#non_member" do
+ should "return the non-member role" do
+ role = Role.non_member
+ assert role.builtin?
+ assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
+ end
+
+ context "with a missing non-member role" do
+ setup do
+ Role.delete_all("builtin = #{Role::BUILTIN_NON_MEMBER}")
+ end
+
+ should "create a new non-member role" do
+ assert_difference('Role.count') do
+ Role.non_member
+ end
+ end
+
+ should "return the non-member role" do
+ role = Role.non_member
+ assert role.builtin?
+ assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
+ end
+ end
+ end
end