Skip to content

Commit

Permalink
Added a username to user model
Browse files Browse the repository at this point in the history
  • Loading branch information
roca committed Dec 18, 2010
1 parent eacbb0d commit 86053d9
Show file tree
Hide file tree
Showing 22 changed files with 4,220 additions and 74 deletions.
8 changes: 7 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.search(params[:search]).paginate(:page => params[:page])
if (params[:search] && params[:search].match(/^[\w+\s+\-.@]+$/i) ) or params[:search].blank?
@feed_items = current_user.feed.search(params[:search]).paginate(:page => params[:page])
else
flash.now[:error] = "Invalid characters used. Avoid using \, or \' or \" in search"
@feed_items = current_user.feed.paginate(:page => params[:page])
end

end
end

Expand Down
16 changes: 13 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ class UsersController < ApplicationController
before_filter :admin_user, :only => :destroy

def index
@users = User.search(params[:search]).paginate(:page => params[:page])
@title = 'All users'
if (params[:search] && params[:search].match(/^[\w+\s+\-.@]+$/i) ) or params[:search].blank?
@users = User.search(params[:search]).paginate(:page => params[:page])
else
flash.now[:error] = "Invalid characters used. Avoid using \, or \' or \" in search"
@users = User.paginate(:page => params[:page])
end
@title = 'All users'
end

def new
Expand All @@ -16,7 +21,12 @@ def new

def show
@user = User.find(params[:id])
@microposts = @user.microposts.search(params[:search]).paginate(:page => params[:page])
if (params[:search] && params[:search].match(/^[\w+\s+\-.@]+$/i) ) or params[:search].blank?
@microposts = @user.microposts.search(params[:search]).paginate(:page => params[:page])
else
flash.now[:error] = "Invalid characters used. Avoid using \, or \' or \" in search"
@microposts = @user.microposts.paginate(:page => params[:page])
end
@title = @user.name
end

Expand Down
29 changes: 18 additions & 11 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@ def self.followed_by(user)
end

def self.search_by_string_fragment(fragment)
if fragment.blank?
all
else
query_array = Array.new
fragment.split.each { |f|
query_array << "(content like '%#{f}%')"
}

where(query_array.join(" and "))
end


if fragment.blank?
all
else
fragments = fragment.split

first_fragment = fragments.shift
result = where("(content like :content)",{:content => "%#{first_fragment}%"})

fragments.each { |f|
result = result.where("(content like :content)",{:content => "%#{f}%"})
}

end

return result

end
end

end
30 changes: 22 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class User < ActiveRecord::Base


attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
attr_accessible :name, :username, :email, :password, :password_confirmation

has_many :microposts, :dependent => :destroy
has_many :relationships, :dependent => :destroy,
Expand All @@ -33,11 +33,18 @@ class User < ActiveRecord::Base
:source => :follower
has_one :activation_token, :dependent => :destroy

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
username_regex = /^[\w-]+$/i


validates :name, :presence => true,
:length => { :maximum => 50}

validates :username, :presence => true,
:length => { :maximum => 50},
:format => { :with => username_regex },
:uniqueness => { :case_sensitive => false}

validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false}
Expand Down Expand Up @@ -86,7 +93,7 @@ def unfollow!(followed)


def self.authenticate(email,submitted_password)
user = find_by_email(email)
user = find_by_email(email) || find_by_username(email)
(user && user.has_password?(submitted_password)) ? user : nil
end

Expand Down Expand Up @@ -127,13 +134,20 @@ def self.search_by_string_fragment(fragment)
if fragment.blank?
all
else
query_array = Array.new
fragment.split.each { |f|
query_array << "(email like '%#{f}%' or name like '%#{f}%')"
fragments = fragment.split

first_fragment = fragments.shift
fragment_hash = {:email => "%#{first_fragment}%", :name => "%#{first_fragment}%", :username => "%#{first_fragment}%"}
result = where("(email like :email or name like :name or username like :username)",fragment_hash)

fragments.each { |f|
fragment_hash = {:email => "%#{f}%", :name => "%#{f}%", :username => "%#{f}%"}
result = result.where("(email like :email or name like :name or username like :username)",fragment_hash)
}

where(query_array.join(" and "))
end

return result

end
end
end
2 changes: 1 addition & 1 deletion app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<%= form_for(:session, :url => sessions_path) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.label :email %> or <b>Username</b><br />
<%= f.text_field :email %>
</div>
<div class="field">
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_feed_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</td>
<td class="micropost">
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
<%= link_to "#{feed_item.user.name}(#{feed_item.user.username})", feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Expand Down
4 changes: 4 additions & 0 deletions app/views/users/_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
<%= link_to "#{user.name}(#{user.username})", user %>
<% if current_user.admin? && current_user != user %>
| <%= link_to "delete", user, :method => :delete ,
:confirm => "You sure?",
Expand Down
12 changes: 12 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
<tr>
<td class="main">
<h1>
<table class="show">
<tr>
<td rowspan = 2>
<%= gravatar_for @user , :size => 100 %>
</td>
<td>
<%= @user.name %>
</td>
</tr>
<tr>
<td>(<%= @user.username %>)</td>
</tr>
</table>
</h1>

<%= render 'follow_form' if signed_in? %>
<% if @user.microposts.any? %>

Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20101214172032_add_username_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddUsernameToUsers < ActiveRecord::Migration
def self.up
add_column :users, :username, :string
end

def self.down
remove_column :users, :username
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20101203135142) do
ActiveRecord::Schema.define(:version => 20101214172032) do

create_table "activation_tokens", :force => true do |t|
t.string "token"
Expand Down Expand Up @@ -46,6 +46,7 @@
t.string "encrypted_password"
t.string "salt"
t.boolean "admin", :default => false
t.string "username"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Expand Down
3 changes: 3 additions & 0 deletions lib/tasks/sample_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ end

def make_users
admin = User.create!(:name => "Romel Campbell",
:username => "desertfox",
:email => "[email protected]",
:password => "foobar",
:passowrd_conformation => "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
username = "person-#{n+1}"
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(:name => name,
:username => username,
:email => email,
:password => password,
:password_confirmation => password)
Expand Down
4 changes: 1 addition & 3 deletions spec/controllers/microposts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@

describe "DELETE 'destroy'" do



describe "for an unauthorized user" do

before(:each) do
@user = Factory(:user)
wrong_user = Factory(:user,:email => Factory.next(:email))
wrong_user = Factory(:user, :username => Factory.next(:username),:email => Factory.next(:email))
@micropost = Factory(:micropost, :user => @user)
test_sign_in(wrong_user)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
get 'home'
response.body.should_not =~ /<body>\s*<\/body>/
end
end
end

describe "when signed in" do
before(:each) do
@user = test_sign_in(Factory(:user))
other_user = Factory(:user, :email => Factory.next(:email))
other_user = Factory(:user, :username => Factory.next(:username), :email => Factory.next(:email))
other_user.follow!(@user)
end

Expand All @@ -52,7 +52,7 @@
end


end
end

end

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/relationships_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
describe "POST 'create'" do
before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
@followed = Factory(:user, :username => Factory.next(:username), :email => Factory.next(:email))
end

it "should create a relationship" do
Expand All @@ -41,7 +41,7 @@
describe "DELETE 'destroy'" do
before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
@followed = Factory(:user, :username => Factory.next(:username), :email => Factory.next(:email))
@user.follow!(@followed)
@relationship = @user.relationships.find_by_followed_id(@followed)
end
Expand Down
22 changes: 11 additions & 11 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

before(:each) do
@user = test_sign_in(Factory(:user))
Factory(:user,:email => "[email protected]")
Factory(:user,:email => "[email protected]")

30.times do
Factory(:user,:email => Factory.next(:email) )
Factory(:user, :username => Factory.next(:username),:email => Factory.next(:email) )
end
end

Expand Down Expand Up @@ -104,12 +102,12 @@

it "should have the users name" do
get :show, :id => @user
response.should have_selector("h1", :content => @user.name)
response.should have_selector("td", :content => @user.name)
end

it "should have a profile image" do
get :show, :id => @user
response.should have_selector("h1>img", :class => "gravatar")
response.should have_selector("td>img", :class => "gravatar")
end

it "should have the right URL" do
Expand Down Expand Up @@ -141,7 +139,7 @@
describe "when signed in as another user" do

it "should be successfull" do
test_sign_in(Factory(:user, :email => Factory.next(:email)))
test_sign_in(Factory(:user, :username => Factory.next(:username), :email => Factory.next(:email)))
get :show, :id => @user
response.should be_success
end
Expand Down Expand Up @@ -189,7 +187,8 @@
describe "failure" do

before(:each) do
@attr = { :name => "",
@attr = {:name => "",
:username => "",
:email => "",
:password => "",
:password_confirmation => ""
Expand Down Expand Up @@ -218,7 +217,8 @@
describe "Success" do

before(:each) do
@attr = {:name => "Romel Campbell",
@attr = {:name => "Romel Campbell",
:username => "desertfox",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar"
Expand Down Expand Up @@ -363,7 +363,7 @@

describe "for signed-in users" do
before(:each) do
wrong_user = Factory(:user,:email => "[email protected]")
wrong_user = Factory(:user, :username => Factory.next(:username),:email => "[email protected]")
test_sign_in(wrong_user)
end

Expand Down Expand Up @@ -406,7 +406,7 @@
describe "as a admin user" do

before(:each) do
@admin = Factory(:user, :email => "[email protected]" , :admin => true)
@admin = Factory(:user, :username => Factory.next(:username), :email => "[email protected]" , :admin => true)
test_sign_in(@admin)
end

Expand Down Expand Up @@ -452,7 +452,7 @@

before(:each) do
@user = test_sign_in(Factory(:user))
@other_user = Factory(:user,:email => Factory.next(:email))
@other_user = Factory(:user, :username => Factory.next(:username),:email => Factory.next(:email))
@user.follow!(@other_user)
end

Expand Down
Loading

0 comments on commit 86053d9

Please sign in to comment.