Skip to content

Commit 4d37262

Browse files
committed
you can watch another user
1 parent 99cfc2a commit 4d37262

14 files changed

Lines changed: 1308 additions & 28 deletions

File tree

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
class UsersController < ApplicationController
2-
def new
3-
@user = User.new
4-
end
5-
6-
def create
7-
@user = User.new(params[:user])
8-
if @user.save
9-
flash[:notice] = "Registration successful."
10-
redirect_to root_url
11-
else
12-
render :action => 'new'
13-
end
14-
end
2+
def new
3+
@user = User.new
4+
end
5+
6+
def create
7+
@user = User.new(params[:user])
8+
if @user.save
9+
flash[:notice] = "Registration successful."
10+
redirect_to root_url
11+
else
12+
render :action => 'new'
13+
end
14+
end
1515

16-
def edit
17-
@user = current_user
18-
end
19-
20-
def update
21-
@user = current_user
22-
if @user.update_attributes(params[:user])
23-
flash[:notice] = "Successfully updated profile."
24-
redirect_to root_url
25-
else
26-
render :action => "edit"
27-
end
16+
def edit
17+
@user = current_user
18+
end
19+
20+
def update
21+
@user = current_user
22+
if @user.update_attributes(params[:user])
23+
flash[:notice] = "Successfully updated profile."
24+
redirect_to root_url
25+
else
26+
render :action => "edit"
2827
end
28+
end
29+
30+
def show
31+
@user = User.find_by_username(params[:username])
32+
end
2933
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class WatchingsController < ApplicationController
2+
def create
3+
@watching = current_user.watchings.build(:watches_id => params[:watches_id])
4+
if @watching.save
5+
flash[:notice] = "Now watching #{@watching.watches.username}"
6+
redirect_to root_url
7+
else
8+
flash[:error] = "Unable to watch #{@watching.watches.username}."
9+
redirect_to root_url
10+
end
11+
end
12+
13+
def destroy
14+
@watching = current_user.watchings.find(params[:id])
15+
@watching.destroy
16+
flash[:notice] = "You are no longer watching #{@watching.watches.username}"
17+
redirect_to user_path(current_user.username)
18+
end
19+
end

app/helpers/watchings_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module WatchingsHelper
2+
end

app/models/user.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class User < ActiveRecord::Base
22
acts_as_authentic
3+
4+
has_many :watchings
5+
has_many :watches, :through => :watchings
36
end

app/models/watching.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Watching < ActiveRecord::Base
2+
belongs_to :user
3+
belongs_to :watches, :class_name => "User"
4+
end

app/views/users/show.html.erb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
<%= render @user %>
1+
<h1><%= @user.username %>'s Page</h1>
2+
3+
<% unless @user == current_user %>
4+
<%= link_to "Watch #{@user.username}", watchings_path(:watches_id => @user), :method => :post %>
5+
<% end %>
6+
7+
<%= @user.username %> is watching:
8+
<ul>
9+
<% for watching in @user.watchings %>
10+
<li>
11+
<%= watching.watches.username %>
12+
<% if @user == current_user %>
13+
(<%= link_to "remove", watching, :method => :delete %>)
14+
<% end %>
15+
</li>
16+
<% end %>
17+
</ul>

config/routes.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
ActionController::Routing::Routes.draw do |map|
2-
map.resources :users
2+
3+
map.resources :users, :except => :show
4+
map.user "/users/:username", :controller => "users", :action => "show"
35

46
map.root :controller => "welcome"
57

@@ -9,5 +11,6 @@
911
map.logout "logout", :controller => "user_sessions", :action => "destroy"
1012

1113
map.resources :user_sessions
14+
map.resources :watchings
1215

1316
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateWatchings < ActiveRecord::Migration
2+
def self.up
3+
create_table :watchings do |t|
4+
t.integer :user_id
5+
t.integer :watches_id
6+
7+
t.timestamps
8+
end
9+
end
10+
11+
def self.down
12+
drop_table :watchings
13+
end
14+
end

db/schema.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# It's strongly recommended to check this file into your version control system.
1111

12-
ActiveRecord::Schema.define(:version => 20091219034919) do
12+
ActiveRecord::Schema.define(:version => 20091219044706) do
1313

1414
create_table "users", :force => true do |t|
1515
t.string "username"
@@ -23,4 +23,18 @@
2323
t.datetime "updated_at"
2424
end
2525

26+
create_table "watches", :force => true do |t|
27+
t.integer "user_id"
28+
t.integer "watches_id"
29+
t.datetime "created_at"
30+
t.datetime "updated_at"
31+
end
32+
33+
create_table "watchings", :force => true do |t|
34+
t.integer "user_id"
35+
t.integer "watches_id"
36+
t.datetime "created_at"
37+
t.datetime "updated_at"
38+
end
39+
2640
end

generate

Lines changed: 1172 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)