Skip to content

Commit

Permalink
searching profiles also by user's login and searching only for active…
Browse files Browse the repository at this point in the history
… users
  • Loading branch information
molpe committed Nov 23, 2009
1 parent 8375b26 commit 913eaf7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Edge
* Acts_as_state_machine code replaced by AASM gem
* Better messages for bidirectional friendships
* Added translations for Brazilian Portuguese
* Fixed: searching only active profiles
* Set profile's first name == login by default

0.5.4
----
Expand Down
3 changes: 2 additions & 1 deletion app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class Profile < ActiveRecord::Base
named_scope :active, :conditions => {'users.state' => 'active'}, :include => :user

def self.site_search(query, search_options={})
Profile.find(:all, :conditions => ["first_name like ? or last_name like ?", "%#{query}%", "%#{query}%"])
q = "%#{query}%"
Profile.active.find(:all, :conditions => ["first_name like ? or last_name like ? or login like ?", q, q, q])
end

def network
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ def assert_no_difference(object, method, &block)
assert_difference object, method, 0, &block
end

def create_user(login)
Factory(:user, :login => login)
end

def create_active_user(login)
user = create_user(login)
user.activate!
user
end

end
29 changes: 24 additions & 5 deletions test/unit/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
class ProfileTest < ActiveSupport::TestCase
context "A Profile " do
setup do
@user = Factory(:user, :login => 'chavez')
@user2 = Factory(:user, :login => 'evo')

@user = create_active_user('chavez')
@user2 = create_active_user('evo')
@user3 = create_active_user('fidel')
@user4 = create_user('ghost')

@chavez = Factory(:profile, :first_name => 'Hugo', :last_name => 'Chavez', :user => @user)
@evo = Factory(:profile, :first_name => 'Evo', :last_name => 'Morales', :user => @user2)
@evo = Factory(:profile, :first_name => 'Evo', :last_name => 'Morales', :user => @user2)
@fidel = Factory(:profile, :user => @user3)
@ghost = Factory(:profile, :first_name => 'Ghost', :user => @user4)
end

should "return full_name correctly base on first_name and last_name" do
Expand Down Expand Up @@ -105,11 +109,26 @@ class ProfileTest < ActiveSupport::TestCase
@chavez.remove_following @evo
@chavez.follows? @evo
end

should "treat a friendship as mutual follower relationship between the 2 profiles" do
@evo.add_friend(@chavez)
assert @evo.follows?(@chavez)
assert @chavez.follows?(@evo)
end

context "being searched " do
should "be found if using his first name" do
assert Profile.site_search('Hugo').include?(@chavez)
end
should "be found if using his last name" do
assert Profile.site_search('Morales').include?(@evo)
end
should "be found if using his login" do
assert Profile.site_search('fidel').include?(@fidel)
end
should "not be found if user is not active" do
assert !Profile.site_search('Ghost').include?(@ghost)
end
end
end
end

0 comments on commit 913eaf7

Please sign in to comment.