Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug in activity_user where users with only a first name or surn…

…ame (but not both) would not be found.
  • Loading branch information...
commit f64d8cb575c834f828503f76d951ee3f130b9101 1 parent 85086f8
@steveyken steveyken authored
View
26 app/controllers/home_controller.rb
@@ -128,20 +128,19 @@ def activity_event
#----------------------------------------------------------------------------
def activity_user
user = @current_user.pref[:activity_user]
- if user && user != "all users"
- user = if user =~ /\s/ # first_name middle_name last_name any_name
- name_query = if user.include?(" ")
- user.name_permutations.map{ |first, last|
- "(upper(first_name) LIKE upper('%#{first}%') AND upper(last_name) LIKE upper('%#{last}%'))"
- }.join(" OR ")
- else
- "upper(first_name) LIKE upper('%#{user}%') OR upper(last_name) LIKE upper('%#{user}%')"
+ if user && user != "all_users"
+ user = if user =~ /@/ # email
+ User.where(:email => user).first
+ else # first_name middle_name last_name any_name
+ name_query = if user.include?(" ")
+ user.name_permutations.map{ |first, last|
+ "(upper(first_name) LIKE upper('%#{first}%') AND upper(last_name) LIKE upper('%#{last}%'))"
+ }.join(" OR ")
+ else
+ "upper(first_name) LIKE upper('%#{user}%') OR upper(last_name) LIKE upper('%#{user}%')"
+ end
+ User.where(name_query).first
end
-
- User.where(name_query).first
- elsif user =~ /@/ # email
- User.where(:email => user).first
- end
end
user.is_a?(User) ? user.id : nil
end
@@ -158,4 +157,3 @@ def activity_duration
end
end
-
View
38 spec/controllers/home_controller_spec.rb
@@ -94,6 +94,42 @@
session[:hello].should == true
end
end
+
+ describe "activity_user" do
+
+ before(:each) do
+ @user = mock(User, :id => 1, :is_a? => true)
+ @cur_user = mock(User)
+ end
+
+ it "should find a user by email" do
+ @cur_user.stub!(:pref).and_return(:activity_user => 'billy@example.com')
+ controller.instance_variable_set(:@current_user, @cur_user)
+ User.should_receive(:where).with(:email => 'billy@example.com').and_return([@user])
+ controller.send(:activity_user).should == 1
+ end
+
+ it "should find a user by first name or last name" do
+ @cur_user.stub!(:pref).and_return(:activity_user => 'Billy')
+ controller.instance_variable_set(:@current_user, @cur_user)
+ User.should_receive(:where).with("upper(first_name) LIKE upper('%Billy%') OR upper(last_name) LIKE upper('%Billy%')").and_return([@user])
+ controller.send(:activity_user).should == 1
+ end
+
+ it "should find a user by first name and last name" do
+ @cur_user.stub!(:pref).and_return(:activity_user => 'Billy Elliot')
+ controller.instance_variable_set(:@current_user, @cur_user)
+ User.should_receive(:where).with("(upper(first_name) LIKE upper('%Billy%') AND upper(last_name) LIKE upper('%Elliot%')) OR (upper(first_name) LIKE upper('%Elliot%') AND upper(last_name) LIKE upper('%Billy%'))").and_return([@user])
+ controller.send(:activity_user).should == 1
+ end
+
+ it "should return nil when 'all_users' is specified" do
+ @cur_user.stub!(:pref).and_return(:activity_user => 'all_users')
+ controller.instance_variable_set(:@current_user, @cur_user)
+ User.should_not_receive(:where)
+ controller.send(:activity_user).should == nil
+ end
+
+ end
end
-
Please sign in to comment.
Something went wrong with that request. Please try again.