Complete Lesson 36
This commit is contained in:
		
							parent
							
								
									2461c5b015
								
							
						
					
					
						commit
						6f95e3b3dc
					
				
							
								
								
									
										2
									
								
								app/controllers/favorites_controller.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/controllers/favorites_controller.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
class FavoritesController < ApplicationController
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										2
									
								
								app/helpers/favorites_helper.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/helpers/favorites_helper.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
module FavoritesHelper
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										4
									
								
								app/models/favorite.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								app/models/favorite.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
class Favorite < ApplicationRecord
 | 
			
		||||
  belongs_to :movie
 | 
			
		||||
  belongs_to :user
 | 
			
		||||
end
 | 
			
		||||
@ -10,6 +10,7 @@ class Movie < ApplicationRecord
 | 
			
		||||
  validates :rating, inclusion: { in: RATINGS }
 | 
			
		||||
 | 
			
		||||
  has_many :reviews, dependent: :destroy
 | 
			
		||||
  has_many :favorites, dependent: :destroy
 | 
			
		||||
 | 
			
		||||
  def flop?
 | 
			
		||||
    total_gross.blank? || total_gross < 225_000_000
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ class User < ApplicationRecord
 | 
			
		||||
  has_secure_password
 | 
			
		||||
  
 | 
			
		||||
  has_many :reviews, dependent: :destroy
 | 
			
		||||
  has_many :favorites, dependent: :destroy
 | 
			
		||||
 | 
			
		||||
  validates :name, presence: true
 | 
			
		||||
  validates :email, presence: true, format: { with: /\S+@\S+/ }, uniqueness: { case_sensitive: false }
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
Rails.application.routes.draw do
 | 
			
		||||
  resources :favorites
 | 
			
		||||
  resource :session, only: [:new, :create, :destroy]
 | 
			
		||||
  resources :users
 | 
			
		||||
  resources :reviews
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								db/migrate/20230927123732_create_favorites.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								db/migrate/20230927123732_create_favorites.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
class CreateFavorites < ActiveRecord::Migration[7.0]
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :favorites do |t|
 | 
			
		||||
      t.references :movie, null: false, foreign_key: true
 | 
			
		||||
      t.references :user, null: false, foreign_key: true
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										13
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										13
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							@ -10,7 +10,16 @@
 | 
			
		||||
#
 | 
			
		||||
# It's strongly recommended that you check this file into your version control system.
 | 
			
		||||
 | 
			
		||||
ActiveRecord::Schema[7.0].define(version: 2023_09_27_122117) do
 | 
			
		||||
ActiveRecord::Schema[7.0].define(version: 2023_09_27_123732) do
 | 
			
		||||
  create_table "favorites", force: :cascade do |t|
 | 
			
		||||
    t.integer "movie_id", null: false
 | 
			
		||||
    t.integer "user_id", null: false
 | 
			
		||||
    t.datetime "created_at", null: false
 | 
			
		||||
    t.datetime "updated_at", null: false
 | 
			
		||||
    t.index ["movie_id"], name: "index_favorites_on_movie_id"
 | 
			
		||||
    t.index ["user_id"], name: "index_favorites_on_user_id"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "movies", force: :cascade do |t|
 | 
			
		||||
    t.string "title"
 | 
			
		||||
    t.string "rating"
 | 
			
		||||
@ -43,5 +52,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_27_122117) do
 | 
			
		||||
    t.boolean "admin", default: false
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  add_foreign_key "favorites", "movies"
 | 
			
		||||
  add_foreign_key "favorites", "users"
 | 
			
		||||
  add_foreign_key "reviews", "movies"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								test/controllers/favorites_controller_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/controllers/favorites_controller_test.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
require "test_helper"
 | 
			
		||||
 | 
			
		||||
class FavoritesControllerTest < ActionDispatch::IntegrationTest
 | 
			
		||||
  # test "the truth" do
 | 
			
		||||
  #   assert true
 | 
			
		||||
  # end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										9
									
								
								test/fixtures/favorites.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								test/fixtures/favorites.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
 | 
			
		||||
 | 
			
		||||
one:
 | 
			
		||||
  movie: one
 | 
			
		||||
  user: one
 | 
			
		||||
 | 
			
		||||
two:
 | 
			
		||||
  movie: two
 | 
			
		||||
  user: two
 | 
			
		||||
							
								
								
									
										7
									
								
								test/models/favorite_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/models/favorite_test.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
require "test_helper"
 | 
			
		||||
 | 
			
		||||
class FavoriteTest < ActiveSupport::TestCase
 | 
			
		||||
  # test "the truth" do
 | 
			
		||||
  #   assert true
 | 
			
		||||
  # end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user