Skip to content

Commit

Permalink
extract user stuff from "movies" controller to "users"
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Sep 30, 2010
1 parent 2e8b026 commit 394d4b0
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 57 deletions.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -41,4 +41,12 @@ def authentication_denied_notice
end
end
end

private

def ajax_pagination
if request.xhr?
render :partial => 'movies/movie', :collection => @movies
end
end
end
37 changes: 0 additions & 37 deletions app/controllers/movies_controller.rb
@@ -1,7 +1,6 @@
class MoviesController < ApplicationController

before_filter :find_movie, :only => [:show, :add_to_watch, :add_watched]
before_filter :find_user, :only => [:watched, :to_watch, :liked, :friends]

def index
if @query = params[:q]
Expand Down Expand Up @@ -30,42 +29,12 @@ def add_watched
ajax_actions_or_back
end

def watched
@movies = @user.watched.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def liked
@movies = @user.watched.liked.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def to_watch
@movies = @user.to_watch.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def friends
@movies = @user.movies_from_friends.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

protected

def find_movie
@movie = Movie.first(params[:id])
end

def find_user
@user = if logged_in? and params[:username] == current_user.username
current_user
else
User.first(:username => params[:username])
end

render :user_not_found, :status => 404 unless @user
end

private

def ajax_actions_or_back
Expand All @@ -75,11 +44,5 @@ def ajax_actions_or_back
redirect_to :back
end
end

def ajax_pagination
if request.xhr?
render :partial => 'movie', :collection => @movies
end
end

end
37 changes: 37 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,37 @@
class UsersController < ApplicationController

before_filter :find_user, :only => [:show, :to_watch, :liked, :friends]

def show
@movies = @user.watched.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def liked
@movies = @user.watched.liked.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def to_watch
@movies = @user.to_watch.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

def friends
@movies = @user.movies_from_friends.paginate(:page => params[:page], :per_page => 10)
ajax_pagination
end

protected

def find_user
@user = if logged_in? and params[:username] == current_user.username
current_user
else
User.first(:username => params[:username])
end

render :user_not_found, :status => 404 unless @user
end

end
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -4,4 +4,12 @@ def nobr(str)
str.gsub(/ +/, '&nbsp;')
end

def body_class(*names)
if names.empty?
@body_class && @body_class.join(' ')
else
(@body_class ||= []).concat names
end
end

end
10 changes: 4 additions & 6 deletions app/styles/movie.scss
Expand Up @@ -172,7 +172,7 @@ ol.movies {
}

/* movie details */
.show .movie {
.movie-show .movie {
margin-top:5.4em;

padding-left: $poster-medium-width + $poster-margin;
Expand Down Expand Up @@ -217,11 +217,9 @@ ol.movies {
}
}

body.watched, body.to_watch, body.liked {
> article > h1 + p {
margin: -2em 0 3em;
a { color: gray; }
}
article > h1 + nav {
margin: -2em 0 3em;
a:link, a:visited { color: gray; }
}

.pagination {
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -7,7 +7,7 @@
<%= stylesheet_link_tag "movie" %>
</head>

<body class="<%= controller.action_name %>">
<body class="<%= body_class %>">
<%= javascript_include_tag 'modernizr' %>

<header>
Expand Down
2 changes: 1 addition & 1 deletion app/views/movies/_paginated.html.erb
@@ -1,5 +1,5 @@
<ol class="movies" style="counter-reset: item <%= movies.offset %>">
<%= render :partial => 'movie', :collection => movies %>
<%= render :partial => 'movies/movie', :collection => movies %>
</ol>

<%= will_paginate movies %>
1 change: 1 addition & 0 deletions app/views/movies/show.html.erb
@@ -1,3 +1,4 @@
<% body_class 'movie-show' %>
<article class="movie">

<%= movie_poster(@movie, :medium) %>
Expand Down
Expand Up @@ -3,7 +3,7 @@
<% if not @movies.empty? %>
<h1>Friends of <%= @user.name %> watched <%= count @movies %>.</h1>

<%= render 'paginated', :movies => @movies %>
<%= render 'movies/paginated', :movies => @movies %>
<% else %>
<p>No movies found.</p>
<% end %>
Expand Down
Expand Up @@ -2,9 +2,9 @@

<% unless @movies.empty? %>
<h1><%= @user.name %> liked <%= count @movies %>.</h1>
<p><%= link_to "#{count @user.watched} in total", watched_path(@user) %></p>
<nav><p><%= link_to "#{count @user.watched} in total", watched_path(@user) %></p></nav>

<%= render 'paginated', :movies => @movies %>
<%= render 'movies/paginated', :movies => @movies %>
<% else %>
<p><%= @user.name %> didn't tell us what he/she liked yet.</p>
<% end %>
Expand Down
Expand Up @@ -2,13 +2,13 @@

<% if not @movies.empty? %>
<h1><%= @user.name %> watched <%= count @movies %>.</h1>
<p>
<nav><p>
<%= link_to "#{count @user.watched.liked} liked", liked_path(@user) %>,
<%= link_to "#{count @user.to_watch} to watch", to_watch_path(@user) %>,
<%= link_to "movies by friends", friends_path(@user) %>
</p>
</p></nav>

<%= render 'paginated', :movies => @movies %>
<%= render 'movies/paginated', :movies => @movies %>
<% elsif my_page? and @user.to_watch.count.zero? %>
<h1>Looks like you're new here.</h1>
<p>Here's the plan:</p>
Expand Down
Expand Up @@ -2,9 +2,9 @@

<% unless @movies.empty? %>
<h1><%= @user.name %> plans to watch <%= count @movies %>.</h1>
<p><%= link_to "#{count @user.watched} watched", watched_path(@user) %></p>
<nav><p><%= link_to "#{count @user.watched} watched", watched_path(@user) %></p></nav>

<%= render 'paginated', :movies => @movies %>
<%= render 'movies/paginated', :movies => @movies %>
<% else %>
<p>There are no movies <%= @user.name %> plans to watch.</p>
<% end %>
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions config/routes.rb
Expand Up @@ -4,6 +4,7 @@
match 'movies/watched/:id' => 'movies#add_watched', :as => :add_watched, :via => :put

resources :movies, :only => [:show]
resources :users, :only => [:index]

match 'director/*director' => 'movies#index', :as => :director, :via => :get

Expand All @@ -23,10 +24,10 @@
match 'user/:username' => redirect('/%{username}')
match 'user/:username/:more' => redirect('/%{username}/%{more}')

match ':username' => 'movies#watched', :as => :watched, :via => :get
match ':username/liked' => 'movies#liked', :as => :liked, :via => :get
match ':username/to-watch' => 'movies#to_watch', :as => :to_watch, :via => :get
match ':username/friends' => 'movies#friends', :as => :friends, :via => :get
match ':username' => 'users#show', :as => :watched, :via => :get
match ':username/liked' => 'users#liked', :as => :liked, :via => :get
match ':username/to-watch' => 'users#to_watch', :as => :to_watch, :via => :get
match ':username/friends' => 'users#friends', :as => :friends, :via => :get

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down

0 comments on commit 394d4b0

Please sign in to comment.