Skip to content

Commit b56b89f

Browse files
fix: tests, remove home controller
1 parent e082daa commit b56b89f

15 files changed

Lines changed: 97 additions & 71 deletions

app/controllers/concerns/authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def request_authentication
3535
end
3636

3737
def after_authentication_url
38-
session.delete(:return_to_after_authenticating) || root_url
38+
session.delete(:return_to_after_authenticating) || movies_url
3939
end
4040

4141
def start_new_session_for(user)

app/controllers/home_controller.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/controllers/movies_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class MoviesController < ApplicationController
2+
allow_unauthenticated_access only: %i[ index ]
23
before_action :set_current_user
34
before_action :set_movie, only: %i[ show edit update destroy ]
45

@@ -80,7 +81,7 @@ def destroy
8081

8182
private
8283
def set_current_user
83-
@current_user ||= Current.session.user
84+
@current_user ||= Current.session&.user
8485
end
8586

8687
def set_movie

app/controllers/votes_controller.rb

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
class VotesController < ApplicationController
22
before_action :set_current_user
3-
before_action :set_movie, only: %i[ create update destroy ]
3+
before_action :set_movie, only: %i[ create destroy ]
44

55
# POST /movies/:movie_id/votes
66
def create
7-
@vote = @movie.votes.new(vote_params)
8-
@vote.user = @current_user
9-
10-
if @vote.save
11-
redirect_to @movie, notice: "Vote was successfully created."
7+
@vote = case params[:vote][:vote_type]
8+
when "like"
9+
@current_user.like(@movie)
10+
when "dislike"
11+
@current_user.dislike(@movie)
1212
else
13-
redirect_to @movie, alert: "Failed to create vote."
13+
nil
1414
end
15-
end
1615

17-
# PATCH /movies/:movie_id/votes
18-
def update
19-
@vote = @movie.votes.find_by(user: @current_user)
20-
if @vote&.update(vote_params)
21-
redirect_to @movie, notice: "Vote was successfully updated."
16+
if @vote
17+
redirect_to @movie, notice: "Vote was successfully created."
2218
else
23-
redirect_to @movie, alert: "Failed to update vote."
19+
redirect_to @movie, alert: "Failed to create vote."
2420
end
2521
end
2622

2723
# DELETE /movies/:movie_id/votes/:id
2824
def destroy
29-
@vote = @movie.votes.find_by(user: @current_user)
30-
if @vote&.destroy
31-
redirect_to @movie, notice: "Vote was successfully deleted."
32-
else
33-
redirect_to @movie, alert: "Failed to delete vote."
34-
end
25+
@movie.votes.destroy_by(user: @current_user)
26+
redirect_to @movie, notice: "Vote was successfully deleted."
3527
end
3628

3729
private

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def like(movie)
2626

2727
if vote = votes.find_by(movie: movie)
2828
vote.update(vote_type: "like") unless vote.vote_type == "like"
29+
vote
2930
else
3031
votes.create(movie: movie, vote_type: "like")
3132
end
@@ -36,6 +37,7 @@ def dislike(movie)
3637

3738
if vote = votes.find_by(movie: movie)
3839
vote.update(vote_type: "dislike") unless vote.vote_type == "dislike"
40+
vote
3941
else
4042
votes.create(movie: movie, vote_type: "dislike")
4143
end

app/views/home/index.html.erb

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</head>
2525

2626
<body>
27-
<%= render partial: 'components/main_header', locals: { current_user: Current.session&.user } %>
27+
<%= render partial: 'components/main_header', locals: { current_user: @current_user } %>
2828
<main class="main">
2929
<%= yield %>
3030
</main>

app/views/movies/index.html.erb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
<p style="color: green"><%= notice %></p>
2-
3-
<% content_for :title, "Movies" %>
4-
5-
<h1>Movies</h1>
6-
7-
<div id="movies">
8-
<% @movies.each do |movie| %>
9-
<%= render movie %>
10-
<p>
11-
<%= link_to "Show this movie", movie %>
12-
</p>
13-
<% end %>
14-
</div>
15-
16-
<%= link_to "New movie", new_movie_path %>
1+
<%= render 'components/movie_sort_bar' %>
2+
<section class="content">
3+
<%= render partial: 'components/movie_list', locals: { movies: @movies, current_user: Current.session&.user } %>
4+
5+
<% if authenticated? %>
6+
<%= render 'components/new_movie_cta' %>
7+
<% end %>
8+
</section>

config/routes.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
Rails.application.routes.draw do
2-
root "home#index", controller: "HomeController"
32
resources :movies do
43
resources :votes, only: [ :create ]
5-
patch "votes" => "votes#update"
64
delete "votes" => "votes#destroy"
75
end
86
resource :session

test/controllers/movies_controller_test.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,49 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest
77
end
88

99
test "should get index" do
10-
get movies_url
10+
get movies_url(format: :json)
1111
assert_response :success
12+
json = JSON.parse(@response.body)
13+
14+
expected_movies = Movie.all.order(created_at: :desc)
15+
assert_equal expected_movies.map(&:id), json.map { |movie| movie["id"] }
16+
end
17+
18+
test "should return movies sorted by like count descending" do
19+
get movies_url(format: :json), params: { sort: "likes" }
20+
assert_response :success
21+
json = JSON.parse(@response.body)
22+
23+
expected_movies = [ movies(:two), movies(:three), movies(:one) ]
24+
assert_equal expected_movies.map(&:id), json.map { |movie| movie["id"] }
25+
end
26+
27+
test "should return movies sorted by dislike count descending" do
28+
get movies_url(format: :json), params: { sort: "dislikes" }
29+
assert_response :success
30+
json = JSON.parse(@response.body)
31+
32+
expected_movies = [ movies(:one), movies(:three), movies(:two) ]
33+
assert_equal expected_movies.map(&:id), json.map { |movie| movie["id"] }
34+
end
35+
36+
test "should return movies sorted by creation date ascending" do
37+
get movies_url(format: :json), params: { sort: "date" }
38+
assert_response :success
39+
json = JSON.parse(@response.body)
40+
41+
expected_movies = Movie.all
42+
assert_equal expected_movies.map(&:id), json.map { |movie| movie["id"] }
43+
end
44+
45+
46+
test "should return movies for a specific user" do
47+
get movies_url(format: :json), params: { user_id: users(:one).id }
48+
assert_response :success
49+
json = JSON.parse(@response.body)
50+
51+
expected_movies = [ movies(:three), movies(:one) ]
52+
assert_equal expected_movies.map(&:id), json.map { |movie| movie["id"] }
1253
end
1354

1455
test "should get new" do

0 commit comments

Comments
 (0)