Skip to content

Commit

Permalink
Users can update their profiles.
Browse files Browse the repository at this point in the history
Conflicts:
	Gemfile.lock
	models.rb
	rstatus.rb
	test/factories.rb
	test/rstatus_test.rb
	views/users/show.haml
  • Loading branch information
steveklabnik committed Mar 15, 2011
2 parents e839138 + fa8c43d commit ba26c7e
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem "rdiscount"
gem "ostatus"
gem "osub"
gem "opub"
gem "nokogiri", "= 1.4.4"

group :development, :test do
gem 'rack-test'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ DEPENDENCIES
haml
i18n
mongo_mapper
nokogiri (= 1.4.4)
omniauth
opub
ostatus
Expand Down
32 changes: 32 additions & 0 deletions rstatus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ class Rstatus < Sinatra::Base
end
end

get "/users/:username" do
@user = User.first :username => params[:username]
haml :"users/show"
end

# user edits own profile
get "/users/:username/edit" do
@user = User.first :username => params[:username]
if @user == current_user
haml :"users/edit"
else
redirect "/users/#{params[:username]}"
end
end

# user updates own profile
put "/users/:username" do
@user = User.first :username => params[:username]
if @user == current_user
@user.author.name = params[:name]
@user.author.email = params[:email]
@user.author.website = params[:website]
@user.author.bio = params[:bio]
@user.author.save
flash[:notice] = "Profile saved!"
redirect "/users/#{params[:username]}"
return
else
redirect "/users/#{params[:username]}"
end
end

# an alias for the above route
get "/users/:name/feed" do
feed = User.first(:username => params[:name]).feed
Expand Down
16 changes: 9 additions & 7 deletions test/factories.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
Factory.define :feed do |f|
#f.user_name "John Public"
#f.user_username "user_name"
#f.user_email "[email protected]"
#f.user_website "http://example.com"
end

Factory.sequence :update_text do |i|
Expand All @@ -23,9 +19,7 @@

Factory.define :user do |u|
u.username Factory.next(:usernames)
#u.email Factory.next(:emails)
#u.website "http://example.com"
#u.name "Something"
u.author {|a| Factory(:author, :username => a.username) }
end

Factory.sequence :integer do |i|
Expand All @@ -37,3 +31,11 @@
a.provider "twitter"
a.association :user
end

Factory.define :author do |a|
a.username "user"
a.email Factory.next(:emails)
a.website "http://example.com"
a.name "Something"
a.bio "Hi, I do stuff."
end
33 changes: 33 additions & 0 deletions test/rstatus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,40 @@ def test_subscribe_to_users_on_other_sites
click_button "Follow"
assert_match "Now following steveklabnik.", page.body
assert "/", current_path
end

def test_user_edit_own_profile_link
u = Factory(:user)
a = Factory(:authorization, :user => u)
u.finalize("http://example.com/")
log_in(u, a.uid)
visit "/users/#{u.username}"

assert has_link? "Edit your profile"
end

def test_user_edit_profile
u = Factory(:user)
a = Factory(:authorization, :user => u)
u.finalize("http://example.com/")
log_in(u, a.uid)
visit "/users/#{u.username}"
click_link "Edit your profile"

assert_equal 200, page.status_code
end

def test_user_update_profile
u = Factory(:user)
a = Factory(:authorization, :user => u)
u.finalize("http://example.com/")
log_in(u, a.uid)
visit "/users/#{u.username}/edit"
bio_text = "To be or not to be"
fill_in "bio", :with => bio_text
click_button "Save"

assert_match page.body, /#{bio_text}/
end

end
Expand Down
4 changes: 4 additions & 0 deletions views/users/_profile_field.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.profile_field{:class => attr}
%label{:for => attr}
#{attr.capitalize}:
%input{:id => attr, :name => attr, :value => @user.author.send(attr)}
18 changes: 18 additions & 0 deletions views/users/edit.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#profile
%h3= "Profile for #{@user.author.username}"

%form#profile-update{:action => "/users/#{current_user.author.username}", :method => "POST", :name => "profile_update_form"}
%input{:type => "hidden", :name => "_method", :value => "put"}

- [:name, :email, :website].each do |attr|
!= haml :"users/_profile_field", :locals => {:attr => attr}

.bio
%div
%label{:for => "bio"}
Bio:
%div
%textarea#bio{:name => "bio"}
#{@user.author.bio}

%input#update-button.button{:type => "submit", :value => "Save"}
18 changes: 17 additions & 1 deletion views/users/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@
.username
@#{@user.author.username}

- unless @user.author.email.blank?
.email
Email:
%a{:href => "mailto:#{@user.author.email}"}
#{@user.author.email}

- unless @user.author.website.blank?
.website
Website:
%a{:href => @user.author.website}
#{@user.author.website}

%p= @user.author.bio


- if current_user and @user == current_user
%a{:href=>"/users/#{current_user.username}/edit"}
Edit your profile

- if current_user and @user != current_user
#follow
- if current_user.following? @user.feed.url
Expand Down

0 comments on commit ba26c7e

Please sign in to comment.