Commit 217ca5b5b62aba4a2b2831d7bcfe5ec10c744868

Authored by Daniel Beardsley
1 parent 34a4b8a1
Exists in master and in 1 other branch production

Github Login: allow setting to blank

Previously, creating a user without a github login would result in
user.github_login == '' instead of nil. This prevented creating more
than one user without a github login because of the unique index on the
field.
app/models/user.rb
@@ -52,6 +52,13 @@ class User @@ -52,6 +52,13 @@ class User
52 github_account? && Errbit::Config.github_access_scope.include?('repo') 52 github_account? && Errbit::Config.github_access_scope.include?('repo')
53 end 53 end
54 54
  55 + def github_login=(login)
  56 + if login.is_a?(String) && login.strip.empty?
  57 + login = nil
  58 + end
  59 + self[:github_login] = login
  60 + end
  61 +
55 protected 62 protected
56 63
57 def destroy_watchers 64 def destroy_watchers
spec/controllers/users_controller_spec.rb
@@ -79,6 +79,11 @@ describe UsersController do @@ -79,6 +79,11 @@ describe UsersController do
79 @user.reload.time_zone.should == "Warsaw" 79 @user.reload.time_zone.should == "Warsaw"
80 end 80 end
81 81
  82 + it "should be able to not set github_login option" do
  83 + put :update, :id => @user.to_param, :user => {:github_login => " "}
  84 + @user.reload.github_login.should == nil
  85 + end
  86 +
82 it "should be able to set github_login option" do 87 it "should be able to set github_login option" do
83 put :update, :id => @user.to_param, :user => {:github_login => "awesome_name"} 88 put :update, :id => @user.to_param, :user => {:github_login => "awesome_name"}
84 @user.reload.github_login.should == "awesome_name" 89 @user.reload.github_login.should == "awesome_name"
spec/models/user_spec.rb
@@ -29,6 +29,15 @@ describe User do @@ -29,6 +29,15 @@ describe User do
29 user2.should_not be_valid 29 user2.should_not be_valid
30 user2.errors[:github_login].should include("is already taken") 30 user2.errors[:github_login].should include("is already taken")
31 end 31 end
  32 +
  33 + it 'allows blank / null github_login' do
  34 + user1 = Fabricate(:user, :github_login => ' ')
  35 + user1.should be_valid
  36 +
  37 + user2 = Fabricate.build(:user, :github_login => ' ')
  38 + user2.save
  39 + user2.should be_valid
  40 + end
32 end 41 end
33 42
34 context 'Watchers' do 43 context 'Watchers' do