From 155083ec9722e2c01ec3323d7a421fa059a80ba9 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Wed, 3 Feb 2010 17:47:47 +0000 Subject: [PATCH] Change Role#anonymous and #non_member so they generate the record as needed. While creating tests, it was a common occurrence to lost the builtin roles because they are only created in the migrations. This makes them behave like User#anonymous. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3363 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/role.rb | 24 ++++++++++++++++---- test/unit/role_test.rb | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/app/models/role.rb b/app/models/role.rb index 22ce2a650..d1bebdb6d 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -120,14 +120,30 @@ class Role < ActiveRecord::Base 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 diff --git a/test/unit/role_test.rb b/test/unit/role_test.rb index 1e76dd887..df751eb49 100644 --- a/test/unit/role_test.rb +++ b/test/unit/role_test.rb @@ -50,4 +50,55 @@ class RoleTest < ActiveSupport::TestCase 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 -- 2.39.5